Fix #19: server-lifecycle & state-lock hardening#25
Merged
Conversation
- SessionEnd teardown is now bounded: teardownServerSession takes a lock-acquire
timeout and returns {skipped, reason:"lock-timeout", diagnostic} on
contention; the SessionEnd hook passes a 3s server-lock + 500ms state-lock
budget, logs the diagnostic, and skips clearing state on timeout so it can't be
killed mid-teardown by the hook host. ensureServer also fails safely when a
bounded acquisition times out.
- State lock adds async APIs (withStateLockAsync / updateStateAsync) that yield
the event loop via awaitable polling instead of Atomics.wait. The async
companion paths (enqueue/spawn bookkeeping, terminal-index sync, cancel read,
in-flight cancellation) and the SessionEnd job cleanup now use them. The
synchronous lock keeps stale-takeover recovery (no premature default timeout;
lockAcquireTimeoutMs is opt-in).
- EPERM is treated as "unconfirmed" (not alive) for lease liveness, so a reused
foreign pid can no longer hold server teardown hostage for the 6h TTL; the TTL
still covers legitimate long sessions.
- server.json is written atomically (temp-file + rename), reusing the state
writer.
npm test: 79 passed, 0 failed, 0 skipped (localhost tests included).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…sue-19-lifecycle # Conflicts: # tests/runtime.test.mjs
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.
Closes #19.
Server-lifecycle & state-lock robustness.
teardownServerSessionpreviously acquired the server lock with no acquisition timeout, so under contention it could exceed the ~5s SessionEnd hook budget and be killed mid-teardown. It now accepts a lock-acquire timeout and returns{skipped, reason:"lock-timeout", diagnostic}on contention; the hook passes a 3s server-lock + 500ms state-lock budget, logs the diagnostic, and skipsclearServerSessionon timeout.ensureServeralso fails safely when a bounded acquisition times out (found via an internal review pass).withStateLockAsync/updateStateAsyncthat poll with awaitablesleepinstead ofAtomics.wait, and migrated the async paths (SessionEnd job cleanup; enqueue/spawn bookkeeping, terminal-index sync, cancel read, in-flight cancellation in the companion) to them, so contention no longer freezes concurrent async work. The synchronous lock keeps its stale-takeover recovery —lockAcquireTimeoutMsis opt-in, with no premature default.processIsAlivenow reports EPERM as "unconfirmed" andisLeaseActiverequires a confirmed-alive owner, so a reused foreign pid can't hold teardown hostage for the 6h TTL. Plugin processes are same-user, so legitimate leases are unaffected.server.json(Low).saveServerSessionnow uses the atomic temp-file + rename writer.Verification:
npm test— 79 passed, 0 failed, 0 skipped (localhost tests included). Regression tests cover bounded teardown diagnostics, sync stale-lock recovery beyond the former deadline, async-lock event-loop yielding, EPERM lease expiry, and atomicserver.json.🤖 Generated with Claude Code