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
- 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.
Script 1:
<?php
use \parallel\{Runtime};
$task1 = function($param){
return "task1:$param";
};
$rt = new Runtime();
$future = $rt->run($task1, array(1));
$futureValue = $future->value();
echo($futureValue);
?>
Script 2:
<?php
use \parallel\{Runtime};
$task2 = function($param){
return "task2:$param";
};
$rt = new Runtime();
$future = $rt->run($task2, array(2));
$futureValue = $future->value();
echo($futureValue);
?>
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
- 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.
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:1If you then execute script 2 the output should be:
task2:2Actual Behavior
The closure from the first script is reused in the second script, leading to unexpected behavior.
script 1 echoes
task1:1but script 2 echoes
task1:2Environment
Additional Information
After inspecting the source code, it appears that the issue lies in the caching mechanism of the
php_parallel_cache_closurefunction, 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.