Repository navigation
FIX REST writes crash the server when several documents of a request fail - #485
Merged
pubkey merged 3 commits intoNov 30, 2026
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015P6w6gSU5X3tPquwWNPFbs
Contributor
✅ Verify Test Reproduction: Tests FAILED without the fix (expected)This confirms the changed tests correctly reproduce the bug that the source changes fix. This workflow runs the changed tests without the source fix to verify they reproduce the bug. Show output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Bug
The REST
/set//patchhandler (onWriteErrorinsetDocuments) and the/deletehandler run the writes of a request withPromise.all. On a non-conflict write error, each failing write callsadapter.closeConnection(res, 500, ...).When two or more writes of one request fail, the second
closeConnectionruns after the response was already sent. For example, a client sends two documents that do not match the schema. The express adapter'scloseConnectionisasyncand callsresponse.set(...), which throwsERR_HTTP_HEADERS_SENT. That rejected promise is never handled. Node exits on unhandled rejections by default, so any client could crash the server process with one request.Fix
Both handlers send the error response only once per request, using an
errorResponseSentflag. The errors are still rethrown, and express handles the rejected route handler.Test
test/unit/endpoint-rest.test.ts: should answer with an error when several documents of a request fail to be written. It sends two invalid documents and expects a500, then checks that the server still answers a normal/setand/query. Without the fix the test process exits with the unhandledERR_HTTP_HEADERS_SENTrejection.Full unit suite: 108 passing.
npm run check-typespasses.An independent review reproduced the crash on master and confirmed the fix. It also checked every other
closeConnectioncall site in the REST and replication endpoints, and none of them can be called twice or after the response was sent.The review also suggested an optional hardening that is not in this PR. The express adapter's
closeConnectioncould return early whenresponse.headersSent, because the adapter type declares it asvoidand no caller handles its promise.Changelog:
orga/changelog/fix-rest-write-error-crash.mdGenerated by Claude Code