Repository navigation
Conversation
Livog
force-pushed
the
fix/bulk-operations-sequential-processing
branch
from
April 12, 2026 15:06
70a2a70 to
46d733b
Compare
Livog
force-pushed
the
fix/bulk-operations-sequential-processing
branch
from
April 12, 2026 17:15
46d733b to
069e731
Compare
Livog
force-pushed
the
fix/bulk-operations-sequential-processing
branch
from
April 12, 2026 17:37
069e731 to
400b74f
Compare
Livog
force-pushed
the
fix/bulk-operations-sequential-processing
branch
from
April 12, 2026 18:24
400b74f to
cddb16a
Compare
Contributor
Author
|
I no longer have the issue after upgraded to 3.83 |
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.
Summary
Fixes a race condition in bulk update/delete that causes silent data loss. Built on top of #16076 by @razmenaia (his PR fixes
delete.ts); this PR adds the same fix toupdate.tsand adds deterministic tests for both files in bothbulkOperationsSingleTransactionmodes.I do not want to take credit for @razmenaia's work — happy for someone to copy these
update.tschanges into #16076 and close this PR. I just want the issue resolved.Closes #16075.
The bug
docs.map(async ...)in bothupdate.tsanddelete.tseagerly starts every per-doc callback at once. The subsequentfor...of awaitonly controls the order results are collected, not when they execute. Every per-doc operation runs concurrently on the shared transaction connection, interleaving queries and silently dropping writes.This affects both modes:
bulkOperationsSingleTransaction: false): all per-doc operations share the outer transaction's connection — verified by the new tests.bulkOperationsSingleTransaction: true: each doc nominally gets its own transaction, but the antipattern still starts all callbacks concurrently — also verified by the new tests.The fix
Extract the per-doc body into a named
processDocfunction, iterate sequentially:Same pattern in both files. The per-doc transaction handling for
bulkOperationsSingleTransactioninsideprocessDocis preserved unchanged.unlinkTempFilesreorder inupdate.tsThe previous code awaited
unlinkTempFilesbetween defining the promise array and consuming it. Because.map(async ...)started the promises eagerly, any consumer readingreq.file.tempFilePathhad usually opened it already. With sequentialfor...of, the unlink happens before anyprocessDocruns, leaving consumers with deleted paths. Moved the unlink to after the per-doc loop. This matches the order already used byupdateByID.ts:235andcreate.ts:443.Performance trade-off
Bulk operations are now strictly sequential rather than
Promise.all-parallel. This is the only correct fix: the bug exists in both modes because all per-doc operations share the outer transaction's connection regardless ofbulkOperationsSingleTransaction. There is no safe parallel path while sharing a connection. The verified-failing tests in both modes confirm the conditionalPromise.allbranch was never safe to begin with.Tests
Razmenaia's #16076 ships a stress test (25 docs × 10 iterations) that does not reliably reproduce the bug on a fast local Postgres — operations can naturally serialize even when started concurrently.
This PR adds a deterministic test instead. A dedicated test-only collection
bulk-ops-sequentialhasbeforeChange/beforeDeletehooks that throw if invoked while another invocation on the same request is in flight, with a 10ms hold to widen the overlap window. Four tests cover the matrix:bulkOperationsSingleTransaction: trueVerified locally:
update.tsanddelete.tstomain: all 4 tests fail withbulk hook ran while another was in flight.