Skip to content

Commit ad8e187

Browse files
authored
PHPLIB-1313 Ensure the GridFS stream is saved when the script ends (#1197)
1 parent e03ac44 commit ad8e187

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
## 1.18.0 (unreleased)
55

66
* Add `addSubscriber` and `removeSubscriber` methods to the `Client` class to ease dependency injection configuration
7-
7+
* Fix GridFS stream closing when the PHP script ends

src/GridFS/StreamWrapper.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ class StreamWrapper
5353

5454
public function __destruct()
5555
{
56-
/* This destructor is a workaround for PHP trying to use the stream well
57-
* after all objects have been destructed. This can cause autoloading
58-
* issues and possibly segmentation faults during PHP shutdown. */
59-
$this->stream = null;
56+
/* Ensure the stream is closed so the last chunk is written. This is
57+
* necessary because PHP would close the stream after all objects have
58+
* been destructed. This can cause autoloading issues and possibly
59+
* segmentation faults during PHP shutdown. */
60+
$this->stream_close();
6061
}
6162

6263
/**

tests/GridFS/BucketFunctionalTest.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function array_merge;
2424
use function call_user_func;
2525
use function current;
26+
use function escapeshellarg;
2627
use function exec;
2728
use function fclose;
2829
use function fopen;
@@ -39,7 +40,7 @@
3940
use function strncasecmp;
4041
use function substr;
4142

42-
use const PHP_EOL;
43+
use const PHP_BINARY;
4344
use const PHP_OS;
4445

4546
/**
@@ -853,21 +854,33 @@ public function testDanglingOpenWritableStream(): void
853854
$this->markTestSkipped('Test does not apply to Windows');
854855
}
855856

856-
$path = __DIR__ . '/../../vendor/autoload.php';
857-
$command = <<<CMD
858-
php -r "require '$path'; \\\$stream = (new MongoDB\Client)->test->selectGridFSBucket()->openUploadStream('filename', ['disableMD5' => true]);" 2>&1
859-
CMD;
857+
$code = <<<'PHP'
858+
require '%s';
859+
$client = new \MongoDB\Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100');
860+
$database = $client->selectDatabase(getenv('MONGODB_DATABASE') ?: 'phplib_test');
861+
$gridfs = $database->selectGridFSBucket();
862+
$stream = $gridfs->openUploadStream('hello.txt', ['disableMD5' => true]);
863+
fwrite($stream, 'Hello MongoDB!');
864+
PHP;
860865

861866
@exec(
862-
$command,
867+
implode(' ', [
868+
PHP_BINARY,
869+
'-r',
870+
escapeshellarg(sprintf($code, __DIR__ . '/../../vendor/autoload.php')),
871+
'2>&1',
872+
]),
863873
$output,
864874
$return,
865875
);
866876

877+
$this->assertSame([], $output);
867878
$this->assertSame(0, $return);
868-
$output = implode(PHP_EOL, $output);
869879

870-
$this->assertSame('', $output);
880+
$fileDocument = $this->filesCollection->findOne(['filename' => 'hello.txt']);
881+
882+
$this->assertNotNull($fileDocument);
883+
$this->assertSame(14, $fileDocument->length);
871884
}
872885

873886
/**

0 commit comments

Comments
 (0)