Skip to content

fix(lsp): process groups on both ports, hosts stop their servers - #104

Merged
avifenesh merged 3 commits into
mainfrom
fix/lsp-review-followups
Nov 5, 2026
Merged

avifenesh merged 3 commits into
mainfrom
fix/lsp-review-followups

Conversation

@avifenesh

@avifenesh avifenesh commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

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_session used 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-cli calls close_session at 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.
  • In the Rust client, a watcher dropped before it reaped its server, as on a runtime that shuts down without close_session, now kills the server's group. Before, only kill_on_drop fired, and it signals the server alone. This covers a host that keeps its client in a static, as the CLI does.
  • harness-tools re-exports harness-lsp and starts no client, and the CLI is the only Rust host in the repo. The e2e harness drives the CLI from TypeScript: its close() 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.
  • Both READMEs tell hosts to call closeSession / close_session before they exit, on SIGINT and SIGTERM too.

4. Port parity: the TS client and the spec

  • The TS client starts each server with detached on 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 its exit event, so the TS kill comes just after the reap; the code says so. Windows keeps signalling the process only.
  • Spec 5.4 now states the group behavior on both ports and that hosts must stop their servers.
  • The fake server gains a -with-sigterm-child suffix: its child exits on SIGTERM, where the -with-child one 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.
  • Changeset: @agent-sh/harness-lsp minor.

5. Wait for the pid, or scan for the server

  • a_file_open_the_server_does_not_read_is_bounded_by_the_call waits 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_stopped no longer reads the pid at all: the server is stopped at the 300ms limit, maybe before node wrote it. After close_session the test looks in /proc for any process that runs the fake server for its root.

6. Stress test and a panicking spawn

  • The concurrency stress test moved into the unit tests. It counts the client's own spawn record, so a slow extra server can no longer be missed, and the 300ms sleep is gone. 50 rounds of 16 tasks take about 1s instead of about 16s.
  • ensure_server holds a guard on its claim. If the spawn panics, the guard removes the Spawning placeholder, so the key no longer answers server_starting until close_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:

Fix undone Test Result
One 2s wait per spawn in flight, in turn close_session_bounds_all_spawns_in_flight_together (3 spawns, 6s each) fails: "close_session took 6.0s for three spawns in flight"
No wait for a spawn in flight at all close_session_waits_for_a_spawn_in_flight fails: "close_session returned before the spawn ended"
A slow spawn holds the servers lock a_slow_spawn_on_one_key_does_not_hold_up_another_key fails at its 4s limit
Group killed after the reap the_group_is_killed_before_the_server_is_reaped fails: "the group was killed after the server was reaped"
No group kill when the watcher is dropped a_runtime_dropped_without_close_session_kills_each_server_s_group fails: "the server's child outlived the runtime"
SIGTERM and SIGKILL to the server only (Rust) close_session_sends_sigterm_to_the_server_s_whole_group fails: the child never ended while the server lived
A panicking spawn leaves its placeholder a_spawn_that_panics_does_not_leave_its_key_starting fails: "the key stayed starting after the spawn panicked"
A second call on a key in flight spawns again the stress test, now a unit test fails in round 0
The CLI exits without close_session the three tests in tests/cli.rs all fail: at the end of input no exit reaches the server, and a signal kills the CLI before any cleanup
Any signal during the close skips the wait a_stop_signal_right_after_end_of_input_still_lets_the_close_finish fails: exit 0 instead of 143, the close cut short
TS: no own process group the three new group tests all fail
TS: no group kill after the exit takes the server's children when the server exits on exit fails
TS: signals to the server only sends SIGTERM to the server's whole process group fails: the child never ended while the server lived
Old pid reads, with node made to start 1.5s late the two tests in point 5 both panic on the missing pid file; the new versions pass

