io server disconnect right after connect: the auth guard ran after the connection was accepted
The guard ran and closed the socket, but the acceptance packet had already gone out. The tests saw it run, while it protected nothing.
io server disconnectAlso searched as: "my WebSocket guard rejects correctly, but the client still connects"
In short
- Symptom
- The server reports no error, and no unit test is red. A client with no session cookie receives
connect, gets a socket identifier and appears in the namespace registry, then is closed on the next event-loop tick. The guard runs and rejects the client, but after the fact. - Real cause
- The guard read the session in the handler of the
connectionevent. In generation 4 of the real-time library, the namespace sends the acceptance packet to the client before emitting that event. The rejection therefore landed on a connection that had already been accepted. - Fix
- Move the rejection into a namespace middleware, which runs before acceptance: a
next(err)there prevents any acceptance packet and returns a connection error to the client. Put it on the adapter, the authentication boundary, and not in one gateway, because the three gateways share the same namespace. - Prevention
- Put the guard at the first point in the lifecycle where rejecting still costs nothing. The test that watches it must fail if the guard stops running, and not only if it becomes permissive.
- Updated
- 2026-10-14
- Read time
- 11 min
- Access
- open
The answer, in three lines
An authentication guard protected the real-time connections of a web application. It read the
session attached to the handshake (the exchange that opens the connection), did not find it, and
closed the socket. It was wired to the server's connection event.
In generation 4 of this library, the namespace sends the acceptance packet to the client before emitting that event. So the guard ran on a connection that had already been accepted, and closed it on the next event-loop tick.
The guard ran, but too late. No test of the guard itself can show this, because the guard was doing exactly what was expected of it.
The symptom
It is almost invisible, and it is its third form in this notebook.
On the server side, nothing: no exception, no warning log, no red unit test. The rejection method is called on every anonymous connection, it makes the right decision, and it closes the transport.
On the client side, an observer tracing the events of a connection without a cookie sees two things
go by, in this order: connect, then disconnect with the reason io server disconnect. The first
should never happen. The connection was given a socket identifier and a memory buffer, and the
socket sat in the namespace registry for one event-loop tick.
The wrong diagnosis
The agent refuted the hypothesis before changing a line, with three mechanical checks:
- the real-time module is indeed imported by the root module, on the same line on both branches compared;
- it does declare both gateways among its providers, on both branches;
- the three files involved are identical byte for byte between the two branches: the file comparison returns nothing at all.
So both branches behaved the same way, and the one I suspected of having closed the hole without knowing it had closed nothing. The agent did not call my hypothesis unlikely: it showed the empty output of the comparison.
What is actually happening
The order, inside the library
When a namespace accepts a socket, it does two things, in this order:
1. socket._onconnect(); // sends the acceptance packet to the client
2. this.emit('connection', socket); // fires the handler, hence the guard
Everything wired to connection runs at step 2. A guard placed there therefore always runs
after the client has been accepted. It can close the connection, but it cannot prevent it from
having happened.
This is not a defect of the library: connection is the event that announces an established
connection, and it keeps exactly that promise. The mistake was reading "the moment a connection
arrives" where one should have read "the moment a connection is made".
What the probe returned
Reading the code might have convinced, but it proved nothing. So a throwaway probe was written: a real server brought up by the framework, the project's real adapter, a real client of the library, no cookie, and simply a record of the events received over two and a half seconds.
["connect","disconnect:io server disconnect"]
That line turned a reading of the code into a finding. It was then reused as-is as the assertion of a regression test, which failed red with exactly the same value:
● … › sans session › ne reçoit JAMAIS `connect`
Expected value: not "connect"
Received array: ["connect", "disconnect:io server disconnect"]
The severity, without rounding it up
There was no data leak. The anonymous socket joined no room, no event was emitted to it, and a test checks this: an intruder does not receive a message meant for a member. On the other hand, the server accepted the connection before refusing it, and every anonymous try cost it a session identifier and a buffer.
I describe the severity without inflating it: a hole presented as worse than it is turns against whoever found it, the next time.
The fix
Step 1: Move the rejection before acceptance, rather than making it stricter
The library offers an entry point that runs before acceptance: the namespace middleware. A
next(err) placed there emits no acceptance packet. The client receives a connection error, with an
application code from the 4000 family, modelled on HTTP's 401.
The guard's logic did not change: the session is read the same way, and the criterion is the same. Only the moment in the lifecycle changed, and the whole failure lay there.
Step 2: Put the gate on the boundary, not in one of the three corridors
Three gateways shared the same namespace, each for a different stream of events. Only one had a connection handler. The other two benefited from its protection by design accident, which the header of one of them asserted without ever having proved it.
So the rejection was put on the adapter, which is the transport's authentication boundary, and extended to any namespace created later, so that a future gateway is not born open. The gateways' connection handlers stay in place, as a second line of defence, and they are the ones that make the user join their room.
The header's claim became a test assertion: an authenticated client does receive the event emitted by a different gateway from the one that authenticated it.
Step 3: Write the test that fails if the guard stops running
This request is what made the failure findable. An ordinary rejection test only checks how strict the guard is. This one must also fail the day the guard is no longer called at all.
It brings up a real server, a real adapter, a real client and a real session layer, and it checks
only one thing: the sequence of events received by a client with no session does not contain
connect. That assertion fails if the guard becomes permissive, if the original adapter comes
back, if the middleware is removed, or if the gateway disappears from the module graph.
A second test walks the real module graph and checks that the three gateways are reachable in it. It covers the failure I feared at the start: a gateway removed from the graph turns no test red, it simply ceases to exist.
Prevention
The rule goes well beyond real-time. An access check in an interceptor that runs after the response has been serialised, a quota check made after the resource has been reserved, a sensitive-field filter applied after caching: each time, the effect has already happened when the decision lands, and the code of the decision is flawless.
So ask another question than "is the guard correct?": what has already happened to the outside world when the guard makes its decision?
Why this matters with agents
This entry tells two things, the failure and then its refutation, and the second will serve you more.
First the failure. An agent writing a real-time gateway reads the documentation, finds "wire your
connection logic to connection", and does so. It gets a guard that works, green tests and a
review that passes. Nothing in what it produces shows the library's internal order, while the whole
failure comes from that order.
Then the refutation. I handed an agent a hypothesis that was stated, argued, and backed by a precedent from the same repository, and it took it apart with three mechanical checks before writing a single line of fix. That did not happen by chance: the prompt explicitly ranked proof that there was nothing to fix alongside a fix.
What this entry does not cover
The exact version of the library was not recorded. The trace only gives its major generation, and the project's manifest only sets a lower bound. The order described here is the one observed on this project's installation, in July 2026. It is not presented as true for every version of the 4.x branch.
No minimal reproduction was built outside the project. Everything written here comes from a setup of the application itself, not from a twenty-line throwaway repository a reader could clone. That is the limit of the proof: it comes from a single project, in its context.
The end-to-end test was never rerun after the fix. It is what triggered the investigation, but it lives on another branch and needs a full container stack. The fix is proven by tests that bring up a real server and a real client, not by the full journey.
Finally, resource consumption per anonymous attempt was not measured under load. It is described as a design fact (an acceptance reserves an identifier and a buffer), never as a figure.
Where this entry comes from
A night of audits on a personal repository, on 25 July 2026. Domain terms from the original application have been replaced with their structural equivalent: the mechanism is intact, and the business domain is not recognisable.
The original verdict is preserved word for word in five independent places in the trace: the agent's report, the night's summary, the implementation report, the commit message of the fix, and a handover note written seventeen days later that still sums it up in the same terms. The event sequence quoted twice in this entry is program output, copied identically from the throwaway probe and from the red test. The refutation of my hypothesis rests on a file comparison whose empty output the report quotes.
Together, these three supports (a preserved text, command output reproduced identically at two moments, and a fix proven red then green) allow the entry to be published without marking it "to be verified". What is not established is stated above, in plain words.
This entry is part of a notebook of 27 production failures hit between February and September 2026. Some are public and free: see the notebook.
The others are published as they are written, along with the modules and their exercises, as part of the paid product. In that product, the modules are in English; the exercises and the other entries are in French. Nothing on this page is a teaser for it.