raszi / node-tmp

Temporary file and directory creator for node.js
MIT License
732 stars 93 forks source link

Tmp files kept on disk #277

Closed nick-xero closed 2 years ago

nick-xero commented 2 years ago

Operating System

NodeJS Version

v14.18.1

Tmp Version

0.2.1

Expected Behavior

As per the documentation, the library should automatically clean up tmp files; but if the process doesn't exit it does not clean up tmp files.

const tmp = require('tmp');
const express = require('express')
const app = express()
const PORT = 3010;

tmp.setGracefulCleanup();

app.get('/', (_req, res) => {
  main((err,filename)=>{
    console.log("πŸš€ ~ file: index.js ~ line 7 ~ err", err)
    console.log("πŸš€ ~ file: index.js ~ line 7 ~ res", filename)
    res.send('hello world')
  });
});

app.listen(PORT, () => {
  console.log(`listening on ${PORT}`)
});

function main(callback){
    tmp.file('test', (err, result, _fdm, cleanUp) => {
      // cleanUp()
        callback(err,result);
    })
}

Calling http://localhost:3010 should create a tmp file and delete it after the callback is called, but the tmp file handle is kept on disk until the server is restarded. The workaround this is to call the cleanUp() callback.

Run lsof | grep node | grep tmp | tail -10 to verify this.

Experienced Behavior

TBD:What did actually happen?

Tmp files are cleaned up or documentation is changed.

This is probably related to #273. Let me know if you have any questions, happy to help.

raszi commented 2 years ago

This is not a bug but the expected behavior.

As the documentation says setGracefulCleanup() is cleaning up the leftover temporary files on process exit. If the application does not exist and you are not cleaning up the files by calling the cleanUp() function then the files won't be cleaned up.

Just think about it, from where would tmp know that the files you created are still required or not. You should somehow signal it.