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

[10.x] Ensures connections only listen for their own events #46231

Closed
wants to merge 1 commit into from

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Feb 22, 2023

The Connection::listen() method currently listens to queries on any connection.

DB::connection('mysql')->listen(function () {
    dump('mysql query run');
});

DB::connection('sqlite')->listen(function () {
    dump('sqlite query run');
});

DB::connection('sqlite')->table('foo')->select('*');

// output:
// "mysql query run"
// "sqlite query run"

This is a bug and unexpected behaviour that is potentially problematic. It also impacts other framework behaviour that depends on adding listeners to the specific connection, e.g. the query duration handlers.

Comment on lines 968 to 972
$this->events?->listen(Events\QueryExecuted::class, function ($event) use ($callback) {
if ($event->connection === $this) {
$callback();
}
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an identity check. I felt this was better than checking the connection name.

If you change the driver instance, it feels like you would want to also remove the listeners.

@taylorotwell
Copy link
Member

I don't think we could change this on a patch release. Most likely people are already depending on this "bug" in the wild pretty heavily. The event contains the connection name so it is possible to check what connection you are listening on.

@timacdonald timacdonald deleted the connection branch April 13, 2023 00:45
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.

2 participants