raszi / node-tmp

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

Garbage collection doesn't remove all objects #97

Closed Ari-H closed 8 years ago

Ari-H commented 8 years ago

Test code to reproduce the problem:

const tmp = require('tmp');

tmp.fileSync({ dir: './', postfix: '.1' }); tmp.fileSync({ dir: './', postfix: '.2' }); tmp.fileSync({ dir: './', postfix: '.3' }); tmp.fileSync({ dir: './', postfix: '.4' }); tmp.fileSync({ dir: './', postfix: '.5' }); tmp.fileSync({ dir: './', postfix: '.6' });

Running that code leaves files with postfixes 1, 3, and 5 unremoved.

The problem in the code is that _garbageCollector loops through _removeObjects array, but at the same time calls to _cleanupCallback modify (splice) the array. As a result some of the _removeObjects functions will get removed before they have been called. (And _garbageCollector tries to invoke call for unexisting array indexes, but because of the empty catch block that is ignored.)

raszi commented 8 years ago

Fixed with #98