-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
Closure Reuse Issue in parallel: Cached Closures from Previous Scripts are Reused #309
Comments
Hey @Skypewalker, |
Hey, I didn't manage to replicate this behavior in a single request. I tried several scenarios with two closures with the same OPCode signatures and everything worked fine. Until I requested another script that contains closure with the same OPCode as the first one. So, as far as I noticed this bug happens only between different scripts. |
Hi, I faced the same kind of issue two days ago with two closures in two different scripts with the same number of arguments but different signatures : the first one is expecting a Channel as second argument, the second one is expecting an array (of Channel) as second argument with a different argument name. In the first script // Launch controller in a parallel thread
$controller = \parallel\run(
function(Channel $controller_channel, Channel $queue_channel, array $config): array {
... In the second script // Launch controller in a parallel thread
$controller = \parallel\run(
function(Channel $controller_channel, array $queues_channels, array $config): array {
... When running the second script after the first one has run I receive a runtime error : TypeError because the second argument passed to the closure does not match the expected type. The error refers to the closure in the first script, although it was running the second script, which I found astounding. Here is the error message :
The complete code that fails can be found here : https://github.com/pielonet/concurrency/blob/main/tutorial/example42/main.php I hopefully found this bug report before becoming crazy... Please consider fixing this soon. I offer help for testing if needed. Kind regards, |
Hey there, |
Hi, |
That's good to hear, I'll try to fix it anyway 😉
In some cases it helps, especially when "reusing" code, or in cases like this, where the address in memory could be the same when OPcache is not enabled. I would not go as far as saying |
If I understand well the issue I faced was a bit different from the one first reported here (as I wasn't using opcache). |
Nah, I guess it is okay. I assume that @Skypewalker also hit this bug without OPcache enabled. |
Hi, I just saw that opchache.ini wasn't loaded at all and Zend OPcache section was missing entirely in phpinfo(). When I included opchache.ini into configuration (which has both caches enabled) everything worked fine. Thank you very much for your effort. |
@realFlowControl Thanks a lot for your support ! |
Description
When using the parallel extension, closures from previously executed scripts are being reused in new scripts, even when the closures are expected to be different. This appears to be due to the caching mechanism in
php_parallel_cache_closure
.Steps to Reproduce
parallel\Runtime
.parallel\Runtime
.Script 1:
Script 2:
Expected Behavior
If you execute script 1 the output should be:
task1:1
If you then execute script 2 the output should be:
task2:2
Actual Behavior
The closure from the first script is reused in the second script, leading to unexpected behavior.
script 1 echoes
task1:1
but script 2 echoes
task1:2
Environment
Additional Information
After inspecting the source code, it appears that the issue lies in the caching mechanism of the
php_parallel_cache_closure
function, which uses the opcode array to identify and cache closures. If two closures have identical opcode arrays, as in an example, they are treated as the same closure.Suggested Fix
Consider adding a mechanism to ensure closures are uniquely identified, possibly by including additional context or metadata in the cache key.
The text was updated successfully, but these errors were encountered: