File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,14 @@ class StreamWrapper
57
57
/** @var ReadableStream|WritableStream|null */
58
58
private $ stream ;
59
59
60
+ public function __destruct ()
61
+ {
62
+ /* This destructor is a workaround for PHP trying to use the stream well
63
+ * after all objects have been destructed. This can cause autoloading
64
+ * issues and possibly segmentation faults during PHP shutdown. */
65
+ $ this ->stream = null ;
66
+ }
67
+
60
68
/**
61
69
* Return the stream's file document.
62
70
*
@@ -88,6 +96,10 @@ public static function register($protocol = 'gridfs')
88
96
*/
89
97
public function stream_close ()
90
98
{
99
+ if (! $ this ->stream ) {
100
+ return ;
101
+ }
102
+
91
103
$ this ->stream ->close ();
92
104
}
93
105
Original file line number Diff line number Diff line change 18
18
use function array_merge ;
19
19
use function call_user_func ;
20
20
use function current ;
21
+ use function exec ;
21
22
use function fclose ;
22
23
use function fread ;
23
24
use function fwrite ;
24
25
use function hash_init ;
26
+ use function implode ;
25
27
use function is_callable ;
26
28
use function min ;
27
29
use function sprintf ;
28
30
use function str_repeat ;
29
31
use function stream_get_contents ;
30
32
use function strlen ;
33
+ use function strncasecmp ;
31
34
use function substr ;
35
+ use const PHP_EOL ;
36
+ use const PHP_OS ;
32
37
33
38
/**
34
39
* Functional tests for the Bucket class.
@@ -708,6 +713,29 @@ public function testExistingIndexIsReused()
708
713
$ this ->assertIndexNotExists ($ this ->chunksCollection ->getCollectionName (), 'files_id_1_n_1 ' );
709
714
}
710
715
716
+ public function testDanglingOpenWritableStream ()
717
+ {
718
+ if (! strncasecmp (PHP_OS , 'WIN ' , 3 )) {
719
+ $ this ->markTestSkipped ('Test does not apply to Windows ' );
720
+ }
721
+
722
+ $ path = __DIR__ . '/../../vendor/autoload.php ' ;
723
+ $ command = <<<CMD
724
+ php -r "require ' $ path'; \\ \$stream = (new MongoDB\Client)->test->selectGridFSBucket()->openUploadStream('filename', ['disableMD5' => true]);" 2>&1
725
+ CMD ;
726
+
727
+ @exec (
728
+ $ command ,
729
+ $ output ,
730
+ $ return
731
+ );
732
+
733
+ $ this ->assertSame (0 , $ return );
734
+ $ output = implode (PHP_EOL , $ output );
735
+
736
+ $ this ->assertSame ('' , $ output );
737
+ }
738
+
711
739
/**
712
740
* Asserts that a collection with the given name does not exist on the
713
741
* server.
You can’t perform that action at this time.
0 commit comments