Skip to content

Terminate the frontend process tree when reflex run shuts down - #7209

Closed
flcrom wants to merge 3 commits into
reflex-dev:mainfrom
flcrom:fix/notty-signal-shutdown
Closed

flcrom wants to merge 3 commits into
reflex-dev:mainfrom
flcrom:fix/notty-signal-shutdown

Conversation

@flcrom

@flcrom flcrom commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

When reflex run is started without a TTY (CI jobs, containers, process supervisors), SIGTERM or SIGINT stops the backend but the launcher hangs inside run_concurrently_context until something SIGKILLs the process group: the frontend dev server keeps running, its log-streaming thread never finishes, and the exit path waits on it forever.

What this changes:

  • Processes launched with new_process(..., run_managed=True) are tracked on a registry scoped to run_concurrently_context. When the run unwinds, each tracked child is terminated: on POSIX the frontend is started in its own session so its whole process group (bun and its node children) gets SIGTERM, with SIGKILL escalation after a timeout; on Windows the tree is terminated via psutil. Registration is opt-in, and only the frontend dev server opts in.
  • In frontend-only mode there is no backend server to install signal handlers, so a bare SIGTERM killed the launcher outright and orphaned the frontend. SIGTERM/SIGINT are now handled there so the run context unwinds, the frontend is terminated, and the launcher exits cleanly. A frontend task that fails still propagates its own exception instead of exiting 0.

I reproduced the hang on current main (launcher stuck after SIGTERM until SIGKILL), then reran with this change: all four combinations (frontend-only/fullstack, SIGTERM/SIGINT) exit 0 promptly with no bun/node/granian processes left behind.

Tests: uv run pytest -q tests/units/test_reflex.py tests/units/utils/test_processes.py tests/units/utils/test_exec.py (85 passed), ruff check/format, pyright.

Closes #6980

Review in cubic

@flcrom
flcrom requested a review from a team as a code owner September 19, 2026 04:47
@greptile-apps

greptile-apps Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Changes process lifecycle management in the dev launcher.

The PR appears safe to merge based on the changes since the previous review and the current state of the resolved findings.

Summary

The PR tracks the frontend dev server for process-tree cleanup and handles signals in frontend-only mode. Since the previous review, it adds a Windows guard against sweeping an already-exited root PID and portable process-status checks in tests.

Reviews (5) · Last reviewed commit: "Avoid reused PID teardown and detect exi..."

Comment thread reflex/reflex.py Resolved
Comment thread reflex/utils/processes.py Resolved
Comment thread tests/units/utils/test_processes.py Resolved
@codspeed

codspeed Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 4.8%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 128 untouched benchmarks
⏩ 17 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_var_access[mutable_dataclass_list] 483.4 ms 461.2 ms +4.8%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing flcrom:fix/notty-signal-shutdown (f5d8953) with main (fe2a78f)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@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.

2 issues found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="reflex/reflex.py">

<violation number="1" location="reflex/reflex.py:453">
P2: On Windows, frontend-only SIGTERM still bypasses the new cleanup path because this condition disables both handlers. Install the handlers on Windows too, so catchable termination signals unwind `run_concurrently_context` and invoke its psutil process-tree cleanup.</violation>
</file>

<file name="tests/units/test_reflex.py">

<violation number="1" location="tests/units/test_reflex.py:640">
P2: If an assertion fails after the frontend tree has spawned, the finally block kills only `child_pid`; the grandchild stand-in for bun/node and its own child keep running in their own session for up to ~60s or longer. The child was started with `start_new_session=True`, so sending SIGKILL to its whole process group (`os.killpg(child_pid, ...)`) tears down the entire tree instead of just the leader. This is exactly the lingering-process scenario the PR targets, and a failing regression test would leak processes in CI.</violation>
</file>

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

Re-trigger cubic

Comment thread reflex/utils/processes.py Resolved
Comment thread reflex/reflex.py Resolved
Comment thread reflex/reflex.py
Comment on lines +453 to +454
and sys.platform != "win32"
and threading.current_thread() is threading.main_thread()

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.

