You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Create a script that runs a closure using parallel\Runtime.
Create another script that runs a different closure using parallel\Runtime.
Observe that the closure from the first script is reused in the second script.
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
OS: Ubuntu 20.04
PHP version: 8.3.8
parallel version: 1.2.2
Server API: Apache 2.0 Handler (but happens with php-fpm as well)
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:
Hey @Skypewalker,
thanks for your bug report, I was able to reproduce this behaviour. If you like to help, could you try and see if you are able to replicate this behaviour in a single request or on CLI?
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 :
Fatal error: Uncaught TypeError: {closure}(): Argument #2 ($queue_channel) must be of type parallel\Channel, array given in /app/tutorial/example10/main.php:24
Stack trace:
#0 [internal function]: {closure}(Object(parallel\Channel), Array, Array)
#1 {main}
thrown in /app/tutorial/example10/main.php on line 24
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: