Repository navigation
Transactions across zones: postgres error zones, ZoneField, memory rollback (v1.36.0) - #213
Merged
Merged
Conversation
…llback (v1.36.0) - DBPostgreSQLAdapter: a cached statement run in the runTx callback's zone forwards its outcome to the caller's zone when they are different error zones (Dart doesn't deliver a Future's error across error zones: a failing statement left the caller waiting forever). - ZoneField.contextZone: a zone forked from a context zone, at any depth, is inside it. Code in a zone forked inside a transaction now sees it: its operations join the transaction instead of committing apart, and executeBlock joins it instead of opening a new one. - DBSQLMemoryAdapter: a rolled back transaction reverts only its own writes (an undo log per context), not the tables to their versions when it began, which also reverted concurrent transactions' commits. - Tests for every adapter: transactions across zones and error zones, nested blocks, sub-transactions, and stress cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #213 +/- ##
=======================================
Coverage 95.96% 95.97%
=======================================
Files 75 75
Lines 25964 26035 +71
=======================================
+ Hits 24917 24987 +70
- Misses 1047 1048 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…ssed bodies, log preview - payloadAsBytes: a String payload with a JSON MIME type is the JSON text (not encoded again, which quoted it); others encoded with the library's Json encoder (entities, DateTime, toJson). Documents the copy per call. - APIServer: reads a chunked body with no Content-Type; passes a compressed body that isn't decompressed as bytes (octet-stream), not as garbage text; the JSON error log gives the body length before trimming and doesn't cut a surrogate pair. - Docs: _resolvePayloadBytes (shared buffer), _runStatement (who calls it). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ry.revertVersions (v1.36.0) - DBSQLMemoryAdapter: each context records the MapHistory versions its writes create (and their IDs); a rollback reverts only those. Replaces the undo log, which restored an entry's value from before the transaction's first write and so lost a later write of another transaction to the same entry. - DBObjectMemoryAdapter: the same for a canceled transaction, instead of rolling the tables back to their versions when it began (which reverted concurrent transactions' commits). - Writes go through _putTableEntry / _removeTableEntry, which record only versions a write actually created. - map_history: ^1.0.8. - Tests: concurrent writes to the same entity/object with one transaction aborted, in both orders and many concurrent rounds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…D sequence, fixes (v1.36.0) - Memory adapters (SQL and object): a transaction is consolidated only once its commit is final (resolveTransactionResult), not in its closer, which runs before the commit can still fail: the cancel then found its writes forgotten (SQL) or already consolidated (object: revertVersions threw). - Memory adapters: the consolidation queue is emptied after consolidating (it grew for the life of the process). - DBSQLMemoryAdapter: a rollback no longer resets the next-ID counter (as a sequence): an aborted insert's ID isn't given again. - New Transaction.runDetached / ZoneField.runDetached: work inside a transaction that must not join it (fire-and-forget, e.g. an audit log), now that forked zones join it. CHANGELOG warns about that change. - APIServer: Content-Encoding in any case; an empty chunked body is no body. - Docs: payloadAsBytes returns a binary payload as is (not a copy); _inAnotherErrorZone's users. - Tests for each, and the commit test pins the context disposed on commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
What
A follow-up to #212 (still 1.36.0, not released yet). Stress-testing transactions across zones found three bugs, two of them older than #212.
DBPostgreSQLAdapter: an error lost across error zones. APIRequest.payloadBytes: the request body exactly as received (v1.36.0) #212 runs a cached statement in the driver'srunTxcallback zone (thepostgres3.7 deadlock fix). When the caller is in another error zone (each API call has its own), Dart doesn't deliver thatFuture's error to it. A failing statement (stale0A000, a constraint) then left the caller waiting forever. The outcome is now forwarded to aCompleterof the caller's zone, asTransaction._runInZonealready does.ZoneField: a zone forked inside a transaction didn't see it (pre-existing).contextZoneonly matched the context zone itself. Code in arunZoned/runZonedGuardedinside a transaction had two problems:executeBlockopened a new transaction instead of joining the running one.contextZonenow walks the zone's ancestors (an identitySet), aszoneValuesare inherited.Memory adapters: an aborted transaction reverted concurrent commits (pre-existing). This affected both
DBSQLMemoryAdapterandDBObjectMemoryAdapter. A rollback restored the tables to their versions from when the transaction began, which also reverted what concurrent transactions committed meanwhile. With 12 concurrent transactions, half aborted, 1 of 18 committed writes survived.MapHistoryversions its writes create, along with their IDs. A rollback reverts only those, withMapHistory.revertVersionsfrom map_history 1.0.8 (revertVersions: revert only some operations; consolidate/rollback fixes (v1.0.8) gmpassos/map_history#2)._putTableEntry/_removeTableEntry. These record only versions a write actually created: a remove of an absent ID creates none, and readingversionthere would capture another transaction's write.Review of #212 (as merged)
payloadAsBytes: aStringJSON payload was encoded twice.APIRequest.post(payload: '{"a":1}', payloadMimeType: 'application/json')gave"{\"a\":1}". AStringis now taken as the JSON text. Other values are encoded with the library'sJsonencoder (entities,DateTime,toJson), notdart:convert's, which fell back totoString(not JSON).Content-Typewas not read (pre-existing). There was nopayloadnorpayloadBytes, so a webhook signature couldn't be verified. It is now read astext/plain.application/octet-stream).payloadAsBytesmakes a copy on each call;payloadBytes;_resolvePayloadBytesand_runStatementsay who uses them.Review of this PR
revertVersionscould throw for versions already consolidated.resolveTransactionResult, and the closer does nothing. An open transaction's versions are therefore never consolidated.runZonedGuardedinside a transaction: such work is reverted on abort, and it can fail the commit if it's added late.Transaction.runDetached(on top ofZoneField.runDetached) runs it apart, in its own transaction, as before.Content-Encodingwas case-sensitive when decompressing (pre-existing):GZIPthrew "UnknownContent-Encoding".null).payloadAsBytesdocs now say a binary payload is not a copy: it's the same buffer aspayloadBytes.Each fix has a test:
Transaction.runDetachedworks on every adapter;ZoneField.runDetached;Content-Encodingin any case, and an empty chunked body.Putting back the old closer consolidation, or the counter reset, makes these tests fail.
Behavior changes
decompressPayloadoff: bytes, not text (above).Transaction().executestarted there is refused ("Already executing a Transaction"), as it already was in the transaction's own zone, instead of running a separate transaction.Tests
runAdapterTestsruns these for every adapter (memory, directory, SQLite, MySQL, PostgreSQL):0A000error. It hangs without the fix.ZoneField: forked zones at any depth, the nearest context zone, async continuations, and disposal.The test helper now uses
executeOrErrorand rethrows.executereturnsnullwhen aborted, which would also hide a failedexpectinside the block.Local results:
-x docker -x concurrency)ensure_build🤖 Generated with Claude Code