P2: On Windows, frontend-only SIGTERM still bypasses the new cleanup path because this condition disables both handlers. Install the handlers on Windows too, so catchable termination signals unwind run_concurrently_context and invoke its psutil process-tree cleanup.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At reflex/reflex.py, line 453:

<comment>On Windows, frontend-only SIGTERM still bypasses the new cleanup path because this condition disables both handlers. Install the handlers on Windows too, so catchable termination signals unwind `run_concurrently_context` and invoke its psutil process-tree cleanup.</comment>

<file context>
@@ -436,21 +441,66 @@ def _run_dev(
+    frontend_only_signals = (
+        running_mode.has_frontend()
+        and not running_mode.has_backend()
+        and sys.platform != "win32"
+        and threading.current_thread() is threading.main_thread()
+    )
</file context>
Suggested change
and sys.platform != "win32"
and threading.current_thread() is threading.main_thread()
and threading.current_thread() is threading.main_thread()

for pid in [child_pid] if child_pid else []:
if not _pid_gone(pid):
with contextlib.suppress(ProcessLookupError):
os.kill(pid, signal_mod.SIGKILL)

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.

P2: If an assertion fails after the frontend tree has spawned, the finally block kills only child_pid; the grandchild stand-in for bun/node and its own child keep running in their own session for up to ~60s or longer. The child was started with start_new_session=True, so sending SIGKILL to its whole process group (os.killpg(child_pid, ...)) tears down the entire tree instead of just the leader. This is exactly the lingering-process scenario the PR targets, and a failing regression test would leak processes in CI.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/units/test_reflex.py, line 640:

<comment>If an assertion fails after the frontend tree has spawned, the finally block kills only `child_pid`; the grandchild stand-in for bun/node and its own child keep running in their own session for up to ~60s or longer. The child was started with `start_new_session=True`, so sending SIGKILL to its whole process group (`os.killpg(child_pid, ...)`) tears down the entire tree instead of just the leader. This is exactly the lingering-process scenario the PR targets, and a failing regression test would leak processes in CI.</comment>

<file context>
@@ -431,3 +435,268 @@ def test_init_records_version_check_after_frontend_setup(
+        for pid in [child_pid] if child_pid else []:
+            if not _pid_gone(pid):
+                with contextlib.suppress(ProcessLookupError):
+                    os.kill(pid, signal_mod.SIGKILL)
+
+
</file context>
Suggested change
os.kill(pid, signal_mod.SIGKILL)
os.killpg(pid, signal_mod.SIGKILL)

Comment thread tests/units/test_reflex.py Resolved
Comment thread tests/units/utils/test_processes.py Resolved
Comment thread tests/units/utils/test_processes.py Resolved
@flcrom
flcrom force-pushed the fix/notty-signal-shutdown branch from 88b0fdf to e4d2667 Compare September 19, 2026 05:15

@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 4 files (changes from recent commits).

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

Re-trigger cubic

Comment thread reflex/utils/processes.py Resolved
Comment thread tests/units/test_reflex.py Resolved
Without a TTY, SIGTERM/SIGINT stopped the Granian backend but left the
frontend dev server running: run_concurrently_context then blocked
forever waiting on the thread streaming the child's output, and only
SIGKILL of the whole process group stopped the launcher.

Children spawned by run tasks are now tracked in a context-scoped
registry (thread-local binding, so unrelated new_process callers are
never touched) and terminated when the context unwinds - on normal
exit, KeyboardInterrupt, and body failure alike. The frontend dev
server runs in its own session on POSIX, so teardown signals its whole
process group and grandchildren cannot survive holding the output pipe
open. On Windows the tree is terminated via psutil. Late registrations
during shutdown are terminated immediately.

Closes reflex-dev#6980
@flcrom
flcrom force-pushed the fix/notty-signal-shutdown branch from e4d2667 to 48f8890 Compare September 19, 2026 05:26

@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 (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/utils/processes.py Resolved
Comment thread tests/units/test_reflex.py Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backgrounded/no-TTY reflex run ignores SIGTERM/SIGINT — requires SIGKILL of the process group

2 participants