File tree Expand file tree Collapse file tree 4 files changed +36
-5
lines changed Expand file tree Collapse file tree 4 files changed +36
-5
lines changed Original file line number Diff line number Diff line change 4
4
## 1.18.0 (unreleased)
5
5
6
6
* 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
Original file line number Diff line number Diff line change @@ -53,10 +53,11 @@ class StreamWrapper
53
53
54
54
public function __destruct ()
55
55
{
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 ();
60
61
}
61
62
62
63
/**
Original file line number Diff line number Diff line change 5
5
use MongoDB \BSON \Binary ;
6
6
use MongoDB \BSON \UTCDateTime ;
7
7
8
+ use function escapeshellarg ;
9
+ use function exec ;
8
10
use function fclose ;
9
11
use function feof ;
10
12
use function fread ;
11
13
use function fseek ;
12
14
use function fstat ;
13
15
use function fwrite ;
14
16
17
+ use const PHP_BINARY ;
15
18
use const SEEK_CUR ;
16
19
use const SEEK_END ;
17
20
use const SEEK_SET ;
@@ -204,4 +207,18 @@ public function testWritableStreamWrite(): void
204
207
205
208
$ this ->assertSame (6 , fwrite ($ stream , 'foobar ' ));
206
209
}
210
+
211
+ public function testAutocloseOnScriptEnd (): void
212
+ {
213
+ $ command = PHP_BINARY . ' ' . escapeshellarg (__DIR__ . '/scripts/stream-autoclose.php ' );
214
+ exec ($ command , $ output , $ exitCode );
215
+
216
+ $ this ->assertSame ([], $ output );
217
+ $ this ->assertSame (0 , $ exitCode );
218
+
219
+ $ fileDocument = $ this ->filesCollection ->findOne (['filename ' => 'hello.txt ' ]);
220
+
221
+ $ this ->assertNotNull ($ fileDocument );
222
+ $ this ->assertSame (14 , $ fileDocument ->length );
223
+ }
207
224
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use MongoDB \Client ;
4
+
5
+ require __DIR__ . '/../../../vendor/autoload.php ' ;
6
+
7
+ $ client = new Client (getenv ('MONGODB_URI ' ) ?: 'mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100 ' );
8
+ $ database = $ client ->selectDatabase (getenv ('MONGODB_DATABASE ' ) ?: 'phplib_test ' );
9
+ $ gridfs = $ database ->selectGridFSBucket ();
10
+ $ stream = $ gridfs ->openUploadStream ('hello.txt ' );
11
+ fwrite ($ stream , 'Hello MongoDB! ' );
12
+
13
+ // The WriteStream must be closed and the file inserted automatically
You can’t perform that action at this time.
0 commit comments