Skip to content

fix: fail the request when a value handler throws, and keep its connection - #1980

Merged
dhensby merged 2 commits into
tediousjs:masterfrom
dhensby:claude/wizardly-jennings-63f616
Nov 27, 2026
Merged

dhensby merged 2 commits into
tediousjs:masterfrom
dhensby:claude/wizardly-jennings-63f616

Conversation

@dhensby

@dhensby dhensby commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

A value handler (sql.valueHandler) runs inside the driver's row event, with no try/catch. So a handler that throws gets out into the driver, and what happens next depends on the driver and its version:

driver before after
tedious 19.2.2 (allowed by ^19.2.2 || ^20.0.0) The throw escapes from tedious's token parser. With no uncaughtException listener, the process crashes (Unhandled 'error' event). With a listener, the request never settles, it keeps its pooled connection, and pool.close() hangs. The request fails with a RequestError (EREQUEST), and the connection goes back to the pool.
tedious 20.4.3 tedious treats the throw as a token parse failure and closes the connection. The request fails with a RequestError, but its code is the thrown error's own code if it has one (for example ERR_INVALID_ARG_TYPE). A thrown Symbol crashes the process in the RequestError constructor. Same as tedious 19. The connection is kept.
msnodesqlv8 (code reading of 2.7.0, 5.6.0 and 6.2.2; no ODBC driver locally) The column event is emitted from the promise chain that fetches rows. Its .catch ends the query with the throw as if it were a driver error, so the rest of the results are lost. On a resume() the rows are emitted synchronously, so the throw gets out to whatever resumed the stream, and the query stays stopped part way through its rows. Same as tedious.

What changes

  • Both drivers catch what a handler throws. The request then fails the way it does on a server error that does not end the batch:
    • It fails with a RequestError that has the code EREQUEST. Its originalError is what the handler threw. A thrown value that is not an Error is put into an Error first. A thrown value that cannot be read, such as a revoked Proxy, becomes the cause of an Error that says so, so nothing is thrown back into the driver.
    • It calls no more handlers, and it builds and keeps no more rows. This includes a later FOR JSON or FOR XML result, which it does not keep or parse.
    • The query runs to its end on the server, and the connection goes back to the pool. This is how EJSON and a destroyed toReadableStream() already behave.
    • In stream mode, it emits the error as an error event, emits no more row events, and then emits done.
    • It keeps its place among the server's errors, so precedingErrors stays in order. On tedious, it is added to the error list only after tedious's own errors are matched up with the ones the connection emitted, so that matching still works.
  • thrownMessage moves from lib/msnodesqlv8/request.js to lib/utils.js. The error is built by one helper, valueHandlerError, in lib/shared.js next to the valueHandler map.
  • README, "Result value manipulation":
    • It now says that handlers are given null for SQL NULL. Both drivers already do this, so the example (value) => value + 1 turned a NULL int into 1. The example now checks for null.
    • It now says what happens when a handler throws.

Two things change for users that the operator may want to confirm:

  • A handler that throws no longer stops the rest of the batch on the server. Before, the batch stopped because tedious 20 closed the connection or msnodesqlv8 freed the statement. A caller who wants the batch stopped can call request.cancel() from the error listener.
  • On tedious 20, err.code is now always EREQUEST. Before, it could be the thrown error's own code. msnodesqlv8 already forced EREQUEST.

Live results (Azure SQL Edge, local)

sql.valueHandler.set(sql.TYPES.Int, () => { throw new Error('boom') }), a pool with max: 1, SELECT 1 AS a, 2 AS b:

code tedious promise / callback / arrayRowMode / execute / stream / toReadableStream connection after
master 19.2.2 process crashes; with an uncaughtException listener, the request hangs with { available: 0, borrowed: 1 } leaked
master 20.4.3 rejects EREQUEST closed and replaced (the context_info and connect_time of the session are gone)
this branch 19.2.2 rejects EREQUEST / stream emits error then done same session, reused
this branch 20.4.3 rejects EREQUEST / stream emits error then done same session, reused

I also checked these on both tedious versions, with the same results on each:

  • a throw on row 3 of 5000 in stream mode (rows 1 and 2 emitted, then error, then done)
  • a server error after the throw and a server error before it (precedingErrors in order)
  • batch() with an output parameter
  • a throw inside a transaction (the transaction carries on and commits)
  • a FOR JSON result after the throw (not emitted)
  • pipe() (the destination is destroyed with the error)

