microlinkhq / youtube-dl-exec

A simple Node.js wrapper for youtube-dl/yt-dlp.
MIT License
423 stars 75 forks source link

process.kill('SIGKILL') throwing tinyspawn error #203

Closed SnuwYx closed 3 months ago

SnuwYx commented 3 months ago

I am trying to use the kill method but I am being thrown a tinyspawn error each time, is this supposed to happen?

/<filePath>/node_modules/tinyspawn/src/index.js:22
  const error = new Error(message)
                ^

Error [ChildProcessError]: The command spawned as:

/<filePath>/node_modules/youtube-dl-exec/bin/yt-dlp https://www.youtube.com/watch?v=6xKWiCMKKJg --dump-single-json

exited with `{ code: null }` and the following trace:

    at createChildProcessError (/<filePath>/node_modules/tinyspawn/src/index.js:22:17)
    at ChildProcess.<anonymous> (/<filePath>/node_modules/tinyspawn/src/index.js:61:18)
    at ChildProcess.emit (node:events:520:28)
    at ChildProcess._handle.onexit (node:internal/child_process:294:12) {
  command:  ...

the function i ran, i tried making as similar as the one in the doc

async function test() {
const youtubedl = require('youtube-dl-exec');
const url = 'https://www.youtube.com/watch?v=6xKWiCMKKJg'
const subprocess = youtubedl.exec(url, { dumpSingleJson: true })

setTimeout(() => {
  subprocess.kill('SIGKILL')
}, 5000)

result = await subprocess
console.log(result)
}
test()
Kikobeats commented 3 months ago

Yes, because killing exit the execution with a no specific exitCode:

https://github.com/microlinkhq/tinyspawn/blob/dd30bb186b1b54ae763999a1286181c65831985f/src/index.js#L59

Since killing is forcing the process to finish unexpectedly, you can just catch the error:

const youtubedl = require('youtube-dl-exec')
const url = 'https://www.youtube.com/watch?v=6xKWiCMKKJg'

;(async () => {
  const result = await youtubedl.exec(url, { dumpSingleJson: true }, { timeout: 500, killSignal: 'SIGKILL' }).catch(err => err)
  console.log(result)
})()