Repository navigation
fix: clean up abandoned connection attempts - #245
Conversation
|
Warning Review limit reached
Next review available in: 21 minutes You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
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! |
Summary
Connection middleware receives a server socket before that socket is admitted, and it may join rooms while deciding whether to call
next()ornext(err). Smocket previously had no object representing the lifetime of that in-progress pairing. A rejected socket was never added to the namespace roster, but any room membership created by middleware remained inadapter.roomsandadapter.sids. A client-sidedisconnect()was also a no-op untilconnectedbecame true, so a middleware callback retained after cancellation could still complete the old pairing, enter the ready queue, fireconnection, and connect the client.This introduces an explicit connection-attempt lifecycle owned by the client. Each pairing moves from
pendingto exactly one terminal state:cancelled,rejected, orconnected. A secondconnect()cannot create another pairing while the current attempt is still pending. Callingdisconnect()before admission cancels that attempt without inventing a clientdisconnect, serverdisconnect, orconnect_error. Auth callbacks, middleware completions, and the deferred admission step all verify that they still belong to the current pending attempt before they can register a socket. Once an attempt is terminal, a late callback cannot put it in the roster or ready queue and cannot fireconnection.Socket.IO reference
The rejection and cancellation paths were measured independently against Socket.IO 4.7.5 and 4.8.3 before the mock changed. Both supported versions produced the same result.
connect_error; the temporary room and sid entry are removedconnectiondisconnectand noconnect_errorare emittedconnectionand supplies the client's idThe cancellation probe used the fresh attempt reaching middleware as the ordering marker. Only after that marker established that the disconnect had propagated were the abandoned callback and then the fresh callback released. The client observed one
connect, the server observed oneconnection, and both belonged to the fresh middleware socket. This pins the late-callback case without waiting for a timeout to prove that the abandoned attempt did not connect.Attempt cleanup and admission
An attempt records its middleware-visible server socket as soon as that socket is created. Rejection and pre-connect cancellation transition the attempt before cleanup, remove every room through the existing adapter
del()hook, delete the reverse sid entry, clear the livesocket.roomsset, keep the socket out of the namespace roster, and disable later joins. The same membership primitive now serves connected-socket teardown afterdisconnecting, so abandoned and connected sockets cannot drift into different cleanup rules. It is idempotent: cancellation may clean immediately, and the retained middleware callback may reach the same cleanup path later without restoring membership or emitting lifecycle events.The connected terminal state is also significant. Socket.IO permits middleware to call
next()more than once without smocket throwing. If a duplicate completion runs after the first one has admitted the socket, it is treated as a duplicate of the already connected attempt rather than as an abandoned attempt, so it cannot clean the live socket that the first completion just registered.Server close continues to use the same asynchronous boundaries. A close observed during auth, middleware, or deferred admission rejects the still-pending attempt once and cleans any membership the middleware created. A callback from an older cancelled attempt cannot affect a newer attempt because every check compares the attempt object as well as its state.
Tests and documentation
The existing rejected-connection conformance case now has middleware join a temporary room before rejecting. A later accepted connection is the marker that lets the test inspect the final roster and both adapter indexes without a timeout; only the accepted socket's id room remains.
A new dual-run case holds two middleware callbacks for the same client. It cancels the first attempt, starts a fresh one, waits until that fresh attempt reaches middleware, then releases the old and new callbacks in order. The case proves that exactly one
connectionfires,nextConnection()and the client id both identify the fresh socket, the abandoned sid and room are gone, and cancellation emits neitherdisconnectnorconnect_error.docs/conformance.mdis regenerated from the green real and mock runs and links both cases. The abandoned-connection-attempt entry is removed fromdocs/differences.mdsection C because #229 is no longer an outstanding accidental gap. The middleware contract now states that rejection and pre-admission cancellation remove middleware-created membership without reporting a disconnect for a socket that never connected.Related issue
Closes #229
Checklist