Tests

  • Unit, tedious: a fake connection plays the row events and records anything thrown back into it. 17 tests cover query, arrayRowMode, callback, execute, batch, stream (query and execute), toReadableStream, non-Error throws, values that cannot be read, a coded error, a JSON result after the throw (query and execute), and the order of errors. All 17 fail on master.
  • Unit, msnodesqlv8: 11 tests in the msnodesqlv8 query events harness: the same cases, and a batch-ending error after the throw. All 11 fail on master.
  • Integration (test/common/tests.js, wired into both driver suites):
    • value handler that throws in six modes, on a one-connection pool. Each test checks that the session's CONTEXT_INFO survives, which shows that the connection was kept. On master all six fail: on tedious 20 because the connection is replaced, and on tedious 19 with an uncaught error.
    • value handler is given null for a NULL value.
  • Locally, npm test passes (537 unit tests). npm run test-tedious gives 222 passing and 5 pending on tedious 19.2.2 and on 20.4.3. Three tests fail on both: user defined types and the two spatial/hierarchyid bulk loads. They fail the same way on master, because Azure SQL Edge has no CLR. The msnodesqlv8 integration tests run only on Windows CI.

Not in this PR

  • tedious does not apply handlers to output parameters or the return value of query() and execute(), but it does for the output parameters of batch(), which it reads through a select. msnodesqlv8 reads both through a select in every case, so its handlers probably do apply to them.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Error normalization can itself throw on revoked proxies or objects that cannot be safely inspected, escaping the driver's catch block and leaving requests unsettled.

4 open issues
What changed in this PR

This PR makes value-handler exceptions request-level failures across both SQL Server drivers, allowing requests to finish and return their connections to the pool rather than letting exceptions escape into the driver.

Changes:

  • Catch and normalize handler exceptions as EREQUEST, suppress later rows, and preserve error ordering.
  • Add unit and integration coverage for failure propagation, streaming, SQL NULL, and connection reuse.
  • Share thrown-value formatting and document null-safe handlers and failure behavior.
File Description
test/​tedious/​tedious.js Registers NULL and throwing-handler integration cases.
test/​msnodesqlv8/​msnodesqlv8.js Registers equivalent integration cases for the native driver.
test/​common/​unit.js Adds driver-event regression tests for handler failures.
test/​common/​tests.js Adds shared NULL, failure, and connection-reuse tests.
README.md Documents NULL handling and handler-error behavior.
lib/​utils.js Extracts reusable thrown-value formatting.
lib/​tedious/​request.js Catches conversion failures and tracks ordered request errors.
lib/​shared.js Adds shared value-handler error normalization.
lib/​msnodesqlv8/​request.js Catches conversion failures and suppresses later row emission.

Review effort: Max

Comment thread lib/shared.js Resolved
Comment thread lib/msnodesqlv8/request.js Resolved
Comment thread lib/tedious/request.js Resolved
Comment thread lib/tedious/request.js Resolved
Both drivers call a value handler for a SQL NULL value and give it
null. The README did not say so, and its example, `value + 1`, turned
a NULL int into 1. The example now checks for null, and an
integration test pins the behaviour on both drivers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dhensby
dhensby force-pushed the claude/wizardly-jennings-63f616 branch from 5995627 to e5ed47c Compare November 27, 2026 14:00
…ction

A value handler runs inside the driver's row event with nothing to catch
what it throws, so the throw got out into the driver:

- tedious 19 let it escape from its token parser. The process crashed,
  or, with an uncaughtException listener, the request never settled and
  kept its pooled connection, so the pool never finished closing.
- tedious 20 took it for a token parse failure and closed the
  connection. A thrown Symbol crashed the process in the RequestError
  constructor, and an Error with a code of its own kept that code.
- msnodesqlv8 emits each value from the promise chain that fetches the
  rows, which ended the query with the throw as a driver error and lost
  the rest of the results. On a resume it emits them synchronously, so
  the throw got out to whatever resumed the stream and left the query
  stopped part way through its rows.

Both drivers now catch it. The request fails the way it does on a server
error that does not end the batch: with a RequestError that has the code
EREQUEST and keeps what was thrown, made an Error if it is not one, as
its originalError. A value that cannot be read, such as a revoked Proxy,
becomes the cause of an Error that says so, so nothing is thrown back
into the driver. The request calls no more handlers, and builds and
keeps no more rows, including any part of a later FOR JSON or FOR XML
result. The query still runs to its end and the connection goes back to
the pool. A stream emits the error, no more rows, and then done. On
tedious the error is added to the request's errors only after tedious's
own errors have been matched up with the ones the connection emitted,
at the place it came.

thrownMessage moves to lib/utils.js, and the error is built by one
helper next to the valueHandler map. The README says what happens when
a handler throws.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dhensby
dhensby force-pushed the claude/wizardly-jennings-63f616 branch from e5ed47c to f58ecfc Compare November 27, 2026 14:03
@dhensby
dhensby marked this pull request as ready for review November 27, 2026 14:03
@dhensby
dhensby merged commit 9406f60 into tediousjs:master Nov 27, 2026
93 of 94 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 12.8.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants