|
| 1 | +--TEST-- |
| 2 | +PHPC-913: Child process should not re-use mongoc_client_t objects from parent |
| 3 | +--SKIPIF-- |
| 4 | +<?php if (!function_exists('pcntl_fork')) { die('skip pcntl_fork() not available'); } ?> |
| 5 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); SLOW(); ?> |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 9 | + |
| 10 | +function logMyURI(MongoDB\Driver\Manager $manager) |
| 11 | +{ |
| 12 | + $command = new MongoDB\Driver\Command(['whatsmyuri' => 1]); |
| 13 | + $cursor = $manager->executeCommand(DATABASE_NAME, $command); |
| 14 | + $uri = $cursor->toArray()[0]->you; |
| 15 | + |
| 16 | + $bulk = new MongoDB\Driver\BulkWrite(); |
| 17 | + $bulk->insert(['pid' => getmypid(), 'uri' => $uri]); |
| 18 | + $manager->executeBulkWrite(NS, $bulk); |
| 19 | +} |
| 20 | + |
| 21 | +$manager = new MongoDB\Driver\Manager(STANDALONE); |
| 22 | +logMyURI($manager); |
| 23 | + |
| 24 | +$parentPid = getmypid(); |
| 25 | +$childPid = pcntl_fork(); |
| 26 | + |
| 27 | +if ($childPid === 0) { |
| 28 | + $manager = new MongoDB\Driver\Manager(STANDALONE); |
| 29 | + logMyURI($manager); |
| 30 | + |
| 31 | + /* Due to PHPC-912, we cannot allow the child process to terminate before |
| 32 | + * the parent is done using its client, lest it destroy the mongoc_client_t |
| 33 | + * object and shutdown its socket(s). Sleep for 250ms to allow the parent |
| 34 | + * time to query for our logged URI. */ |
| 35 | + usleep(250000); |
| 36 | + exit; |
| 37 | +} |
| 38 | + |
| 39 | +if ($childPid) { |
| 40 | + /* Sleep for 100ms to allow the child time to log its URI. Ideally, we would |
| 41 | + * wait for the child to finish, but PHPC-912 prevents us from doing so. */ |
| 42 | + usleep(100000); |
| 43 | + |
| 44 | + $cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([])); |
| 45 | + $results = $cursor->toArray(); |
| 46 | + |
| 47 | + printf("%d connections were logged\n", count($results)); |
| 48 | + printf("PIDs differ: %s\n", $results[0]->pid !== $results[1]->pid ? 'yes' : 'no'); |
| 49 | + printf("URIs differ: %s\n", $results[0]->uri !== $results[1]->uri ? 'yes' : 'no'); |
| 50 | + |
| 51 | + $waitPid = pcntl_waitpid($childPid, $status); |
| 52 | + |
| 53 | + if ($waitPid > 0) { |
| 54 | + printf("Parent(%d) waited for child(%d) to exit\n", $parentPid, $waitPid); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +?> |
| 59 | +===DONE=== |
| 60 | +<?php exit(0); ?> |
| 61 | +--EXPECTF-- |
| 62 | +2 connections were logged |
| 63 | +PIDs differ: yes |
| 64 | +URIs differ: yes |
| 65 | +Parent(%d) waited for child(%d) to exit |
| 66 | +===DONE=== |
0 commit comments