Skip to content

fix: allow after commit callbacks to start a new unit of work - #49

Merged
lindyhopchris merged 1 commit into
developfrom
fix/48-after-commit-unit-of-work
Nov 19, 2026
Merged

lindyhopchris merged 1 commit into
developfrom
fix/48-after-commit-unit-of-work

Conversation

@lindyhopchris

Copy link
Copy Markdown
Member

Closes #48

Summary

UnitOfWorkManager now lets an after commit callback start (and commit) a new unit of work, e.g. by dispatching a command that runs in a unit of work. Before this change it threw Not expecting unit of work manager to start a unit of work within an existing one., because the committed unit of work was still marked active while its after commit callbacks ran.

Approach: a FIFO queue, not the "execute immediately" design in the issue

After commit callbacks now run as a single FIFO queue, drained by the outermost call to execute():

  • An after commit callback registered while the queue is draining is added to the end of the queue. It is not run immediately. This keeps the existing order, so it_executes_additional_commit_callbacks is unchanged.
  • A unit of work started from an after commit callback runs its own before commit callbacks as normal. Its after commit callbacks are added to the end of the same queue, so they run after the callback that started it has returned.
  • If one of those nested after commit callbacks fails, the failure is collected with the other after commit failures and thrown by the outermost execute(). It is not thrown by the nested execute(), so the nested call still returns its result.
  • Starting a unit of work while another is still executing (i.e. before it has committed) is still rejected.

Compared with the design in the issue, this avoids running callbacks inside other callbacks, never makes afterCommit() hold on to exceptions for its caller, and avoids the reset pitfall described in the issue.

Other changes

  • Exception message: calling beforeCommit() from an after commit callback, outside a new unit of work, now throws Cannot queue a before commit callback when not executing a unit of work. The previous message said the unit of work had been committed.
  • FakeUnitOfWork:
    • It has a new executions property.
    • Each sequence entry now records which execution it belongs to, in the format execute:<execution>/<event>:<attempt>. The manager retries by calling the unit of work once per attempt, and previously every attempt made through the manager appeared as attempt:1.
    • Consumers who assert on sequence will need to update their tests. This is recorded under ### Changed in the changelog.
  • Docs: a new "After Commit Queue" section explains the FIFO behaviour, and warns that after commit callbacks must never rely on the order they run in or know about other callbacks. The Testing section shows the fake used with the manager.

Tests

  • UnitOfWorkManagerTest: adds the scenarios from the issue, adjusted for FIFO. Covers queuing while draining, nested units of work and their retries, failures inside nested units of work, guards, and resetting state.
  • New tests/Integration/Testing/FakeUnitOfWorkManagerTest: runs the real manager over FakeUnitOfWork. Checks the fake is inactive when after commit callbacks run, and that executions, attempts, commits, rollbacks and sequence record nested units of work correctly.
  • FakeUnitOfWorkTest: updated to the new sequence format, with tests for counting executions.

composer all passes locally.

🤖 Generated with Claude Code

The unit of work manager now executes after commit callbacks as a single
FIFO queue, drained by the outermost call to execute(). An after commit
callback can start (and commit) a new unit of work, as the previous unit
of work has committed. The new unit of work's after commit callbacks are
added to the end of the queue, rather than resetting or draining the
queue that is already in progress.

Starting a unit of work while another is executing is still rejected.

The fake unit of work now counts executions, and records which execution
each entry in its sequence belongs to. As the manager handles re-attempts
by executing the unit of work once per attempt, this distinguishes each
attempt made by the manager.

Closes #48

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lindyhopchris lindyhopchris self-assigned this Nov 19, 2026
@lindyhopchris
lindyhopchris added this pull request to stack #51 November 19, 2026 10:46
@lindyhopchris
lindyhopchris merged commit 2512ec0 into develop Nov 19, 2026
2 checks passed
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.

Allow after commit callbacks to start a new unit of work

1 participant