Skip to content

php7 + opcache + mongodb: Query and php array is getting changed randomly. #216

@cb-msales

Description

@cb-msales

We found some strange behaviour where queries sent to mongodb don't match what we intended to send.

After digging deeper we found that under load with multiple requests handeled concurrently by php-fpm, the system randomly starts changing the php array (!).
This only happens when the array is sent to the mongodb find operation (other operations not tested).

We have only seen it changing the array when it has been sent to mongodb. Not calling ->find() does not produce the error.

Also opcache need to be enabled for the file where the query array is defined. With opcache disabled it will not fail.

The array is changed in a way that 'static' contents seem to be removed. We don't know if any more is getting replaced or changed. For example the part '[10, 12]' is replaced with NULL in our test case.
Replacing one value with dynamic content eliminates the error too: repalce for example 12 with (int)$_GET['id'], call the test script with parameter test.php?id=10 and it will NOT fail.

When using json_decode(json_encode($test), true) before submitting the data to ->find it will NOT fail and not change any contents of $test.

The database and selection does not exist in our test case.

We have tested with
ext-mongodb 1.1.8
mongodb/mongodb 1.0.2
PHP 7.0.8-4

Below the test script, run with apache benchmark


require __DIR__ . '/../vendor/autoload.php';

$test = ['test' => ['$in' => [10, 12]]];

$client = new MongoDB\Client();
$client->selectCollection('a','b')->find($test);

file_put_contents('/tmp/mongodb-errors.log', (int)(serialize($test) == 'a:1:{s:4:"test";a:1:{s:3:"$in";a:2:{i:0;i:10;i:1;i:12;}}}'), FILE_APPEND);

Run ab test:
(optional) > sudo service php-fpm restart;

ab -c 40 -n 1000 http://your.domain.goes.here/test.php

Example result: 1 = success, array not changed, 0 = error, array has been changed. '[10, 12]' has been nulled.

11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000
00000000000000000000000100000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111
11100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000111111111111111000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000001111111```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions