Skip to content

fix(request): propagate connection errors while reading the response body - #1860

Open
Arestout wants to merge 1 commit into
jsforce:mainfrom
Arestout:fix/stream-error-mid-body
Open

Arestout wants to merge 1 commit into
jsforce:mainfrom
Arestout:fix/stream-error-mid-body

Conversation

@Arestout

@Arestout Arestout commented Nov 2, 2026 •

Copy link
Copy Markdown

Fixes #1852

Outcome

  • If the connection drops after the response headers arrive (while the body is still streaming), conn.query(), search(), limits(), request() and other calls now reject with the socket error (TypeError: terminated, cause UND_ERR_SOCKET).
  • Before this fix the error was raised as an uncaughtException, which crashes Node processes that have no handler, and the returned promise never settled.
  • Successful responses behave the same as before.

Design

  • src/request.ts: Readable.fromWeb(res.body).pipe(output) ignored errors from the source stream, because .pipe() does not forward them. The source stream now has an error listener that calls output.destroy(err). output is the readable half of the request duplex, and that duplex already forwards its errors (DuplexifiedStream in src/util/stream.ts). So the error reaches the duplex error event once, and Transport already rejects on that event.
  • src/request-helper.ts: the response handler catches the rejection from readAll(). That rejection is the same error the duplex has already emitted, so catching it only stops a second, unhandled rejection.
  • I didn't use stream.pipeline(). It would also destroy output with the error, but it requires a callback, and that callback would either report the error a second time or be an empty no-op.

Verification

  • Added two tests to test/http-api.test.ts. They are node-only and run against a real local node:http server that sends headers and part of the body, then drops the connection:
    • the conn.query() promise rejects with terminated / UND_ERR_SOCKET, and jest reports no uncaught exception or unhandled rejection;
    • a raw request() stream emits error and does not emit end.
  • Both tests fail on main (unhandled terminated error plus a 3 s timeout) and pass with the fix. Each of the two source changes is needed for them to pass.
  • Ran the reproduction script from the issue:
    • before the fix: uncaughtException: terminated, and query() was still pending after 5 s;
    • after the fix: query() rejected: terminated | cause: UND_ERR_SOCKET;
    • the happy path still resolves with the same body.
  • Ran on Node 24:
    • eslint src test: clean;
    • tsc -p test: clean;
    • jest test/http-api.test.ts: 58/58 passed;
    • offline suites (query-pagination mock, soql-builder, jwt-util, body-size, cache): all passed.
  • Not run: the E2E jest suites and the karma browser tests, which need a Salesforce org. The browser transport is not changed.

Known limitation (not changed here)

conn.request(...).stream() uses createLazyStream (src/util/stream.ts), which connects its streams with .pipe(). So after a mid-body drop that stream gets no error event, although the promise now rejects. Errors that happen before the headers arrive already behave this way today. Fixing it belongs in a separate change.

…body

When the connection drops after the response headers were received, the error from the response body stream was not forwarded (`.pipe()` ignores source errors). It surfaced as an uncaughtException and the request promise never settled.

Destroy the output stream with the source error so it reaches the request duplex (and the transport promise rejects), and handle the resulting readAll() rejection in the response handler.

Fixes jsforce#1852
@Arestout
Arestout force-pushed the fix/stream-error-mid-body branch from e04e553 to 423a257 Compare November 2, 2026 16:15
@Arestout
Arestout marked this pull request as ready for review November 2, 2026 16:15
@yauheni-liauchenia-nrich

Copy link
Copy Markdown

This also fixes Bulk API v2 query result downloads.

I tested the PR against a local server that sends part of a bulk2.query results page and then drops the connection, reading the stream via record / error / end events:

  • 3.10.23: uncaught terminated (UND_ERR_SOCKET), and the query never settles
  • this PR: the query rejects with terminated, no uncaught exception

The new tests and the http-api and query-pagination.mock suites pass (61/61).

Could a maintainer take a look? Thanks

This branch has not been deployed

No deployments
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.

Connection dropped mid-response-body causes uncaughtException and the request never settles

2 participants