diff --git a/lib/fs-storage.js b/lib/fs-storage.js index 455d745e8..170b8efc1 100644 --- a/lib/fs-storage.js +++ b/lib/fs-storage.js @@ -42,7 +42,7 @@ function write_stream(name) { this.queue(data); }, function() { this.queue(null); - }); + }, {autoDestroy: false}); stream.pause(); fs.exists(name, function(exists) { @@ -54,10 +54,13 @@ function write_stream(name) { file.write(data); }); stream.on('end', function() { - fs.rename(tmpname, name, function(err) { - if (err) stream.emit('error', err); - stream.emit('close'); + file.on('close', function() { + fs.rename(tmpname, name, function(err) { + if (err) stream.emit('error', err); + stream.emit('close'); + }); }); + file.destroySoon(); }); stream.resume(); });