Skip to content

fix(transactions): commit() no longer raises after the transaction was applied - #130

Merged
kalaspuff merged 2 commits into
issues-stack/4-unique-guardsfrom
issues-stack/5-commit-and-batch-writer
Oct 31, 2026
Merged

kalaspuff merged 2 commits into
issues-stack/4-unique-guardsfrom
issues-stack/5-commit-and-batch-writer

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What and why

The two issues of #83:

  • commit() could raise after DynamoDB applied the transaction. Instances updated in a transaction are refreshed by evaluating the update on the item as it was loaded. For an unversioned item that another writer changed since, the evaluation could fail where DynamoDB's did not: a list_append to a list the instance read as missing but another writer had stored. commit() then raised the ValidationError that normally means "not applied", and a caller who retried applied the change twice. Now, when the update does not apply to the loaded item, the instance is refreshed from the item as stored now (one consistent read). If that read fails or the item is gone, the instance forgets its stored copy, as if it had not been loaded. In both cases commit() returns normally and after_update hooks run. Versioned models are unaffected: their version condition guarantees the loaded item.
  • A BatchWriter block that raises: __aexit__ does not flush when the block raises, also when the exception comes from a failed automatic flush inside await batch.put(). The writes are not dropped: they stay queued in the writer. I kept that behaviour, which matches transactions (a block that raises writes nothing), and documented it in the module and class docstrings, the writing guide and AGENT_GUIDE: len(batch) shows what is left, and await batch.flush() writes it.

Verified with integration tests on the in-memory backend and MiniStack: a transaction appending to a list another writer stored since the read refreshes the instance to [1, 2], and one whose item is deleted right after the commit, or whose refresh read fails, returns with an unloaded instance; all fail without the fix. A fault-injection test shows that a block ending with a failed automatic flush, or with its own exception, keeps its writes for a later flush(). make check passes.

Fixes #83

Checklist

  • Tests cover the change (in-memory backend and MiniStack; batch writer faults on the memory backend)
  • Docs updated (transactions and writing guides, architecture, AGENT_GUIDE, batch docstrings, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No

Worth confirming

  • Decision: a batch writer block that raises keeps not writing its queue (documented as intended, like transactions) rather than flushing on the way out; the queued writes stay in the writer for an explicit flush().
  • Decision: when the refresh after a commit cannot read the item, the instance becomes "not loaded" instead of keeping a stored copy dynamoxy knows is outdated, so a later save() of it writes like one of a new instance.

fctl session


Note

Low risk. It changes only what happens after a transaction was already applied, and only when refreshing an updated instance fails, plus documentation and tests for the batch writer.

What it does. A transaction refreshes the instances it updated by running the update on the item as it was loaded. For an unversioned item that another writer changed since, that could fail, so commit() raised after DynamoDB had applied the transaction. Now such an instance is refreshed from the item as stored now, or forgets its stored copy if that read fails or the item is gone, and commit() returns normally. The pull request also documents and tests that a batch_writer() block that raises writes nothing more and keeps its queue for flush(); the batch writer code itself is unchanged.

Instance forgets its stored copy. When the fallback read fails or the item is gone, the updated instance becomes not loaded, so a later save() of it writes like one of a new instance.

fctl review's first look at 6fee03a · 9 files, +155 −8 · Review agent settings

…s applied

Updated instances were refreshed by evaluating the update on the item as
loaded; for an unversioned item another writer changed since, that could
fail, and commit() raised a ValidationError after DynamoDB had applied
the transaction. Such instances are now refreshed from the item as
stored now, or forget their stored copy when it cannot be read.

What a batch_writer() block that raises does is now documented and
tested: it writes nothing more and keeps what is queued for flush().

Fixes #83

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-review

fctl-review Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Partly addressed as of 6fee03a. Later commits fixed 1 of the 2 issues this review found.


fctl reviewed 7d434ea and found 2 issues.

The fix does what it says: a failed local evaluation no longer makes commit() raise after the transaction was applied, and the batch writer docs match what __aexit__ does. Only a documentation mismatch and an untested branch.

On lines the diff doesn't show:

  • Architecture doc still says no extra read · Low severity · docs/architecture.md line 121: The transaction section says updated instances are refreshed by evaluating the update locally, "no extra read". When the local evaluation fails, _refreshed_after_commit now does a consistent GetItem, so this is no longer true; AGENTS.md asks for docs/architecture.md to follow structural changes to a pipeline.

Reviewed by fctl · Review agent settings

Comment thread python/dynamoxy/repository.py Resolved
Also say in the architecture doc that a refresh after a commit reads the
item when the update does not apply to the item as loaded.

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Architecture doc finding: fixed in 6fee03a. The transaction section of docs/architecture.md now says that updated instances are refreshed without an extra read, except when the update does not apply to the item as loaded: then the item is read as stored now, and if that read fails or finds nothing the instance forgets its stored copy, without making commit() raise.

@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Stack of 10 pull requests, merged in this order with "Squash and merge":

  1. fix(models): numbers of union fields render as stored, and literals of plain enum members write #126 fix(models): numbers of union fields render as stored, and literals of plain enum members write
  2. fix(client): calls keep their middleware chain and deliver all their events #127 fix(client): calls keep their middleware chain and deliver all their events
  3. fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work #128 fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work
  4. fix(unique)!: upserts that race with creates, unique floats written back, and collections refused #129 fix(unique)!: upserts that race with creates, unique floats written back, and collections refused
  5. fix(transactions): commit() no longer raises after the transaction was applied #130 fix(transactions): commit() no longer raises after the transaction was applied ← this pull request
  6. fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows #131 fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows
  7. fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB #132 fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB
  8. fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits #133 fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits
  9. fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active #134 fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active
  10. fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses #135 fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commit() can raise after a transaction was applied, and a failing BatchWriter block drops queued writes

1 participant