Repository navigation
Description
Summary
UnitOfWorkManager should allow an after commit callback to start (and commit) a new unit of work.
Once the unit of work has committed, its consistency boundary is closed. Starting a separate unit of work at that point is a legitimate pattern, e.g. a synchronous follow-on use case or cross-module orchestration: it is the synchronous equivalent of a queued handler that runs after commit. Starting a unit of work within an active transaction must still be rejected.
Current behaviour
After the unit of work commits, execute() sets committed = true and runs the after commit callbacks, but active is still true until reset() in the finally block. So if an after commit callback calls execute() (directly, or by dispatching a command through ExecuteInUnitOfWork), it throws:
Not expecting unit of work manager to start a unit of work within an existing one.
Pitfall: relaxing the guard is not enough
If the guard were just relaxed, the nested execute() would call reset() in its finally block. That clears the outer manager's afterCommit queue while it is still draining, so every callback queued after the one that opened the nested unit of work would be silently skipped.
Proposed behaviour
- Hand off and reset before draining. After a successful commit, take the after commit callbacks, reset the manager's state, then execute the taken callbacks. A callback can then start a new unit of work, with its own before and after commit callbacks, without affecting the outer queue.
- Keep draining behaviour via a draining flag. While after commit callbacks are being executed,
afterCommit()must remain allowed (as it is today). Track this with adrainingflag: a callback registered viaafterCommit()while draining, and not inside a nested unit of work, is executed immediately, since the unit of work has already committed.- If an immediately executed callback throws, its exception should be collected with the other after commit failures, so remaining callbacks still execute (same semantics as
executeAfterCommit()today: rethrow one, or wrap several inAfterCommitCallbacksFailed). - If a nested unit of work is started while draining,
afterCommit()calls inside it belong to the nested unit of work and execute when it commits.
- If an immediately executed callback throws, its exception should be collected with the other after commit failures, so remaining callbacks still execute (same semantics as
- Keep existing guards:
execute()inside an active (uncommitted) unit of work still throws.beforeCommit()from an after commit callback still throws ("Cannot queue a before commit callback as unit of work has been committed."), unless it is inside a nested unit of work, where it applies to that unit of work.
Sketch:
try {
$result = $this->retry($callback, $attempts);
} catch (Throwable $ex) {
$this->reset();
throw $ex;
}
$callbacks = $this->afterCommit;
$this->reset();
$this->drain($callbacks); // sets draining; afterCommit() while draining executes immediately
return $result;Ordering change to note
it_executes_additional_commit_callbacks currently expects a callback registered during draining to be appended to the end of the queue. With "execute immediately", it runs before the remaining queued callbacks, so that test's expected sequence needs updating to the new semantics.
Test cases to add
In UnitOfWorkManagerTest:
- An after commit callback can start a new unit of work; it commits, and its result is returned to the callback.
- After commit callbacks queued after the one that starts a nested unit of work are still executed (regression for the reset pitfall).
- A nested unit of work's own before and after commit callbacks run as part of the nested unit of work, not the outer one.
-
afterCommit()called while draining (outside a nested unit of work) executes immediately. - An immediately executed callback that throws does not prevent remaining callbacks executing, and the failure is reported like other after commit failures.
- A nested unit of work that fails (throws) does not prevent remaining outer callbacks executing.
-
execute()within an active unit of work still throws. -
beforeCommit()from an after commit callback (outside a nested unit of work) still throws. - The manager is fully reset afterwards, so a subsequent
execute()works.
The same scenarios should also be covered for the fake (FakeUnitOfWorkTest, running a real UnitOfWorkManager over FakeUnitOfWork), so the fake is verified to behave the same way as a real unit of work. In particular, that it is inactive by the time after commit callbacks run, so a nested unit of work can start, and that attempts, commits and sequence record the nested unit of work correctly.
Activity
lindyhopchris commentedon Nov 19, 2026
Implemented in #49, but with a different approach from the one proposed above.
Rather than running callbacks registered while draining immediately, after commit callbacks now run as a single FIFO queue, drained by the outermost call to
execute():execute(). The nestedexecute()still returns its result.This is a better solution than the proposal:
afterCommit()never has to hold on to exceptions for its caller.it_executes_additional_commit_callbacksdid not need changing.As noted in the docs, after commit callbacks should never rely on the order they run in, or on knowledge of other callbacks.
fix: allow after commit callbacks to start a new unit of work