Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShouldDispatchAfterCommit does not work after child transactions are committed #49057

Closed
hansnn opened this issue Nov 20, 2023 · 3 comments · Fixed by #49093
Closed

ShouldDispatchAfterCommit does not work after child transactions are committed #49057

hansnn opened this issue Nov 20, 2023 · 3 comments · Fixed by #49093

Comments

@hansnn
Copy link
Contributor

hansnn commented Nov 20, 2023

Laravel Version

10.32.1

PHP Version

8.2

Description

When a child transaction is committed inside of a parent transaction, any events with the ShouldDispatchAfterCommit interface that are dispatched after the child transaction has committed, but before the parent transaction is committed, will be dispatched immediately.

The problem is related to that the dispatcher adds the event listeners using DatabaseTransactionManager@addCallback, and that this method only adds the callback if it has any pendingTransactions - which are flushed whenever a transaction is committed.

Steps To Reproduce

I wrote a failing test case in ShouldDispatchAfterCommitEventTest.php that illustrates the problem:

public function testItOnlyDispatchesEventsAfterTheRootTransactionIsCommitted()
{
    Event::listen(ShouldDispatchAfterCommitTestEvent::class, ShouldDispatchAfterCommitListener::class);

    DB::transaction(function () {
        // Begin and commit a child transaction. This line causes the test to fail.
        DB::transaction(function () {});

        Event::dispatch(new ShouldDispatchAfterCommitTestEvent);

        $this->assertFalse(ShouldDispatchAfterCommitTestEvent::$ran);
    });

    $this->assertTrue(ShouldDispatchAfterCommitTestEvent::$ran);
}
Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@cosmastech
Copy link
Contributor

@hansnn do you have the failing test case in a repo? Or the body of those two classes at the very least?

@hansnn
Copy link
Contributor Author

hansnn commented Nov 21, 2023

@cosmastech The test can be copy-pasted as-is into the tests/Integration/Events/ShouldDispatchAfterCommitEventTest.php file of this repository.

I should've made that more clear in the description. I guess I assumed familiarity with the PR where this feature, and the test, were added.

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

Successfully merging a pull request may close this issue.

3 participants