Repository navigation
fix(lsp): process groups on both ports, hosts stop their servers - #104
Conversation
Follow-ups from the review of #102. - close_session waits for every spawn in flight under one stop step, not one step each in turn. - The Rust watcher sees the exit with waitid(WNOWAIT), kills what is left of the server's group while the exited server still holds the group id, then reaps it. A watcher dropped before the reap, as on a runtime that shuts down without close_session, kills the group too. - harness-lsp-cli calls close_session at the end of its input and on SIGINT, SIGTERM and SIGHUP, then exits with 128 plus the signal. A second signal skips the wait. The e2e harness waits for the CLI to exit instead of sending SIGTERM at once. - The TS client starts each server detached on POSIX, signals the group, and kills the group once the server has exited. Spec 5.4 and both READMEs state the group behavior and that hosts must stop their servers. - ensure_server clears its placeholder if the spawn panics. - Tests wait for the pid file, or scan /proc, instead of reading the pid right after a short bound. The concurrency stress test is now a unit test that counts the client's own spawn record, with no sleep.
There was a problem hiding this comment.
This is an auto review done by revuto.
The Rust watcher's waitid(WNOWAIT) reap order, the Unclaim guard's drop order, the single close deadline and the TS group signalling all check out. One correctness gap is left, in the CLI's signal handling: each stop_signal() call creates fresh tokio Signal streams. A signal that arrives between one call's streams being dropped and the next call's being created is caught by tokio's already-installed handler, but no receiver sees it, so it is lost.
| #[cfg(unix)] | ||
| async fn stop_signal() -> i32 { | ||
| use tokio::signal::unix::{signal, SignalKind}; | ||
| let (Ok(mut int), Ok(mut term), Ok(mut hup)) = ( |
There was a problem hiding this comment.
This is an auto review done by revuto.
Each call to stop_signal() registers new tokio::signal::unix::signal(...) streams and drops them when the future is dropped. After the first registration, tokio's process-wide handler stays installed, so the default action no longer runs. But a new Signal subscribes to the signal's watch channel and treats its current value as already seen. With no receiver alive, the broadcast is dropped. So any SIGINT/SIGTERM/SIGHUP that lands in the gap is swallowed for good:
- After
serve()wins the firstselect!inmain: the firststop_signal()future is dropped, and the stream in the loop'sselect!is only created when the loop is first polled. A host that ends stdin and sends SIGTERM at once (the casea_stop_signal_right_after_end_of_input_still_lets_the_close_finishcovers) gets exit 0 instead of 143 if the signal lands there. - After the first stop signal in the loop: the stream is recreated on the next iteration, so the second signal that should skip the wait can be lost the same way.
The close still runs, but the exit code and the second-signal escape hatch the PR documents don't hold. The window is small, so the test will rarely hit it.
Suggested fix: create the three Signal streams once at the top of main, before serve() starts, and poll the same streams with recv() in both select!s. Tokio keeps a delivery made while a stream is alive but not being polled, and the next recv() returns it.
There was a problem hiding this comment.
Fixed in a7fc88a. main registers SIGINT, SIGTERM and SIGHUP once, before serve() starts, and both select!s poll those same streams. A signal that comes while neither is polling stays in its stream for the next recv(). If registration fails, the CLI runs without stop signals, as before this PR.
No test hits the gap you describe: it lasts from one future being dropped to the next being polled. The existing CLI tests pass, and with any signal during the close taken as a skip, a_stop_signal_right_after_end_of_input_still_lets_the_close_finish still fails (exit 0 instead of 143).
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 6ac8240. Configure here.
| signal = Some(s); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Stop signal dropped at shutdown boundary
Medium Severity
The first select! treats serve() finishing and a stop signal as alternatives, and the loop then calls stop_signal() again, which builds new listeners. When stdin EOF and SIGTERM arrive together, select! can take serve(), drop the already-received signal, and leave a gap with no listener. Default terminate is already replaced, so that SIGTERM is swallowed and the CLI exits 0 instead of 128 + signal.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 6ac8240. Configure here.
There was a problem hiding this comment.
Fixed in a7fc88a, the same change as #104 (comment): the three signal streams are registered once, before serve() starts, and both waits poll them. A SIGTERM that arrives with the end of input stays in its stream when select! takes serve(), and the next recv() in the close loop returns it, so the exit code is 143.
Each wait for a stop signal registered new tokio streams, so a signal that came between one wait and the next was taken by the installed handler and lost. main now registers SIGINT, SIGTERM and SIGHUP once, before serve starts, and both waits poll the same streams, which keep a signal that comes while no one polls them.
There was a problem hiding this comment.
This is an auto review done by revuto.
Revuto completed the review and found no evidence-backed concerns.
|
Ready to land. No blockers. The items below are follow-ups and can go in a later PR. Checked and sound:
Findings:
Checks on a7fc88a:
|
Address the review of #104. - harness-lsp-cli leaves through a bounded runtime shutdown. A second stop signal no longer calls process::exit at once: the shutdown drops each server's watcher, which kills the server's process group, then the CLI exits with 128 plus the signal. - The Rust exit wait checks si_code. macOS reports a stopped or continued child to waitid(WEXITED | WNOWAIT) too; that report is now consumed and the wait goes on, so a stopped server keeps its group. - A test hook (HARNESS_LSP_CLI_TEST_PAUSE_DIR) pauses the CLI between the end of its input and the close, so the kept-signal fix has a deterministic test. A second test covers a second signal cutting the close short. - The TS client keeps a process exit handler while it has a live server; it sends SIGKILL to each one's group, as a Rust runtime shutdown does. No SIGINT or SIGTERM handler is installed. - The lsp e2e suite closes every session and Rust runner after each test, and each session has one executor, which the warmup uses too. - Spec 5.4 describes the Rust wait for spawns in flight, the stop reports, the cases that skip the group kill and the TS exit handler.
|
Re #104 (comment): all seven items are in d2d0bf9.
Reran on d2d0bf9:
|
There was a problem hiding this comment.
This is an auto review done by revuto.
Revuto completed the review and found no evidence-backed concerns.
|
Ready to land. No blockers. Five follow-ups and one nit below. I would fix 1 before the next release. Each item of the last round, checked against the code at d2d0bf9, not the reply:
No new path signals a pid or pgid after reuse. On the Rust shutdown path, Findings:
Checks on d2d0bf9:
|


The six follow-ups from the review of #102 (#102 (comment)). Each new test fails with its fix undone; the table at the end has the results.
What changed
1. One bound for every spawn in flight
close_sessionused to wait for each spawn in flight in turn, 2s each. Now one deadline, one stop step from the start of the wait, covers all of them, as servers stop in parallel (spec 5.4).2. The group is killed before the server is reaped
The watcher no longer reaps the server and then kills its group, when the group id could in theory belong to a new process. A thread waits for the exit with
waitid(P_PID, pid, WEXITED | WNOWAIT), which leaves the server unreaped. The watcher then kills the group while the exited server still holds the group id, and only then reaps it. If that thread cannot start, the watcher reaps without the group kill.3. Hosts stop their servers
harness-lsp-clicallsclose_sessionat the end of its input and on SIGINT, SIGTERM and SIGHUP, then exits with 128 plus the signal number. A second stop signal skips the wait. A signal that comes right after the end of input, as from a host that closes stdin and sends SIGTERM at once, does not cut the close short. The three signals are registered once, before the CLI reads its input, so one that comes while no wait is polling is kept, not lost.close_session, now kills the server's group. Before, onlykill_on_dropfired, and it signals the server alone. This covers a host that keeps its client in a static, as the CLI does.harness-toolsre-exportsharness-lspand starts no client, and the CLI is the only Rust host in the repo. The e2e harness drives the CLI from TypeScript: itsclose()used to end stdin and send SIGTERM at once. It now ends stdin and waits up to 15s for the CLI to exit, then sends SIGTERM.closeSession/close_sessionbefore they exit, on SIGINT and SIGTERM too.4. Port parity: the TS client and the spec
detachedon POSIX, so the server leads its own process group (and session). SIGTERM and SIGKILL go to the group, and once the server has exited, the client kills what is left of its group. Node reaps the server before itsexitevent, so the TS kill comes just after the reap; the code says so. Windows keeps signalling the process only.-with-sigterm-childsuffix: its child exits on SIGTERM, where the-with-childone ignores it. A new test on each port uses it to show that SIGTERM reaches the whole group: the child goes at the SIGTERM step while the server, which ignores SIGTERM, lives until the SIGKILL. The other tests cannot tell this apart, since the kill after the exit takes the child anyway.@agent-sh/harness-lspminor.5. Wait for the pid, or scan for the server
a_file_open_the_server_does_not_read_is_bounded_by_the_callwaits for the pid file. The start goes on after its call gave up, so the server writes it.initialize_is_bounded_and_the_server_is_stoppedno longer reads the pid at all: the server is stopped at the 300ms limit, maybe before node wrote it. Afterclose_sessionthe test looks in/procfor any process that runs the fake server for its root.6. Stress test and a panicking spawn
ensure_serverholds a guard on its claim. If the spawn panics, the guard removes theSpawningplaceholder, so the key no longer answersserver_startinguntilclose_session.The "no server entry" message for #103 goes with that work, as asked.
Tests
Each fix undone on this branch, one at a time:
close_session_bounds_all_spawns_in_flight_together(3 spawns, 6s each)close_session_waits_for_a_spawn_in_flightserverslocka_slow_spawn_on_one_key_does_not_hold_up_another_keythe_group_is_killed_before_the_server_is_reapeda_runtime_dropped_without_close_session_kills_each_server_s_groupclose_session_sends_sigterm_to_the_server_s_whole_groupa_spawn_that_panics_does_not_leave_its_key_startingclose_sessiontests/cli.rsexitreaches the server, and a signal kills the CLI before any cleanupa_stop_signal_right_after_end_of_input_still_lets_the_close_finishtakes the server's children when the server exits on exitsends SIGTERM to the server's whole process groupFound under load, and fixed here:
close_session_waits_for_a_spawn_in_flight(from fix(rust-lsp): bring the spawn client in line with the lifecycle spec #102) failed once: on a starved run its 500ms spawn took longer than the 2s bound, soclose_sessionstopped waiting, as it should for a hung exec. The test now checks the strict guarantee only whenclose_sessionreturned within the bound, and in every case that a secondclose_sessionfinds the server stopped. With no wait at all it still fails.The SIGTERM tests read how the child ended from the fake server, which outlives the SIGTERM step and records its child's exit signal. So they need no timing: without a group signal the child ends only after the server, from the kill after the exit, and nothing is recorded.
The 1.5s late start is a scratch change to the fake server for that check only, not part of this PR.
Checks
cargo test --workspace --locked: 323 passed, 0 failed.cargo clippy -p harness-lsp --all-targets --locked: no warnings in the files this PR touches; the 4 left are informat.rs,manifest.rsandtests/lsp.rs, as on main.bin/cli.rsincluded, which was not clean on main.cargo check --workspace --all-targets --locked, andcargo check -p harness-lsp --all-targetsforx86_64-pc-windows-gnuandx86_64-apple-darwin: clean, no warnings.pnpm test:dependencies,pnpm lint:harness,turbo run build typecheck testwithout harness-e2e (39 of 39, 64 harness-lsp tests) and the harness-e2e typecheck: pass.yes: the Rust spawn-client tests (15 unit tests, 19 intests/spawn_client.rs, 8 intests/spawn_client_api.rs, 3 intests/cli.rs) passed 20 of 20 runs, and the TSspawnClient.test.ts(35 tests) 20 of 20.Review changes (d2d0bf9)
From the review in #104 (comment):
std::process::exit, so each watcher'sServerGroupkills its server's group before the CLI exits with 128 plus the signal.CLD_EXITED,CLD_KILLEDorCLD_DUMPEDas an exit. A stop or continue, which macOS also reports towaitid(WEXITED | WNOWAIT), is consumed and the wait goes on, as CPython does (gh-158287: Fix blocked asyncio event loop on macOS when child process is stopped python/cpython#158962).HARNESS_LSP_CLI_TEST_PAUSE_DIR, pauses the CLI between the end of its input and the close, so a signal can be sent there.processexit handler sends SIGKILL to each live server's group, as a Rust runtime shutdown does. It goes once the last server has exited. No SIGINT or SIGTERM handler is installed.a_second_stop_signal_cuts_the_close_short_and_still_stops_every_servera_stop_signal_before_the_close_begins_is_keptwaitidreport taken as an exit (on macOS)a_stopped_server_is_not_taken_for_an_exited_oneA host script that starts a server and calls
process.exit(0)withoutcloseSessionleaves the server and its child alive without the TS handler, and nothing with it.