Add AbortSignal to NodePgPreparedQuery #3954
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This signal is aborted from the NodePgSession object when an error event occurs on the underlying node-postgres client object. By default, there is no handler for such errors, meaning they will crash the Node.js process.
The AbortSignal acts as a "buffer" for the error event, storing it and turning it into a promise rejection that will then travel up the call stack, allowing the code that invoked Drizzle to decide what to do with it.
Closes #3908.
This is a minimal reproducer for the error case this addresses:
Before this patch, this results in an uncaught error terminating the process. Notably, the print in the
finally
block is never executed.With this patch, such errors are now caught by the session and rethrown on next attempted use. As you can see, the
finally
block executes as expected; the process doesn't just crash. Also, the stack trace, while not complete due to a v8 bug with custom thenables, now clearly leads into Drizzle code at the location that tried to use the connection next.Due to the aforementioned stack trace issue, I wasn't quite sure where to place the re-throw in
NodePgPreparedQuery.execute
. For now, I placed it at the start of the method, but I was wondering if it might be better to put it after thelogQuery
invocation or even inside thedrizzle.driver.execute
spans, to ensure observability tools can fill the gap.