Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-1969

GridFS read stream emits last chunk after emitting close

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.3.2
    • Affects Version/s: 3.2.4
    • Component/s: None
    • Environment:
      - Using Mongoose elsewhere in the application, but this is via GridFSBucket directly
      - We are using TypeScript, although that should not have transpiled the driver

      When using GridFs.createReadStream, the stream always emits the final 'data' event after the 'close' event, which means for libraries that stop listening when close is emitted, the last chunk is always lost.

      Reproduction code:

      await new Promise(resolve => {
        const result: string[] = [];
      
        if (!stream.readable) {
          resolve(result);
        }
      
        stream.on('data', (data) => {
          console.log('Got chunk');
          result.push(data);
        });
      
        stream.on('end', () => {
          console.log('Ending');
          resolve(result);
        });
      
        stream.on('close', () => {
          console.log('Closing');
          resolve(result);
        });
      });
      

      Log

      ...
      Got chunk
      Got chunk
      Got chunk
      Got chunk
      Got chunk
      Got chunk
      Got chunk
      Closing
      Got chunk
      Ending
      

      Result from db.isMaster()

      {
      	"ismaster" : true,
      	"maxBsonObjectSize" : 16777216,
      	"maxMessageSizeBytes" : 48000000,
      	"maxWriteBatchSize" : 1000,
      	"localTime" : ISODate("2019-05-15T16:04:27.607Z"),
      	"maxWireVersion" : 5,
      	"minWireVersion" : 0,
      	"readOnly" : false,
      	"ok" : 1
      }
      

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            zhangyijiang Zhang Yijiang
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: