I'm downscaling images within a separate worker thread using the sharp library. The code snippet goes like this:
async function create_preview(path) {
return lib.sharp(path)
.resize({ width: 1024, height: 1024, fit: lib.sharp.fit.inside })
.webp({ quality: 25, alphaQuality: 25 })
.toBuffer()
.then(result => "data:image/webp;base64," + result.toString("base64"));
}
Meanwhile, within my main back-end thread, I'm trying to serve up some data from a sqlite database using sqlite3. The problem is that running the preview generation on a large image (81MP) will sometimes cause the database to become unresponsive while processing. Asynchronously queueing up multiple images like that will result in a definite freeze until the processing is done.
I'm pretty lost on how to fix this, threads in node are janky to begin with and there appears to be no reason why this would happen, this happens even if there is zero communication between the main back thread and the worker thread processing the images. I'm thinking perhaps node has a limited thread pool and the worker thread using sharp is hogging all of them so there is none left for sqlite3? Does anyone have a theory?