I am using bugsnag in a symfony 5 project to keep track of exeptions. I also use symfony messenger with rabbit for heavy processing.
Everything works fine overall, except the fact that in some cases, inside a messenger middleware (RejectRedeliveredMessageMiddleware) an exception is thrown in some particular cases in order to trigger a message reque while avoiding a possible infinite loop (RejectRedeliveredMessageException).
The exception should be there, and the behavior is correct. For example if a process stopped unexpectedly (a hard reboot or a kill), the message is requed. The logic works well, autorecovery works perfectly.
The only problem is that bugsnag registers itself as a subscriber for all kernel events, treated or not (code below)
$listeners = [ KernelEvents::REQUEST => ['onKernelRequest', 256], KernelEvents::EXCEPTION => ['onKernelException', 128],
Normally exceptions are caught in our app before getting to the general event dispatcher and so they are not reported if they shouldn’t. But, because that is in the middleware, and an event is risen it gets reported even though is already treated by the autoretry mechanism and because of that it generates a lot of noise and fake positives.
What I would need to do is unsubscribe for a particular event, but since I can’t touch the 3rd party code (bugsnag and messenger) I can’t find a way to do it.
Is there a way to programatically remove an event from a subscriber?
Source: Symfony Questions