-
Notifications
You must be signed in to change notification settings - Fork 209
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
foreach over ObjectId and Garbage Collection #1776
Comments
Best I can tell,
This ultimately calls When using I looked at I was able to reduce the reproduction of an empty $oid = new MongoDB\BSON\ObjectId;
var_dump($oid);
echo "Iterating ObjectId:\n";
foreach ($oid as $k => $v) {
printf("%s: %s\n", $k, $v);
}
printf("gc_collect_cycles: %d\n", gc_collect_cycles());
var_dump($oid);
echo "Iterating ObjectId:\n";
foreach ($oid as $k => $v) {
printf("%s: %s\n", $k, $v);
} Output resembles the following:
In my local testing, each I also observed that |
Created PHPC-2505 to track this. |
Bug Report
foreach over ObjectId changes behavior after circular reference collector.
Before GC foreach over ObjectId executes one loop iteration with $key="oid".
After GC foreach over ObjectId executes zero loop iterations.
Environment
PHP inside Docker on Windows 10.
Docker Image: php:8.2.27-apache
PHP: 8.2.27
MongoDB PHP Driver: 1.20.1
MongoDB PHP Library: 1.20.0
MongoDB: 8.0.0
mongodb/laravel-mongodb: 4.8.1
MongoDB: I run DB in separate Docker container locally and use in PHP only HOST and PORT of DB.
Test Script
I've used mongodb/laravel-mongodb to reproduce the bug but there is not so much Laravel specific and main issue in \MongoDB\BSON\ObjectId.
ObjectIdTestModel.php
Test.php
Test output:
If the bug is not repoduced you can increase number of created objects in line
for ($i = 0; $i < 200; $i++) {
Expected and Actual Behavior
Expected
$number1 === 1
$number2 === 1
Circular reference collector do not change script behavior.
Actual
$number1 === 1
$number2 === 0
Calling gc_disable() "fixes" the bug.
Preffered solution
Prohibit to iterate over \MongoDB\BSON\ObjectId especially by hidden public property "oid".
I have iterated it by mistake and prefer empty loop over object without public properties instead of strange behavior.
Debug Log
object_id_gc.zip
The text was updated successfully, but these errors were encountered: