julianhille / MuhammaraJS

Muhammara a node module with c/cpp bindings to modify PDF with js for node or electron (based/replacement on/of galkhana/hummusjs)
Other
205 stars 41 forks source link

Unable to Delete Input File #381

Open gclego opened 2 months ago

gclego commented 2 months ago

Hi, first of all thank you for maintaining this project. Its awesome.

I just encountered an issue where I will merge two pdfs, then delete the files after wards. However I'm unable to delete the files and get an error EBUSY: resource busy or locked. Workaround is not to use Recipe, but would be great if this is fixed. Please see code below:

const Recipe = require('muhammara').Recipe;
const fs = require('fs');

const pdfDoc = new Recipe('test1.pdf', 'result.pdf');
const pdf2 = 'test2.pdf';

pdfDoc.appendPage(pdf2).endPDF();

// error here: Error: EBUSY: resource busy or locked, unlink 
fs.unlinkSync('test1.pdf');
fs.unlinkSync('test2.pdf');
julianhille commented 2 months ago

And the same node script can delete the files (without any user/owner / rights modification) if the node js process has been restarted?

gclego commented 2 months ago

yes. if I just delete the files, it works fine.

julianhille commented 2 months ago

you need to do that in the done callback:

const Recipe = require('muhammara').Recipe;
const fs = require('fs');

const pdfDoc = new Recipe('test1.pdf', 'result.pdf');
const pdf2 = 'test2.pdf';

pdfDoc.appendPage(pdf2).endPDF(() => {
  fs.unlinkSync('test1.pdf');
  fs.unlinkSync('test2.pdf');
});

// error here: Error: EBUSY: resource busy or locked, unlink 
gclego commented 2 months ago

still the same error :(

julianhille commented 2 months ago

I wrote a Test which does exactly this. No issues there. Did you move the delete, for Testing purposes, to the first lines and return? So basically remove the recipe Code and then it works?

There is either some Race condition or you have that file Open somwewhere else.

Please also test if a setTimeout and unlink files later works. This might inidicate some Timing issues.

gclego commented 2 months ago

If I comment the Recipe code and just delete the files, it works. But when I put back the code, I'm getting the error. Also setTimeout does not work and the file is still locked. So I'm guessing something within the Recipe is not freeing up the files.

julianhille commented 2 months ago

please provide your os, arch and node version

gclego commented 2 months ago

windows x64 node 20.12.2