Streampunk / beamcoder

Node.js native bindings to FFmpeg.
GNU General Public License v3.0
397 stars 76 forks source link

Governor used in MuxerStream doesn't allow node process to exit #65

Open jbaudanza opened 3 years ago

jbaudanza commented 3 years ago

I'm having an issue where sometimes my node process won't exit, even after calling process.exit.

The reason is that node won't execute if there are still outstanding async tasks in its thread pool. I tracked the outstanding task down to the readExecute call in governor.cc.

Here is a simple program to illustrate the problem:

const beamcoder = require("beamcoder");

const muxerStream = beamcoder.muxerStream({ highwaterMark: 65536 });

muxerStream.on('data', () => { console.log('data'); })

// At this point, the process should exit, but it won't because of the outstanding readExecute call in governor.cc
process.exit(0) // <- won't work

The workaround I've found it to force muxerStream to emit an "end" event by pushing "null". This triggers an event handler in beamstreams.js that calls governor.finish(). There is probably a better way.

muxerStream.push(null);

This is never an issue when the muxes is allowed to run to completion. However, often times error cases mean I need to abort the process early. In this case, I need a clean way to shut down the governor queue.

Bahlinc-Dev commented 2 years ago

I think you are doing it correctly? It's just a node stream thing. From nodejs docs:

Passing chunk as null signals the end of the stream (EOF) and behaves the same as readable.push(null), after which no more data can be written. The EOF signal is put at the end of the buffer and any buffered data will still be flushed.

Also, Looks like this is what node does internally if you call readable.destroy()