Repository navigation
Fix settings-managed tunnels dying on Linux, and stop-during-retry orphans - #4
Merged
Lyxot merged 1 commit intoAug 6, 2026
Conversation
…phans for PR #7875 PR_SET_PDEATHSIG is a parent-THREAD death signal, not parent-process: the kernel fires it when the thread that forked the child exits. start_remote_access forks cloudflared from a short-lived worker thread, so the connector was SIGTERMed about a second after it came online, every time. Reproduced 3/3 on a real install: the URL is logged, then state goes to error with "cloudflared exited". Auto-start shares the same worker, so it was affected too. Only Linux and WSL are hit. macOS arms no per-thread signal and Windows uses a process-wide Job Object, so both were fine, as was the launch path, which forks from the main thread. Changes: - process_lifetime: spawn_on_lifetime_thread() performs the fork on one process-lifetime daemon thread, so PDEATHSIG means "die with the parent process" again. Non-Linux spawns directly. Falls back to an inline spawn when no helper thread can be obtained, so it can never block. - cloudflare_tunnel: spawn through that helper. The abort branch now tears the connector down instead of returning the URL of a process it just detached, which could leave a live public tunnel no later stop_studio_tunnel() could reach. Restore _tunnel_state to "starting" after attempt 1's teardown so the http2 retry no longer advertises "stopping", which made stop_studio_tunnel() early-return and stop nothing, and made the Settings Stop route a no-op. - run.py: an explicit --cloudflare/--no-cloudflare/--secure on this invocation now beats an inherited _UNSLOTH_CLOUDFLARE_INTENT, so a stale export, Docker ENV or systemd Environment= cannot re-enable a tunnel the user opted out of. The marker still softens the compatibility --no-cloudflare into "unset". - Add tests for remote-access-state.ts, which had no coverage. No bugs found there; the logic is correct, it simply was not exercised. Verified: 701 passed in the focused backend slice (the one failure is a pre-existing missing optional dep, and fails on main too), 347 frontend tests, typecheck, biome and 80 simulation tests including a real-child-process tunnel lifecycle and Chromium/Firefox/WebKit CORS runs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This targets the branch behind unslothai#7875. "Allow edits by maintainers" is off on that PR, so I could not push to the branch directly. Merging this brings the fixes into unslothai#7875.
What is broken
PR_SET_PDEATHSIGis a parent-thread death signal, not parent-process. The kernel fires it when the thread that forked the child exits, which the man page states explicitly and the kernel maintainers declined to change for backwards compatibility.start_remote_accessforkscloudflaredfrom a short-lived worker thread whose last action is logging the URL. The connector is therefore SIGTERMed about a second after it comes online. Reproduced 3/3 on a real install of this branch:maybe_auto_start_remote_accessgoes through the same worker, so auto-start was affected too.This is not environmental. On the same host, standalone
cloudflaredpasses every Cloudflare pre-check and stays up, and a--securelaunch-managed tunnel from this build works end to end (public200, SPA served,401on unauthenticated API). The launch path forks from the main thread, which is why it was unaffected.Isolated proof using the repo's own
child_popen_kwargs():Only Linux and WSL are affected
child_popen_kwargs(){"preexec_fn": ...}{}terminate_all{}No existing test could catch it: every tunnel test substitutes a stub, so nothing ever spawns a real child from a worker thread.
Second issue: stop during the http2 retry
_tunnel_stateis set to"stopping"for attempt 1's teardown and never restored, so the whole http2 retry runs advertising"stopping".stop_studio_tunnelearly-returns in that state and stops nothing, and the Settings Stop route still reportsoff. The abort branch then returns the URL of a connector it just detached, leaving a live public tunnel that no later stop can reach and a controller wedged with bothcan_startandcan_stopfalse.On Linux this is currently masked by the first bug, since the orphan is killed anyway. Fixing the first without the second would expose it, so both belong together.
Changes
utils/process_lifetime.py:spawn_on_lifetime_thread()performs the fork on one process-lifetime daemon thread, so PDEATHSIG means "die with the parent process" again. Non-Linux spawns directly. Falls back to an inline spawn if no helper thread can be obtained, so it can never block.cloudflare_tunnel.py: spawn through that helper; the abort branch tears the connector down instead of returning its URL;_tunnel_stateis restored to"starting"after attempt 1's teardown.run.py: an explicit--cloudflare/--no-cloudflare/--secureon this invocation now beats an inherited_UNSLOTH_CLOUDFLARE_INTENT, so a staleexport, DockerENVor systemdEnvironment=cannot re-enable a tunnel the user opted out of. The marker still softens the compatibility--no-cloudflareintounset.studio/frontend/tests/remote-access-state.test.ts: 13 tests forremote-access-state.ts, which had no coverage. No bugs found there, the logic is correct, it simply was not exercised.Verification
maintoo.cloudflared, a backwards-compatibility matrix, and Chromium/Firefox/WebKit CORS runs.The simulations discriminate rather than just passing. Against this branch without the fix, the settings start, auto-start and retry cases fail while the launch-path case still passes.
One test note:
test_cloudflare_tunnel.pydoesmonkeypatch.setattr(ct.threading, "Thread", ...), which patches the stdlibthreadingmodule globally rather than a module-local alias. My first attempt hung because of it. I hardened around it, but that monkeypatch is worth changing.Not included
The old-CLI half of the intent problem is left alone deliberately. An older outer CLI forwards a compatibility
--no-cloudflarewithout the marker, so the backend reads a genuine opt-out and Settings reportsexplicitly_disabled. Making a missing marker meanunsetwould let a real--no-cloudflarebe overridden, and combined with persisted auto-start could publish a tunnel against the user's wishes. Failing closed is safer, so that one needs a separate decision.