Repository navigation
feat: add server close lifecycle - #200
Conversation
📝 WalkthroughWalkthroughChangesThe PR adds Server close lifecycle
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Test
participant Server
participant Namespace
participant ServerSocket
participant ClientSocket
Test->>Server: close()
Server->>Namespace: close()
Namespace->>ServerSocket: closeFromServer()
ServerSocket->>ClientSocket: disconnect with transport close
Server-->>Test: resolve shared close promise
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/test-runner-integration.md`:
- Around line 125-128: Update the lifecycle explanation around the
beforeEach/afterEach flow to state that afterEach calls io.close(),
disconnecting clients and closing the old server, while beforeEach creates a new
Server with a new adapter. Remove claims that clients from the previous server
remain usable or that empty rooms result only from server replacement, and align
the wording with the behavior covered by server-close.test.ts.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: d117f7c9-33ea-47dc-8498-a9ce4545fbd4
📒 Files selected for processing (10)
docs/conformance.mddocs/decisions/0020-close-follows-socket-lifecycle.mddocs/decisions/README.mddocs/test-runner-integration.mdscripts/conformance-report.mjssrc/connect-url.test.tssrc/contract.tssrc/mock-server.tssrc/server-close.test.tssrc/setup-mock-server.ts
ce5fd23 to
d6b6ba5
Compare
d6b6ba5 to
4173d8b
Compare
Summary
new Server(url)registers itself in smocket's module-level origin registry, but there was no inverse operation. Constructing another server for the same origin did replace the registry entry, so new clients reached the replacement, but the old object and every lifecycle it had already started remained alive. Tests could create a fresh server inbeforeEach; they could not explicitly shut that server down inafterEach.This adds
io.close(callback?). It marks the server and its namespaces closed before teardown starts, closes every namespace socket, completes the server-side disconnect lifecycle, disconnects the paired clients, and removes the server from the origin registry. A connection started immediately before close is rejected instead of being admitted on the next tick after close has resolved. The returnedPromise<void>is shared by repeated calls, while the optional callback keeps the call shape accepted by socket.io 4.7 and 4.8.What socket.io actually does
The behavior was measured on a real server before the mock was changed. The first implementation pinned the 4.8 promise return, and the 4.7 CI job exposed that the supported versions differ there. The common lifecycle is now tested through the callback;
ServerContractcarries the commonvoid | Promise<void>return while smocket's concreteServer.close()keeps the 4.8 promise. The contract was also typechecked locally against the actual 4.7.5 packages. #201 tracks making that version-specific typecheck permanent in CI.disconnecting, thendisconnect, both withserver shutting downdisconnectwithtransport closeconnection; the client observesconnect_erroremitWithAcksocket has been disconnectedemitWithAckoperation has timed outvoidin 4.7,Promise<void>in 4.8ERR_SERVER_NOT_RUNNING; the 4.8 promise still resolvesOne measurement corrected the issue's original motivation. A faithful
close()does not disarm an acknowledgement timeout that was already armed. Real socket.io lets that timer fire after close, so cancelling it in smocket would invent a different lifecycle and would also require inventing how the abandoned callback or promise settles. The test-runner guide now says that a test which arms such a timeout must settle it before teardown or drive it with fake timers. ADR 0020 records that decision instead of leavingclose()described as a timer-reset API.Smocket-specific lifecycle
The origin registry has no socket.io counterpart. Leaving the closed server registered would let a later
connect(url)activate an object whose lifecycle had ended, so close removes its entry. The deletion is conditional on that server still owning the origin: closing an older object afternew Server(url)installed a replacement cannot unregister the replacement. With no replacement, a later connect follows the existing missing-server behavior and emitsconnect_error.Namespace and server-socket teardown share their first close promise, so concurrent or repeated shutdown paths do not fire the disconnect lifecycle twice. The closed state is visible at every asynchronous pairing boundary, so a delayed auth callback, middleware completion, or next-tick registration cannot revive the server after teardown. Closing a namespace also discards unclaimed connections. The mock test harness now awaits
server.close()inafterEach, which makes the documented fresh-server recipe close the previous clients and then create a new adapter for the next test.Tests and documentation
src/server-close.test.tsruns against both real socket.io and smocket. It covers the callback and repeated-close error, a connection started in the same turn as close, every-namespace teardown and reasons, both pending acknowledgement directions, and the armed timeout. The pending server acknowledgement uses close completion plus a microtask as its lifecycle marker; it does not prove non-settlement with a wall-clock timeout.src/connect-url.test.tsseparately pins the smocket-only registry rules: close returns the 4.8-style promise, a closed origin reports a missing server, and an old server cannot remove its replacement. The conformance report has a Server close area, and the lifecycle and test-runner consequences are documented in ADR 0020 and the integration guide.Related issue
Closes #193
Follow-up tooling guard: #201
Checklist