Found 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, so close_session stopped waiting, as it should for a hung exec. The test now checks the strict guarantee only when close_session returned within the bound, and in every case that a second close_session finds the server stopped. With no wait at all it still fails.
  • The slow-spawn tests now block 6s per spawn and allow 4s, so a starved run has room while an undone fix still fails clearly.

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 in format.rs, manifest.rs and tests/lsp.rs, as on main.
  • rustfmt: every touched Rust file is clean, bin/cli.rs included, which was not clean on main.
  • cargo check --workspace --all-targets --locked, and cargo check -p harness-lsp --all-targets for x86_64-pc-windows-gnu and x86_64-apple-darwin: clean, no warnings.
  • Concurrency stress, 16 tasks over 50 rounds: pass.
  • pnpm test:dependencies, pnpm lint:harness, turbo run build typecheck test without harness-e2e (39 of 39, 64 harness-lsp tests) and the harness-e2e typecheck: pass.
  • Load, on two CPUs each shared with a busy yes: the Rust spawn-client tests (15 unit tests, 19 in tests/spawn_client.rs, 8 in tests/spawn_client_api.rs, 3 in tests/cli.rs) passed 20 of 20 runs, and the TS spawnClient.test.ts (35 tests) 20 of 20.
  • No fake server or child was left after any run.

Review changes (d2d0bf9)

From the review in #104 (comment):

  • A second stop signal still stops every group. The CLI leaves through a bounded runtime shutdown instead of std::process::exit, so each watcher's ServerGroup kills its server's group before the CLI exits with 128 plus the signal.
  • Stop reports on macOS. The exit wait takes only CLD_EXITED, CLD_KILLED or CLD_DUMPED as an exit. A stop or continue, which macOS also reports to waitid(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).
  • Deterministic CLI tests. A test hook, 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.
  • e2e cleanup. The lsp e2e suite closes each session's client and each Rust runner after its test, and keeps one executor per session, which the warmup uses too.
  • TS exit handler. While a client has a live server, a process exit 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.
  • 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.
Fix undone Test Result
Second signal exits at once a_second_stop_signal_cuts_the_close_short_and_still_stops_every_server fails: "the server outlived the CLI"
Second signal does not cut the close short the same test fails: exit 143 after the full close, not 130
Signals registered again for the close (a7fc88a undone) a_stop_signal_before_the_close_begins_is_kept fails: exit 0, "the SIGTERM was lost"
Any waitid report taken as an exit (on macOS) a_stopped_server_is_not_taken_for_an_exited_one fails: "a stopped server was taken for an exited one"; passes 11 of 11 with the fix, and on Linux either way
TS: no exit handler the two new exit-handler tests both fail

A host script that starts a server and calls process.exit(0) without closeSession leaves the server and its child alive without the TS handler, and nothing with it.

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.

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

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.

