← 输入框|github.com|刷新
Skip to content

fix: Keep the metrics server running when a Gunicorn worker exits - #7012

Merged
ntkathole merged 4 commits into
feast-dev:masterfrom
aborgatin:fix/metrics-process-worker-exit
Oct 18, 2026
Merged

ntkathole merged 4 commits into
feast-dev:masterfrom
aborgatin:fix/metrics-process-worker-exit

Conversation

@aborgatin

@aborgatin aborgatin commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

Since #6855 the metrics server runs in a multiprocessing.Process(daemon=True) started in the Gunicorn master. Every forked worker inherits multiprocessing's registry of child processes, and at worker exit multiprocessing.util._exit_function terminates every daemon process in it. So the first worker restart (--max-requests, worker timeout, reload) sends SIGTERM to the master's metrics server, and /metrics stops responding until feast serve is restarted. Every worker exit also logs AssertionError: can only join a child process. Details and steps to reproduce in #7011.

When the metrics process is started, this registers an os.register_at_fork(after_in_child=...) hook that removes it from the registry in every child forked from that process. An at-fork hook rather than a Gunicorn hook, so it also covers workers that exit before post_worker_init (e.g. a worker that fails to boot). Workers no longer terminate the metrics server; the master still terminates it on its own exit.

Tested with the steps from #7011 (--max-requests 3):

  • macOS, Python 3.12: /metrics returns 200 after the worker restarts (000 before the change), no AssertionError.
  • Linux (python:3.12-slim, 0.67.0 with this metrics.py): 0.67.0 as released: /metrics 000 after the first worker exit. With the change, 2 workers: 19 worker exits, /metrics answered all 300 checks, no AssertionError; after stopping feast serve no metrics process is left.

Which issue(s) this PR fixes:

Fixes #7011

Checks

  • I've made sure the tests are passing. (pytest sdk/python/tests/unit/test_metrics.py: 127 passed; ruff check and ruff format --check pass; mypy feast reports no errors in metrics.py)
  • My commits are signed off (git commit -s)
  • My PR title follows conventional commits format

Testing Strategy

  • Unit tests – TestMetricsProcessSurvivesForkedChildExit: start_metrics_server registers a daemon stand-in process (fork context, so it starts instantly on every platform), a forked child runs multiprocessing.util._exit_function() as a worker does at exit, then the test checks the process is still alive (before the change it is terminated with -SIGTERM). The /metrics HTTP checks are the end-to-end runs above.
  • Integration tests

🤖 Generated with Claude Code

@aborgatin
aborgatin requested a review from a team as a code owner October 14, 2026 13:06
@codecov-commenter

codecov-commenter commented Oct 14, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 50.99%. Comparing base (1299e4e) to head (125f4e8).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
sdk/python/feast/metrics.py 88.88% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7012   +/-   ##
=======================================
  Coverage   50.99%   50.99%           
=======================================
  Files         455      455           
  Lines       56970    56979    +9     
  Branches     8313     8315    +2     
=======================================
+ Hits        29051    29059    +8     
  Misses      25935    25935           
- Partials     1984     1985    +1     
Flag Coverage Δ
go-feature-server 30.58% <ø> (ø)
python-unit 52.46% <88.88%> (+<0.01%) ⬆️
Files with missing lines Coverage Δ
sdk/python/feast/metrics.py 86.19% <88.88%> (+0.10%) ⬆️

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1299e4e...125f4e8. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread sdk/python/tests/unit/test_metrics.py Outdated
pid = os.fork()
if pid == 0:
# What a worker runs at interpreter exit
multiprocessing.util._exit_function()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I reproduced the worker exit killing the metrics process and confirmed this fix keeps it alive. One small thing: the test runs the full _exit_function() inside pytest, so checking proc not in active_children() in the forked child would be safer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing! Just applied this fix, could you please check

@patelchaitany patelchaitany left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! LGTM.

aborgatin and others added 4 commits October 18, 2026 18:57
Since the metrics server runs in a multiprocessing.Process started in
the Gunicorn master, every forked worker inherits multiprocessing's
registry of child processes. At worker exit,
multiprocessing.util._exit_function terminates every daemon process in
that registry, so the first worker restart (--max-requests, timeout,
reload) sends SIGTERM to the master's metrics server and /metrics stops
responding. It also logs "AssertionError: can only join a child
process" on every worker exit.

Register an at-fork hook when the metrics process is started that
removes it from the registry in every forked child. Workers no longer
terminate it; the master still does on its own exit.

Fixes feast-dev#7011

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Alexandr Borgatin <a.borgatin@yandex.ru>
On macOS CI the spawned metrics process can take longer than the 5 s
readiness wait to start listening, so the first /metrics request was
refused. Poll /metrics (up to 30 s) before forking the child.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Alexandr Borgatin <a.borgatin@yandex.ru>
The spawned metrics server did not come up within 30 s inside the full
unit suite on macOS CI. The test is about the process surviving a
forked child's exit, not about serving HTTP, so register a fork-context
daemon process via start_metrics_server instead and assert it is still
alive after the child runs multiprocessing.util._exit_function().
Without the fix it is terminated (exitcode -SIGTERM).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Alexandr Borgatin <a.borgatin@yandex.ru>
…_exit_function

Running multiprocessing.util._exit_function() in a fork of the pytest
process runs every registered finalizer. Check what a worker's exit
would act on instead: the metrics process must not be in the child's
active_children(). Also cover _forget_metrics_process_in_child directly,
and kill the stand-in process in cleanup (terminate() can wait out the
join timeout under xdist).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Alexandr Borgatin <a.borgatin@yandex.ru>
@ntkathole
ntkathole force-pushed the fix/metrics-process-worker-exit branch from 80dc00f to 125f4e8 Compare October 18, 2026 13:27
@ntkathole
ntkathole merged commit 68c5d54 into feast-dev:master Oct 18, 2026
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feast serve: /metrics stops responding after the first gunicorn worker exits (regression in 0.67.0, #6855)

5 participants