yao-pkg / pkg

Package your Node.js project into an executable
https://www.npmjs.com/package/@yao-pkg/pkg
MIT License
312 stars 11 forks source link

Temporary files in GZip compressed binary using native module are not removed when application uses `process.exit()` #52

Open kayahr opened 4 months ago

kayahr commented 4 months ago

What version of pkg are you using?

5.11.5

What version of Node.js are you using?

20.12.0

What operating system are you using?

Debian 12

What CPU architecture are you using?

x86_64

What Node versions, OSs and CPU architectures are you building for?

node20-linux-x64

Describe the Bug

A GZip packaged program using a native node module and which exits with process.exit() does not remove the extracted external files from the /tmp directory. This is because the cleanup code is attached to the "beforeExit" event which is not called when process.exit is used or an exception is thrown. When attaching the cleanup code to "exit" instead then it works.

See documentation of beforeExit and exit event.

Expected Behavior

Temporary files should always be removed when program exits.

To Reproduce

  1. Create a test.js program using a native module like node-canvas (npm i canvas). Example

    const { createCanvas } = require("canvas");
    const { writeFileSync } = require("node:fs");
    const canvas = createCanvas(1024, 768);
    buffer = canvas.toBuffer();
    writeFileSync("/tmp/out.png", buffer);
    process.exit(0);
  2. Package the program with GZIP compression:

    pkg -C GZip test.js -t node20-linux-x64
  3. Run the program.

  4. Check for /tmp/pkg-* files and notice that directory was not removed.

When not using process.exit() in the test program then the directory is removed. When using exit event instead of beforeExit event in bootstrap code then tmp directory is also correctly removed.