Await closing Playwright before finish request

I noticed when loading Your Outfits earlier (before I switched it to just use prod images even on dev), that there was a big memory leak slowing down my machine.

My hypothesis is that this is because I wasn't _waiting_ for the resources to tear down before finishing the request, so Vercel terminated the request early, and I further hypothesize that terminating a Playwright session partway through isn't guaranteed to clean up the browser.

Not sure about that! Could have just been that we spun up a lot at once, and a bunch of things went into swap! (But I thought it generally handles requests in serial in the dev server? So that feels unlikely.)

Anyway, I don't feel like extensively testing this again and maybe messing up my computer session again :p Just, when I first wrote this without awaits, I knew that it was a bit risky, but I wanted to _see_ if it was a problem before slowing down individual requests by awaiting. And now, my "it's likely to be a problem" threshold has been reached, lol!

So, I'm not _confident_ this is the best play, I don't know the internals well enough; but it seems like a better side to err on than the other, now that I know more!
This commit is contained in:
Emi Matchu 2021-09-03 15:43:27 -07:00
parent 9a68bd1355
commit dfcd9985d4

View file

@ -122,9 +122,18 @@ async function loadAndScreenshotImage(libraryUrl, size) {
);
}
} finally {
// Tear down our resources when we're done!
page.close();
browser.close();
// Tear down our resources when we're done! If it fails, log the error, but
// don't block the success of the image.
try {
await page.close();
} catch (e) {
console.warn("Error closing page after image finished", e);
}
try {
await browser.close();
} catch (e) {
console.warn("Error closing browser after image finished", e);
}
}
}