Skip to content

[DoctrineBridge] Throw an exception in case of circular dependencies in event listeners #48955

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

Closed
wants to merge 1 commit into from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jan 12, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #46168
License MIT
Doc PR n/a

When a doctrine event listener or subscriber calls the event manager in its constructor, the event manager has an incomplete initialization status. This can cause errors that are difficult to figure (for event listener) or invisible (for event subscribers).

This PR introduces EventSubscriberCircularException as a new feature to help debugging circular dependencies. This is not a bugfix since there is no broken behavior.

@carsonbot carsonbot changed the title Throw an exception in case of circular dependencies in event listeners [DoctrineBridge] Throw an exception in case of circular dependencies in event listeners Jan 12, 2023
@carsonbot carsonbot added this to the 5.4 milestone Jan 12, 2023
@GromNaN GromNaN force-pushed the doctrine-evm-circular branch from 76dd0c4 to 5394f83 Compare January 12, 2023 03:00
@GromNaN GromNaN requested review from derrabus and xabbuh January 12, 2023 03:04
}

private function initializeSubscribers()
{
$this->initializedSubscribers = true;
if ($this->initializingSubscribers) {
throw new EventSubscriberCircularException('Circular dependency detected during initialization of event subscribers.');
Copy link
Member

Choose a reason for hiding this comment

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

Could there be a way to know which one triggers the loop?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can catch the EventSubscriberCircularException in the loop to rethrow an other exception with more context, but I didn't want to make code more complex for an info already provided by the stack trace.
image

Copy link
Member Author

Choose a reason for hiding this comment

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

@symfony/mergers Do you think that context details should be added to the exception message, when this information can be found in the stacktrace?

That is, add a try-catch to throw a new exception with more information, pushing the original exception as $previousException.

Copy link
Member Author

@GromNaN GromNaN Jan 29, 2023

Choose a reason for hiding this comment

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

I added more context to the message.

For a subscriber:
image

For a listener:
image

@GromNaN GromNaN force-pushed the doctrine-evm-circular branch from 5394f83 to 6421d03 Compare January 13, 2023 09:05
@GromNaN GromNaN force-pushed the doctrine-evm-circular branch from 12e5402 to 963f7bb Compare January 29, 2023 00:01
@GromNaN GromNaN force-pushed the doctrine-evm-circular branch 2 times, most recently from b160737 to 335631b Compare January 29, 2023 00:06
@GromNaN GromNaN force-pushed the doctrine-evm-circular branch from 335631b to f1277c1 Compare July 13, 2023 14:49
@GromNaN GromNaN changed the base branch from 5.4 to 6.4 July 13, 2023 14:50
@GromNaN GromNaN force-pushed the doctrine-evm-circular branch from f1277c1 to 60d8628 Compare July 13, 2023 15:24
@GromNaN
Copy link
Member Author

GromNaN commented Jul 13, 2023

I rebased this PR on 6.4. Adding a new exception is a feature.

The detection of circular dependency on even subscriber have been removed because this feature is deprecated.

@GromNaN GromNaN force-pushed the doctrine-evm-circular branch from 60d8628 to 082d997 Compare July 13, 2023 15:27
@nicolas-grekas nicolas-grekas modified the milestones: 5.4, 6.4 Jul 16, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
*/
class EventListenerCircularException extends \RuntimeException
{
public function setMessage(string $message): self
Copy link
Member

Choose a reason for hiding this comment

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

I suggest marking that method as @internal

@OskarStark
Copy link
Contributor

As we are in feature freeze this must target 7.1

@OskarStark OskarStark modified the milestones: 6.4, 7.1 Nov 15, 2023
@Nyholm Nyholm added Feature and removed Bug labels Dec 22, 2023
@Nyholm
Copy link
Member

Nyholm commented Dec 22, 2023

@GromNaN do you still have interest to work on this PR?

@GromNaN
Copy link
Member Author

GromNaN commented Dec 22, 2023

I missed last comment from @stof. I can update the PR if we think this feature is useful.

In fact, I would prefer getting rid of the doctrine event listeners and leverage symfony system in Doctrine. But that's an other story.

@GromNaN
Copy link
Member Author

GromNaN commented Feb 3, 2024

Closing as the problem doesn't seem to occur all that often to justify dedicated exception to help debugging.

@GromNaN GromNaN closed this Feb 3, 2024
@GromNaN GromNaN deleted the doctrine-evm-circular branch February 3, 2024 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DoctrineBridge] weird error with mongodb services
7 participants