Skip to content

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

Open

Description

@Arestout

Description

If the connection breaks while the response body is being read (after headers were received), the error is never propagated to the caller:

  • the error surfaces as an uncaughtException (TypeError: terminated, cause UND_ERR_SOCKET: other side closed), which crashes Node processes that don't install a handler, and
  • the promise returned by conn.query() / search() / limits() / request() stays pending forever.

Reproduction

jsforce 3.10.29 and 3.11.1, Node 24.14.1.

const http = require('node:http');
const jsforce = require('jsforce');

process.on('uncaughtException', (err) => {
  console.log('uncaughtException:', err.message, '| cause:', err.cause?.code);
});

const server = http.createServer((_req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.write('{"totalSize":1,');
  setTimeout(() => res.destroy(), 50); // connection drops mid-body
});

server.listen(0, '127.0.0.1', async () => {
  const conn = new jsforce.Connection({
    instanceUrl: `http://127.0.0.1:${server.address().port}`,
    accessToken: 'token',
  });
  const timer = setTimeout(() => {
    console.log('query() still pending after 5s');
    process.exit(1);
  }, 5000);
  try {
    await conn.query('SELECT Id FROM Account');
    console.log('query() resolved');
  } catch (err) {
    console.log('query() rejected:', err.message);
  } finally {
    clearTimeout(timer);
    server.close();
  }
});

Output:

uncaughtException: terminated | cause: UND_ERR_SOCKET
query() still pending after 5s

Expected: query() rejected: terminated and no uncaught exception.

Cause

src/request.ts (startFetchRequest) pipes the web stream without an error listener:

Readable.fromWeb(res.body).pipe(output);

.pipe() does not forward source errors, so the error event on the Readable has no listener (→ uncaught) and output is never ended. As a result, readAll(duplex) in src/request-helper.ts never resolves, complete is never emitted, and the transport promise stays pending.

Suggested fix

Forward the source error to the request stream, e.g.:

Readable.fromWeb(res.body)
  .on('error', (err) => {
    output.destroy();
    emitter.emit('error', err);
  })
  .pipe(output);

and guard readAll() in createHttpRequestHandlerStreams so the rejection from the destroyed stream isn't unhandled either (the transport promise has already been rejected through the error event). Using stream.pipeline() instead of .pipe() would work as well.

We're currently working around this with a custom Transport subclass that sets conn._transport / conn.oauth2._transport. Happy to open a PR with the fix and a test if that helps.

Activity

  1. added a commit that references this issue on Nov 2, 2026
    423a257
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions