Uploaded image for project: 'PHP Driver: Library'
  1. PHP Driver: Library
  2. PHPLIB-345

GridFS stream close handler may error during shutdown

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.7.1
    • Affects Version/s: 1.1.0
    • Component/s: None
    • None

      This came up during a code review for PHPLIB-233:

      <?php
      
      require 'vendor/autoload.php';
      
      $bucket = (new MongoDB\Client)->test->selectGridFSBucket();
      
      $stream = $bucket->openUploadStream('filename');
      

      The following produces the following error when run as a CLI script:

      Warning: hash_final(): supplied resource is not a valid Hash Context resource in /home/jmikola/workspace/mongodb/phplib/src/GridFS/WritableStream.php on line 222
      
      Fatal error: Uncaught Error: Class 'MongoDB\Operation\FindOne' not found in /home/jmikola/workspace/mongodb/phplib/src/Collection.php:594
      Stack trace:
      #0 /home/jmikola/workspace/mongodb/phplib/src/GridFS/CollectionWrapper.php(333): MongoDB\Collection->findOne(Array, Array)
      #1 /home/jmikola/workspace/mongodb/phplib/src/GridFS/CollectionWrapper.php(317): MongoDB\GridFS\CollectionWrapper->isFilesCollectionEmpty()
      #2 /home/jmikola/workspace/mongodb/phplib/src/GridFS/CollectionWrapper.php(254): MongoDB\GridFS\CollectionWrapper->ensureIndexes()
      #3 /home/jmikola/workspace/mongodb/phplib/src/GridFS/WritableStream.php(229): MongoDB\GridFS\CollectionWrapper->insertFile(Array)
      #4 /home/jmikola/workspace/mongodb/phplib/src/GridFS/WritableStream.php(137): MongoDB\GridFS\WritableStream->fileCollectionInsert()
      #5 /home/jmikola/workspace/mongodb/phplib/src/GridFS/StreamWrapper.php(73): MongoDB\GridFS\WritableStream->close()
      #6 [internal function]: MongoDB\GridFS\StreamWrapper->stream_close()
      #7 {main}
        thrown in /home/jmikola/workspace/mongodb/phplib/src/Collection.php on line 594
      

      Running this script through PHP 7.2 produces the following errors:

      • The MD5 hash context has already been freed before we finalize it
      • The MongoDB\Operation\FindOne class cannot be autoloaded at this point

      On PHP 5.6, only the autoloading error appears (with a reference to spl_autoload() being unable to load the class).

      I believe the commonality in both of these errors is that we're running logic after the request has ended. As the last reference to the $stream is removed, its close handler is invoked. We should investigate whether there is a workaround for this short of advising users to manually close any streams defined in a global context.

            Assignee:
            andreas.braun@mongodb.com Andreas Braun
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: