Skip to content

fix(engine.io-client): handle close packets across websocket and webtransport - #5644

Open
yxshee wants to merge 1 commit into
socketio:mainfrom
yxshee:fix/eio-client-websocket-close-packet
Open

yxshee wants to merge 1 commit into
socketio:mainfrom
yxshee:fix/eio-client-websocket-close-packet

Conversation

@yxshee

@yxshee yxshee commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

In engine.io-client, Polling#onData() explicitly handles incoming Engine.IO "close" packets ("1") by closing the transport with { description: "transport closed by the server" } instead of forwarding them as "packet" events. In contrast, WebSocket (via Transport#onData() -> Transport#onPacket()) and WebTransport (via Transport#onPacket()) forward "close" packets up to Socket#_onPacket(), which has no "close" case, leaving the transport and underlying connection open.

New behavior

  • Transport#onPacket() now handles packet.type === "close" by closing the underlying transport via this.doClose() and invoking this.onClose({ description: "transport closed by the server" }) when this.readyState !== "closed", matching Polling's close details without emitting a "packet" event. (Polling#onData() continues to intercept "close" packets directly so it does not trigger Polling#doClose(), which writes a close packet back to the server.)
  • BaseWS#doClose() guards if (this.ws) and clears onopen, onclose, onmessage, and onerror handlers before calling this.ws.close(), preventing the WebSocket's asynchronous onclose callback from invoking onClose() a second time.
  • WT guards its _transport.closed handlers and read loop with this.readyState !== "closed" so WebTransport stops reading and does not invoke onClose() a second time after closing on a "close" packet.

Other information (e.g. related issues)

Fixes #5286.

Added regression tests in packages/engine.io-client/test/node.js (covering server-sent "close" packets over WebSocket, Polling, and upgraded Polling -> WebSocket across ws, fetch, and built-in WebSocket) and packages/engine.io-client/test/webtransport.mjs (covering WebTransport), asserting close reason/details, closed states, no "close" packet emission, closure of the underlying server connection, and single transport.onClose() invocation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent behavior of close packet processing in websocket and polling transports

1 participant