Skip to content

Fix settings-managed tunnels dying on Linux, and stop-during-retry orphans - #4

Merged
Lyxot merged 1 commit into
Lyxot:feat/studio-public-accessfrom
unslothai:pr7875-fix-linux-tunnel-lifetime
Aug 6, 2026
Merged

Lyxot merged 1 commit into
Lyxot:feat/studio-public-accessfrom
unslothai:pr7875-fix-linux-tunnel-lifetime

Conversation

@danielhanchen

Copy link
Copy Markdown

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_PDEATHSIG is 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_access forks cloudflared from 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:

09:36:32 INFO Secure link access via Cloudflare: https://valentine-consist-rational-plasma.trycloudflare.com
09:36:33 state=error  url=None  error="cloudflared exited"

maybe_auto_start_remote_access goes through the same worker, so auto-start was affected too.

This is not environmental. On the same host, standalone cloudflared passes every Cloudflare pre-check and stays up, and a --secure launch-managed tunnel from this build works end to end (public 200, SPA served, 401 on 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():

[main thread   ] alive after 3s: True   (rc=None)
[worker thread ] alive after 3s: False  (rc=-15)  -> SIGTERM

Only Linux and WSL are affected

Platform child_popen_kwargs() Binding Settings tunnel
Linux, WSL 1/2 {"preexec_fn": ...} PDEATHSIG, per-thread broken
macOS {} cooperative + terminate_all works
Windows {} Job Object, per-process works

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_state is set to "stopping" for attempt 1's teardown and never restored, so the whole http2 retry runs advertising "stopping". stop_studio_tunnel early-returns in that state and stops nothing, and the Settings Stop route still reports off. 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 both can_start and can_stop false.

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_state is restored to "starting" after attempt 1's teardown.
  • 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.
  • studio/frontend/tests/remote-access-state.test.ts: 13 tests for remote-access-state.ts, which had no coverage. No bugs found there, the logic is correct, it simply was not exercised.

Verification

  • 701 passed in the focused backend slice. The single failure is a pre-existing missing optional dependency in my environment and fails on main too.
  • 234 passed in the tunnel, settings, lifetime, colab and middleware suites.
  • 347 frontend tests, typecheck, and biome clean on the new file.
  • 80 simulation tests in an isolated venv: OS matrix, fix verification, a real-child-process tunnel lifecycle using a fake 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.py does monkeypatch.setattr(ct.threading, "Thread", ...), which patches the stdlib threading module 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-cloudflare without the marker, so the backend reads a genuine opt-out and Settings reports explicitly_disabled. Making a missing marker mean unset would let a real --no-cloudflare be 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.

…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.
@Lyxot
Lyxot merged commit 6b2ad69 into Lyxot:feat/studio-public-access Aug 6, 2026
@Lyxot
Lyxot deleted the pr7875-fix-linux-tunnel-lifetime branch August 6, 2026 11:29
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.

3 participants