Comment thread crates/lsp/src/bin/cli.rs Outdated
#[cfg(unix)]
async fn stop_signal() -> i32 {
use tokio::signal::unix::{signal, SignalKind};
let (Ok(mut int), Ok(mut term), Ok(mut hup)) = (

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 first select! in main: the first stop_signal() future is dropped, and the stream in the loop's select! is only created when the loop is first polled. A host that ends stdin and sends SIGTERM at once (the case a_stop_signal_right_after_end_of_input_still_lets_the_close_finish covers) 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

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

Comment thread crates/lsp/src/bin/cli.rs
signal = Some(s);
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 6ac8240. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

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

This is an auto review done by revuto.


Revuto completed the review and found no evidence-backed concerns.

@avifenesh

Copy link
Copy Markdown
Collaborator Author

Ready to land. No blockers. The items below are follow-ups and can go in a later PR.

Checked and sound:

  • Rust watcher: the group kill runs while the exited server is still a zombie, and child.id() stays Some until tokio reaps it, so neither signal_server nor ServerGroup::kill can reach a freed pid. When a kill message wins select! after the notice, only the child.wait() future is dropped, and the next pass reaps.
  • Drop path: when a suspended watch_process future is dropped, locals drop before parameters. So group sends SIGKILL before the drop of child can reap the server. I checked that order with a small program. The code depends on it but does not say so; one comment at let mut group would stop someone from reordering it.
  • Unclaim: every return or unwind releases the servers guard before Unclaim::drop locks it, so it cannot deadlock. A poisoned lock is handled.
  • close_session: one deadline now covers every spawn in flight.
  • The Revuto and Bugbot thread (a signal lost between two stop_signal() calls) is fixed in a7fc88a. The three streams are created once, before serve(), and both select!s poll them.
  • TS detached: the host still waits for its servers and still owns their pipes. A Node host exits by itself after closeSession(). Without it, the host stays up while a server runs, as on main. Windows keeps detached: false, so no console window opens.

Findings:

  1. Follow-up. crates/lsp/src/bin/cli.rs:114: on the second stop signal the CLI calls std::process::exit at once. No destructor runs, so the ServerGroup kill never fires. A server that is partway through its stop stays alive, with its children. Repro: start the CLI with a stubborn-with-child server, send SIGTERM, then SIGINT 300ms later. The CLI exits 130 at once, and both the server and its child are still alive 11s later. A server that exits on stdin EOF does go, but its children stay. Fix: kill each server's group before exiting. For example, leave main through a runtime shutdown with a short timeout, so each watcher's ServerGroup drop runs, then exit with 128 plus the signal.

  2. Follow-up. crates/lsp/src/spawn_client.rs:652: any waitid that returns 0 is taken as an exit. On macOS, waitid(P_PID, pid, WEXITED | WNOWAIT) also returns when the child is stopped, with si_code == CLD_STOPPED. This is a Darwin kernel quirk; CPython fixed the same bug in gh-158287: Fix blocked asyncio event loop on macOS when child process is stopped python/cpython#158962. I checked it with a small C program on macOS 27: with a child under SIGSTOP, three calls in a row each return 0 with CLD_STOPPED, while Linux blocks. On macOS, then, a server that gets SIGSTOP (from a debugger, or kill -STOP) is treated as exited, and its whole group gets SIGKILL. Fix: check info.si_code for CLD_EXITED, CLD_KILLED or CLD_DUMPED. On a stop, consume the stop report as CPython does and wait again. A plain retry would spin, because WNOWAIT leaves the report pending. Related: when waitid fails, the group kill is skipped and nothing records it, while spec 5.4 says the group is killed "for any reason".

  3. Follow-up. Two CLI behaviors have no test that fails when they are undone:

    • The a7fc88a fix. With cli.rs from 6ac8240, tests/cli.rs passed 3 of 3 runs. The reply in the review thread says the same.
    • "A second stop signal skips the wait" has no test at all.

    A test hook in the CLI that pauses between the end of serve() and the close loop would make the first deterministic: send SIGTERM during the pause and expect 143. For the second: a stubborn server, two signals, and an exit right after the second one.

  4. Follow-up. packages/harness-e2e/test/lsp.e2e.hard.test.ts:39-40: pickLspExecutor keeps tool and execute and drops close. So the new close() at lspExecutorRust.ts:185 has no caller in the lsp suite. Each Rust runner runs until the vitest worker exits, and so do the TS sessions there, which never call closeSession(). The CLI now stops its servers at end of input, so nothing outlives the worker, but the new close() never runs. Also, warmupLsp in Rust mode starts its own runner. It warms a different CLI from the one the test then uses.

  5. Follow-up. Port parity: the TS client has nothing like the Rust group kill on runtime drop. A Node host that calls process.exit() without closeSession() leaves each server's children running. Before this PR, a Ctrl-C at a terminal still reached them through the host's group; with detached, it does not. A process.once("exit", ...) handler that sends SIGKILL to each live server's group is synchronous and cheap, and would match Rust. Spec 5.4 already states the difference, so keeping it is also fine.

  6. Follow-up. agent-knowledge/design/lsp.md:315: spec 5.4 does not describe the Rust wait for spawns in flight. close_session waits one stop step in total for them. A spawn that takes longer stops its own server after close_session has returned. The PR body cites 5.4 for this, but the spec does not say it.

  7. Nit. packages/lsp/src/spawnClient.ts:735: the old /** True while the process was spawned and has not exited yet. */ now sits above the doc comment of signalServer, and isRunning at line 768 has no doc.

Checks on a7fc88a:

  • cargo test -p harness-lsp --locked: all pass (15 unit, 3 cli, 16 lsp, 19 spawn_client, 8 spawn_client_api).
  • cargo clippy -p harness-lsp --all-targets --locked: 4 warnings, all in files this PR does not touch (format.rs, manifest.rs, tests/lsp.rs).
  • cargo fmt -p harness-lsp --check: diffs only in untouched files (fence.rs, manifest.rs, schema.rs, tests/lsp.rs). The touched files are clean.
  • cargo check -p harness-lsp --all-targets --locked for x86_64-pc-windows-gnu and aarch64-apple-darwin: clean.
  • turbo run build typecheck test --filter=@agent-sh/harness-lsp: 64 tests pass. The harness-e2e typecheck passes.
  • Spawn-client tests looped 10 times on each port. Rust (lib, spawn_client, spawn_client_api, cli): 10 of 10 passed. TS (spawnClient.test.ts): 10 of 10 passed.
  • Fix undone, test fails:
    • Group killed after the reap: the_group_is_killed_before_the_server_is_reaped fails (Some(false)).
    • ServerGroup drop made a no-op: a_runtime_dropped_without_close_session_kills_each_server_s_group fails ("the server's child outlived the runtime").
    • TS, no killGroup on exit: "takes the server's children when the server exits on exit" fails.
    • TS, signals to the server only: "sends SIGTERM to the server's whole process group" fails (no child-exit file).

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

Copy link
Copy Markdown
Collaborator Author

Re #104 (comment): all seven items are in d2d0bf9.

  1. Second signal. main now leaves through a bounded runtime shutdown (500ms) instead of std::process::exit. Dropping the tasks drops each watcher, whose ServerGroup kills the server's group, and then the CLI exits with 128 plus the signal. New test a_second_stop_signal_cuts_the_close_short_and_still_stops_every_server, your repro: a stubborn-with-child server, SIGTERM, SIGINT 300ms later. The CLI exits 130 within 3s, and the server and its child are gone. With std::process::exit back it fails: "the server outlived the CLI".
  2. Stop reports on macOS. The exit wait checks si_code and takes only CLD_EXITED, CLD_KILLED or CLD_DUMPED as an exit. Any other report (a stop, a continue) is consumed with waitid(WSTOPPED | WCONTINUED | WNOHANG), as CPython does, and the wait goes on; a 10ms pause keeps an unknown report from spinning the loop. New test a_stopped_server_is_not_taken_for_an_exited_one, in a module that needs no /proc, so it runs on macOS too: it SIGSTOPs the server, checks after 500ms that the client still counts it as running and that it and its child live, SIGCONTs it, checks again, and closes. On macOS it fails without the fix ("a stopped server was taken for an exited one") and passed 11 of 11 runs with it; on Linux it passes either way, since Linux blocks. Spec 5.4 now says the group kill is skipped when the exit cannot be seen without a reap (waitid fails, or its thread cannot start).
  3. Deterministic CLI tests. A test hook, HARNESS_LSP_CLI_TEST_PAUSE_DIR, pauses the CLI between the end of its input and the close. a_stop_signal_before_the_close_begins_is_kept sends SIGTERM during that pause and expects 143. With the streams registered again for the close, as before a7fc88a, it fails: exit 0, "the SIGTERM was lost". The second-signal test above also fails when a second signal no longer cuts the close short (143 after the full close, not 130).
  4. e2e. pickLspExecutor keeps one executor per session, and each session's client and each Rust runner is registered for an afterEach that closes them. On the warmup: makeLspExecutorRust already caches its runner per session in a WeakMap, so the warmup and the test shared one CLI. The per-session executor now makes that explicit. The suite needs a model to run, so this was checked with the e2e typecheck only.
  5. TS exit handler. While a client has a live server, it keeps one process.once("exit") handler that sends SIGKILL to each live server's group; the handler goes when the last server exits, so closeSession removes it. No SIGINT or SIGTERM handler is installed. Two new tests: one calls the handler as Node would at process.exit() and checks the server and its child are gone, the other checks closeSession removes it. Both fail without the handler. A host script that starts a server and calls process.exit(0) without closeSession leaves the server and its child alive without the handler, and nothing with it.
  6. Spec 5.4 has a new bullet on spawns in flight: one stop step in total, and a spawn that takes longer stops its own server after close_session returns. The TS client spawns synchronously, so it has none.
  7. Doc comments in spawnClient.ts are back in place. The drop-order comment you suggested is above let mut group.

Reran on d2d0bf9:

  • cargo test --workspace --locked: 326 passed. clippy: no warnings in touched files. rustfmt: touched files clean.
  • cargo check -p harness-lsp --all-targets for x86_64-pc-windows-gnu, x86_64-apple-darwin and aarch64-apple-darwin: clean. On macOS, cargo test -p harness-lsp passes.
  • pnpm test:dependencies, pnpm lint:harness, turbo run build typecheck test without harness-e2e (39 of 39, 66 harness-lsp tests), and the harness-e2e typecheck: pass.
  • Load, two CPUs each shared with a busy yes: the Rust spawn-client tests (16 unit, 19 in tests/spawn_client.rs, 8 in tests/spawn_client_api.rs, 5 in tests/cli.rs) passed 20 of 20 runs, and the TS spawnClient.test.ts (37 tests) 20 of 20. No fake server was left after any run.

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

This is an auto review done by revuto.


Revuto completed the review and found no evidence-backed concerns.

@avifenesh

Copy link
Copy Markdown
Collaborator Author

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:

  1. Second signal: done. run returns 128 plus the second signal, and main shuts the runtime down. That drops each watcher, so its ServerGroup kills the group while the server is still unreaped. a_second_stop_signal_cuts_the_close_short_and_still_stops_every_server fails 3 of 3 with std::process::exit(128 + s) back, and 3 of 3 with the runtime leaked (mem::forget) instead of shut down: "the server outlived the CLI".
  2. Stop reports on macOS: done. Only CLD_EXITED, CLD_KILLED and CLD_DUMPED count as an exit. EINTR retries. The consuming waitid has no WEXITED, so it cannot reap. A small C probe on macOS 27: two waitid(WEXITED | WNOWAIT) calls in a row return CLD_STOPPED; after waitid(WSTOPPED | WCONTINUED | WNOHANG) the next call blocks, and it still blocks after a SIGCONT. So the 10ms pause never turns into a poll while a server stays stopped. a_stopped_server_is_not_taken_for_an_exited_one passes 5 of 5 on macOS and fails 3 of 3 there with the si_code check undone. On Linux it passes either way, as the reply says.
  3. Pause hook: acceptable, with finding 4.
  4. e2e: done. One executor per session, and afterEach closes each client and each runner. The warmup and the test share one CLI.
  5. TS exit handler: done, with findings 1 and 2. It goes when the last server exits: the new closeSession test checks it, and with 11 clients the exit listener count is back to 0 after closeSession. It signals only servers whose exit event has not run. Node reaps a server just before that event, so this is the same window spec 5.4 already accepts for the group kill after exit. On Windows it calls proc.kill, which ends the server through its process handle, so a reused pid cannot be hit.
  6. Spec 5.4: done. The new bullet matches the code: one deadline for all spawns in flight.
  7. Doc comments: done.

No new path signals a pid or pgid after reuse. On the Rust shutdown path, group drops before child, and the watcher disarms the group right after its reap.

Findings:

  1. Follow-up. packages/lsp/src/spawnClient.ts:131: each client adds its own process exit listener. Servers in one client share it, clients do not. With 11 clients that each have a live server, Node prints MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process] to the host's stderr. A host that makes one client per session, as the e2e suite does, hits this at 11 concurrent sessions. Fix: one module-level set of live servers and one handler for all clients.

  2. Follow-up. packages/lsp/test/spawnClient.test.ts:253: "kills each live server's group when the host exits without closeSession" passes 3 of 3 with the handler changed to proc.kill("SIGKILL"), which kills the server only. After the test calls the handler, the event loop runs, and the client's own exit listener kills the group. After a real process.exit(), no event loop runs. With that change, a host script that starts a well-behaved-with-child server and calls process.exit(0) leaves the child alive, 3 of 3. With the PR's code, the server and its child are gone, 3 of 3. Fix: run such a host script as a child process in the test, and check that the server and its child are gone once it exits.

  3. Follow-up. crates/lsp/tests/cli.rs:233: the second-signal test needs the CLI to take SIGTERM in the 300ms before SIGINT. If the CLI does not run in that window, both signals are pending at once. StopSignals::recv (cli.rs:211) then picks one at random, since tokio::select! is not biased. If it picks SIGINT first, the CLI exits 143 and line 235 fails. With the CLI frozen by SIGSTOP across both signals, 8 of 10 runs exited 143. That takes a 300ms stall, so it is rare, but a loaded CI runner can do it. Fix: wait for a sign that the close has begun before SIGINT. For example, have the fake write got-shutdown on every shutdown, not only in hold-shutdown-reply, and wait for that file. The other 300ms wait, at line 212, cannot fail on this code: a signal sent during the pause is kept, and the close loop sees it on its first poll, before the close can finish. Under load it can only miss a regression.

  4. Follow-up. crates/lsp/src/bin/cli.rs:100 and :132: the pause hook ships in release builds. If HARNESS_LSP_CLI_TEST_PAUSE_DIR is set, then at end of input the CLI creates or truncates paused in that directory (through a symlink too), and waits for resume with no time limit. While it waits, SIGINT, SIGTERM and SIGHUP are caught and do nothing. Only SIGKILL stops it, and then no server group is killed. This is not a privilege problem: whoever sets the CLI's environment already controls its servers' PATH and NODE_OPTIONS. But it is a trap, and only the source comment says it is for tests. Fix: compile it only with debug_assertions, and gate the test the same way. cargo test builds the binary in the dev profile; cargo install builds it in release.

  5. Follow-up. macOS coverage: on macOS, cargo test -p harness-lsp runs one spawn-client test, the new one. The tests module in spawn_client.rs, tests/spawn_client.rs, tests/spawn_client_api.rs and tests/cli.rs are Linux-only, so the macOS paths of the group kill and the CLI stop have no other test. Liveness checks with kill(pid, 0), as unix_tests::alive does, would let most of them run there.

  6. Nit. crates/lsp/src/bin/cli.rs:118: after a stop signal with stdin still open, the CLI now exits about 500ms late. The stdin reader's blocking thread never ends, so shutdown_timeout always waits its full bound. With a well-behaved server: exit 3ms after end of input, 515ms after SIGTERM. Fix: read stdin on a plain thread outside tokio's blocking pool, so the shutdown ends once the tasks are dropped. Keeping it is also fine: 500ms is small next to a full close.

Checks on d2d0bf9:

  • cargo test -p harness-lsp --locked: all pass (16 unit, 5 cli, 16 lsp, 19 spawn_client, 8 spawn_client_api).
  • cargo clippy -p harness-lsp --all-targets --locked: 4 warnings, all in files this PR does not touch (format.rs, manifest.rs, tests/lsp.rs).
  • cargo fmt -p harness-lsp --check: diffs only in untouched files (fence.rs, manifest.rs, schema.rs, tests/lsp.rs).
  • cargo check -p harness-lsp --all-targets --locked --target x86_64-pc-windows-gnu: clean.
  • macOS (aarch64), native: cargo test -p harness-lsp --locked passes (1 unit, 16 lsp).
  • turbo run build typecheck test --filter=@agent-sh/harness-lsp: 66 tests pass. The harness-e2e typecheck passes.
  • Looped 10 times under other load: Rust lib, spawn_client, spawn_client_api and cli: 10 of 10. TS spawnClient.test.ts (37 tests): 10 of 10. No fake server was left after the loop.
  • Fix undone, test fails:
    • Second signal exits at once (std::process::exit): the second-signal test, 3 of 3.
    • Runtime leaked instead of shut down: the same test, 3 of 3.
    • No exit handler in the TS client: both new TS tests, 3 of 3.
    • si_code check undone, on macOS: the stop test, 3 of 3.

@avifenesh
avifenesh merged commit d2d0bf9 into main Nov 5, 2026
7 checks passed
@avifenesh
avifenesh deleted the fix/lsp-review-followups branch November 5, 2026 10:01
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.

1 participant