thread-stream icon indicating copy to clipboard operation
thread-stream copied to clipboard

TransformStream does not close

Open Eomm opened this issue 2 years ago • 9 comments

When using this module within a TranformStream, it leads to this error:

/Users/mspigolon/workspace/thread-stream/index.js:278
        throw new Error('end() took too long (10s)')
        ^

Error: end() took too long (10s)
    at ThreadStream.end (/Users/mspigolon/workspace/thread-stream/index.js:278:15)
// worker.js
const { Transform } = require('stream');

async function run (opts) {
  const myTransform = new Transform({
    autoDestroy: true,
    transform(chunk, encoding, callback) {
      console.log(chunk.toString());
      callback(null)
    }
  });
  return myTransform
}

module.exports = run

// ===========================
// index.js (from README)
'use strict'

const ThreadStream = require('./index')
const { join } = require('path')

const stream = new ThreadStream({
  filename: join(__dirname, 'worker.js'),
  workerData: { dest: './qwe.sss'},
  workerOpts: {}, // Other options to be passed to Worker
  sync: false, // default
})

stream.write('hello')

// Asynchronous flushing
stream.flush(function () {
  stream.write(' ')
  stream.write('world')

  // Synchronous flushing
  stream.flushSync()
  stream.end()
})

Adding the statement:

myTransform.end = myTransform.destroy

solve the issue, but I'm not sure it is the right path to solve because it mix the readable stream interface withing the writable stream one. It seems the writer stream end correctly, but the reader is still alive.

The myTransform does not emit any events (close, finish etc..)

I tried Node.js v16 and 14 and I get the same results.

Eomm avatar Sep 21 '21 15:09 Eomm