Skip to content

Skip Granian's unexpected-exit log for clean worker exits - #7538

Merged
masenf merged 5 commits into
reflex-dev:mainfrom
FarhanAliRaza:farhan/granian-clean-worker-exit
Oct 13, 2026
Merged

masenf merged 5 commits into
reflex-dev:mainfrom
FarhanAliRaza:farhan/granian-clean-worker-exit

Conversation

@FarhanAliRaza

@FarhanAliRaza FarhanAliRaza commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7266. Alternative to #7457.

Problem

A group SIGTERM (or SIGINT) of a dev reflex run reaches the Granian worker and the supervisor at the same time. The worker runs its own signal handler and exits 0. The supervisor is blocked in the watchfiles reloader, so its handler has not run yet and interrupt_by_parent is not set when the per-worker watcher thread joins the worker. Granian then logs [ERROR] Unexpected exit from worker-1.

#7457 checks exitcode == -SIGTERM and interrupt_signal. In a real shutdown the worker exits 0 and the flag is usually unset, so that check never matches.

Fix

A worker process exits 0 only from its own SIGINT/SIGTERM handler. The watcher now treats exit code 0 as a requested stop and skips Granian's watcher. Crashes keep a non-zero or negative exit code and are still reported. Thread workers (free-threaded builds) have no exit code and keep Granian's handling. The socket is still released when no worker is left serving. Granian's watcher only logs in this case, because Reflex sets reload_ignore_worker_failure=True.

Validation

Full reflex run (frontend + backend), blank template, Python 3.11.15, Granian 2.8.3, Linux, group SIGTERM after the app is ready:

Build Clean shutdowns that log the error
main 8 / 10
#7457 10 / 10
this PR 0 / 10 (plus 0 / 5 on the rebased branch)

Worker SIGKILLed, then the app stopped: the crash is logged once, and the shutdown adds no error.

One unit test checks that a worker exiting 0 skips Granian's report and a SIGKILLed worker still reaches it. It fails on main.

View guided diff

A group SIGTERM or SIGINT reaches the worker and the supervisor at once.
The worker shuts down through its own signal handler and exits 0, while
the supervisor, blocked in the watchfiles reloader, has not run its
handler yet, so interrupt_by_parent is not set when the watcher thread
joins the worker. Granian then logs "Unexpected exit from worker-1".

A worker process only exits 0 from that handler, so treat exit code 0 as
a requested stop and skip granian's watcher. Crashes keep a non-zero
code and are still reported. Thread workers of free-threaded builds have
no exit code and keep granian's handling. The socket is still released
when no worker is left serving.

Fixes reflex-dev#7266
@FarhanAliRaza
FarhanAliRaza requested a review from a team as a code owner October 9, 2026 19:03
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T19:08:34.569727Z d22129c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Oct 9, 2026

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/units/utils/test_exec.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d22129cd2b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/units/utils/test_exec.py Resolved
Comment thread reflex/utils/exec.py Resolved
@greptile-apps

greptile-apps Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable new issue was found.

Summary

Prevents Granian’s unexpected-exit message when a worker exits cleanly and the supervisor receives a stop signal.

  • Clean worker shutdowns no longer trigger Granian’s unexpected-exit log.

Reviews (4) · Last reviewed commit: "Release the socket before waiting for th..." · Reviewed by Greptile

Comment thread tests/units/utils/test_exec.py Resolved
Comment thread tests/units/utils/test_exec.py Resolved
@codspeed

codspeed Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 15 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 148 untouched benchmarks


Comparing FarhanAliRaza:farhan/granian-clean-worker-exit (6591814) with main (dd41524)

Open in CodSpeed

masenf and others added 2 commits October 9, 2026 12:20
A worker exiting 0 is no longer enough on its own to skip Granian's report.
The watcher now also waits up to a second for the supervisor's interrupt
event, which its SIGINT/SIGTERM handler sets as soon as the watchfiles
reloader hands control back. A worker that exits 0 without any signal, such
as an app module raising SystemExit(0) on import, keeps the "Unexpected exit"
diagnostic after that grace period.

The regression test now also covers the clean exit releasing the socket when
no worker is left, and the unsignalled zero exit reaching Granian's watcher.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUjvFSWE66oQT5YUbSLTQZ

@cubic-dev-ai cubic-dev-ai Bot 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.

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread reflex/utils/exec.py Resolved
The watcher released the shared socket only after the grace wait, so a
worker exiting 0 with no signal left the port bound with nobody serving for
up to a second. Releasing first keeps the port closed from the moment the
worker is gone.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUjvFSWE66oQT5YUbSLTQZ
@masenf
masenf merged commit 73c7cb3 into reflex-dev:main Oct 13, 2026
146 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clean group-SIGTERM shutdown of dev "reflex run" still logs granian's "[ERROR] Unexpected exit from worker-1" (residual of #6981)

3 participants