Skip to content

Allow after commit callbacks to start a new unit of work #48

Closed

Description

@lindyhopchris

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

  1. 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.
  2. 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 a draining flag: a callback registered via afterCommit() 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 in AfterCommitCallbacksFailed).
    • 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.
  3. 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

  1. lindyhopchris commented on Nov 19, 2026

    @lindyhopchris
    MemberAuthor

    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():

    • An after commit callback registered while draining is added to the end of the queue.
    • 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.
    • Failures from those nested callbacks are collected with the other after commit failures and thrown by the outermost execute(). The nested execute() still returns its result.

    This is a better solution than the proposal:

    • No callback runs inside another callback.
    • afterCommit() never has to hold on to exceptions for its caller.
    • A nested unit of work can never reset or drain the outer queue, which avoids the reset pitfall described above.
    • The existing order of callbacks registered while draining is kept, so it_executes_additional_commit_callbacks did 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.

  2. added a commit that references this issue on Nov 19, 2026
    2512ec0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions