SamuelScheit / puppeteer-stream

A Library for puppeteer to retrieve audio and/or video streams
MIT License
333 stars 105 forks source link

Close browser causes Protocol error #143

Closed xuhpppp closed 2 months ago

xuhpppp commented 7 months ago

Hi everyone, i'm using Puppeteer-stream for some recording tasks, i followed the example code on main page, everything worked fine except the case that after recording, i want to close the browser by using browser.close():

setTimeout(async () => {
        await stream.destroy();
        file.close();
                await browser.close();
        console.log("finished");
}, 1000 * 10);

but the console returned error:

this._reject(callback, new Errors_js_1.TargetCloseError('Target closed'));
                                   ^

TargetCloseError: Protocol error (Runtime.callFunctionOn): Target closed

then my application stoped. I'm running the application on Ubuntu 20.04, NodeJS 18.16.0 and Puppeteer-stream 3.0.7 Have someone ever met this issue and had the fixing way for it? Thanks for reading my issue, have a good day.

dcprime commented 7 months ago

I'm also seeing this same issue. Can't find a good way to close the process after the stream has finished writing.

dhavalCode commented 7 months ago

Facing same issue. @SamuelScheit Please have a look on it.

SamuelScheit commented 7 months ago

As a workaround until I find a solution you can prevent node from crashing on errors via

process.on('uncaughtException', function (err) {
  console.error(err);
});

The error is probably caused because the stream is not properly closed, I'll investigate

saif-o99 commented 6 months ago

@SamuelScheit Hey, any news on this ?

unravelers-tech commented 5 months ago

The workaround that worked for me.

The most critical line is:

await stream.destroy();

await new Promise((resolve) => {
  file.close(async () => {
    const pages = await browser.pages();

    for (let i = 0; i < pages.length; i++) {
      const page = pages[i];

      // @ts-expect-error - get STOP_RECORDING from extension page.
      await page.evaluate((index) => window.STOP_RECORDING?.(index), i);

      await page.close();
    }

    await browser.close();

    wss.close(() => {
      resolve(undefined);
    });
  });
});

cc. @SamuelScheit @saif-o99 @dcprime @dhavalCode @xuhpppp

farzadso commented 5 months ago

@SamuelScheit Thanks for your awesome library. Did you ever find a fix for this?