Skip to content

fix(eio-client): honor the requestTimeout option in Node.js and with fetch() - #5611

Open
mark1russell7 wants to merge 1 commit into
socketio:mainfrom
mark1russell7:fix/eio-client-request-timeout
Open

mark1russell7 wants to merge 1 commit into
socketio:mainfrom
mark1russell7:fix/eio-client-request-timeout

Conversation

@mark1russell7

Copy link
Copy Markdown

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

The requestTimeout option is applied with xhr.timeout = requestTimeout. This works with the XMLHttpRequest object of a browser only: the xmlhttprequest-ssl package (the default polling transport in Node.js) has no timeout attribute, and the Fetch transport ignores the option. A request that never gets an answer leaves the socket in the opening (or open) state with no error.

New behavior

  • Fetch: when requestTimeout is set, each request gets the signal of an AbortController that is aborted after requestTimeout ms. The timer is cleared when the response headers are received (as xhr.timeout does not apply to the body read either). The rejection goes to the existing fetch read error / fetch write error path.
  • XHR: when requestTimeout is set and the XMLHttpRequest object has no ontimeout attribute (the case of xmlhttprequest-ssl), the request starts its own timer. When it fires, the request emits error (status 0, the same value as a browser timeout) and is cleaned up. The timer is cleared in _cleanup(), and it is unref'd when autoUnref is set.

In the XHR timer, xhr.abort() is not called: with xmlhttprequest-ssl 2.1.2 and later (the version that ~2.1.1 installs), aborting a pending request throws an uncaught TypeError: Cannot read properties of null (reading 'reusedSocket') (mjwwit/node-XMLHttpRequest#37). The lock file of this repository has 2.1.1, which does not have this problem, so a test in this repository would not show it. The request is only detached, so its connection stays open until the server answers or closes it.

Other information (e.g. related issues)

Fixes #5610

The new test in test/node.js ("should abort the request after requestTimeout ms (polling)") uses an HTTP server that never answers, and a socket with requestTimeout: 100. It expects an error event (xhr poll error, or fetch read error with USE_FETCH=1) and a close event with the reason transport error.

Test results:

  • Node.js 24.21.0 on Windows 11, mocha 10.8.2, TypeScript 6.0.3, engine.io 6.6.11, xmlhttprequest-ssl 2.1.1 (the version of the lock file).
  • "Before" is the lib/ folder of main (545e984) with the tests of this branch.
  • The test hooks also build the browser bundle with rollup. This build was stubbed, because only the Node.js tests were run.
npx mocha --exit --require test/support/hooks.js --grep requestTimeout test/node.js
before after
default (xmlhttprequest-ssl) 0 passing, 1 failing (timeout of 2000 ms) 1 passing
USE_FETCH=1 0 passing, 1 failing (timeout of 2000 ms) 1 passing

Full Node.js suites of the branch: test/index.js 99 passing, 1 pending (USE_FETCH=1: 98 passing, 2 pending); main has 98 and 97 passing. test/webtransport.mjs: 10 passing, 1 failing (see below). prettier --check passes on the changed files.

The failure in test/webtransport.mjs also happens on main in this environment: "should send some binary data (server to client) (as ArrayBuffer)" (a timeout of 2000 ms). On main, "should allow to upgrade to WebTransport" also failed with a timeout in the same run.

The issue repro was also run with xmlhttprequest-ssl 2.1.2 (the version that ~2.1.1 installs): the socket emits error and close after the timeout, with no exception.

This PR was prepared with AI assistance (Claude). The tests named above were run before and after the change, with the results above.

🤖 Generated with Claude Code

…fetch()

The requestTimeout option was only applied through the "timeout"
attribute of the XMLHttpRequest object, which is not supported by the
"xmlhttprequest-ssl" package (the default polling implementation in
Node.js), and was ignored by the Fetch transport. A request that never
received a response stayed pending forever.

The XHR request now handles the timeout itself when the XMLHttpRequest
implementation does not support it, and the Fetch transport aborts the
request with an AbortController.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

[engine.io-client] the requestTimeout option has no effect in Node.js (xmlhttprequest-ssl) and with the fetch() transport

1 participant