Skip to content

fix: clean up abandoned connection attempts - #245

Merged
electrohyun merged 2 commits into
mainfrom
fix/abandoned-connection-attempts
Aug 12, 2026
Merged

electrohyun merged 2 commits into
mainfrom
fix/abandoned-connection-attempts

Conversation

@electrohyun

Copy link
Copy Markdown
Owner

Summary

Connection middleware receives a server socket before that socket is admitted, and it may join rooms while deciding whether to call next() or next(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 in adapter.rooms and adapter.sids. A client-side disconnect() was also a no-op until connected became true, so a middleware callback retained after cancellation could still complete the old pairing, enter the ready queue, fire connection, and connect the client.

This introduces an explicit connection-attempt lifecycle owned by the client. Each pairing moves from pending to exactly one terminal state: cancelled, rejected, or connected. A second connect() cannot create another pairing while the current attempt is still pending. Calling disconnect() before admission cancels that attempt without inventing a client disconnect, server disconnect, or connect_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 fire connection.

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.

Observation Socket.IO 4.7.5 and 4.8.3
middleware joins a temporary room, then rejects the client receives connect_error; the temporary room and sid entry are removed
rejected attempt reaches connection never
client disconnects while middleware is pending no client disconnect and no connect_error are emitted
old middleware callback completes after cancellation has propagated the abandoned attempt is not admitted
the same client starts a fresh attempt only the fresh server socket reaches connection and supplies the client's id
abandoned attempt membership its sid and temporary room are absent after the fresh connection completes

The 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 one connection, 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 live socket.rooms set, keep the socket out of the namespace roster, and disable later joins. The same membership primitive now serves connected-socket teardown after disconnecting, 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 connection fires, nextConnection() and the client id both identify the fresh socket, the abandoned sid and room are gone, and cancellation emits neither disconnect nor connect_error.

docs/conformance.md is regenerated from the green real and mock runs and links both cases. The abandoned-connection-attempt entry is removed from docs/differences.md section 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

  • Tests added or updated
  • Behavior verified against real socket.io

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@electrohyun, you've reached your PR review limit, so we couldn't start this review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3abfdd07-cdd2-4491-94b6-86e88259686d

📥 Commits

Reviewing files that changed from the base of the PR and between 5889db1 and 5082763.

📒 Files selected for processing (5)
  • docs/conformance.md
  • docs/differences.md
  • src/contract.ts
  • src/middleware.test.ts
  • src/mock-server.ts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added bug Something isn't working 📏 l Pull request changes 200–999 lines labels Aug 12, 2026
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 64.70588% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mock-server.ts 64.70% 6 Missing and 12 partials ⚠️

📢 Thoughts on this report? Let us know!

@electrohyun
electrohyun merged commit 015c2b3 into main Aug 12, 2026
14 checks passed
@electrohyun
electrohyun deleted the fix/abandoned-connection-attempts branch August 12, 2026 05:30
@electrohyun electrohyun self-assigned this Aug 12, 2026
@github-project-automation github-project-automation Bot moved this to Done in smocket Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working 📏 l Pull request changes 200–999 lines

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix: clean up abandoned connection attempts

1 participant