-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
cloned TraceableStack operates differently than StackMiddleware #51564
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
Comments
Hi @rela589n, Thanks for submitting this bug report. I tried out your test case and I can confirm the same bug is happening for Symfony version 6.3.4 and that the fix you suggested indeed makes the test case pass. I'm not sure whether this bug is necessary to fix since TraceableStack is marked as In terms of the potential fix, I would also suggest cloning $stopwatch as well as it may have the same issue: public function __clone(): void
{
$this->stack = clone $this->stack;
$this->stopwatch = clone $this->stopwatch;
} Here's my reproduction of the test case including the potential fix (which causes it to pass): krciga22@9c71a0e By the way, since Status: Reviewed |
@krciga22 the Stopwatch must not be cloned, otherwise the traced timing won't be registered in the profiler due to registering them in a different Stopwatch. |
@stof makes sense, I didn't realize that. |
Hi, @krciga22 ,
I'm currently working on project built with Symfony framework. It has its own micro-framework built with Messenger component inside primarily used for cross-layer communication. Basically this is just a command bus with some additional middlewares providing common features for our specific needs. The implementation of one more middleware required a source stack to be cloned. When running in prod mode everything worked fine, while running in dev-mode causes bug, since Current implementation uses following workaround for $closure = fn (): object => $this->stack = clone $this->stack;
$closure->call($stack); It would be great if this bug was fixed so that we won't have to write dirty workarounds. |
I see, definitely sounds like a plausible scenario that might occur when writing different middleware. I've worked on the fix for this based off of your suggested fix and will try to create a PR for it tonight. In case you or @stof have any suggestions before I submit a PR, here's my latest commits: 6.4...krciga22:symfony:issue-51564 One thing I'm not sure of though is the line with |
it should be inside the test at least, not at top-level |
@stof , good call I thought there was something else funny about that line. I've gone ahead and updated the test as well as refactored it to (I hope) more accurately test that the cloned |
…tack independently (krciga22) This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Messenger] Fix cloned TraceableStack not unstacking the stack independently | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #51564 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Fixed a bug with cloned `TraceableStack` not unstacking the stack independently from the original due to the __clone() method not yet being implemented. Clones of `StackMiddleware` currently unstack the stack independently, but not `TraceableStack`. Commits ------- b91bdc6 [Messenger] Fix cloned TraceableStack not unstacking the stack independently
Symfony version(s) affected
6.x
Description
The
StackMiddleware
has it's own state ($offset
field), which changes during the operation flow. Therefore, if at some point of time we clone this stack, we get a completely different instance, which may be safely used.The problem lies in the absence of
TraceableStack::__clone()
method .It is not possible to use cloned stack after main stack has been iterated over, since internally it references the same object.
See provided test for details.
How to reproduce
Below two tests are provided. First passess (
StackMiddleware
), second fails (TraceableStack
).Possible Solution
I guess adding
__clone
method toTraceableStack
would fix the issue:Additional Context
PHP version: 8.2.8
Symfony messenger version: 6.2.7
The text was updated successfully, but these errors were encountered: