Repository navigation
Description
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, causeUND_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
fix(request): propagate connection errors while reading the response …