Filehandle leak on error #157

Merged
merged 2 commits into from Jul 13, 2012
View
@@ -11,7 +11,7 @@ var util = require('./util'),
Stream = require('stream').Stream;
function IncomingForm(opts) {
- if (!(this instanceof IncomingForm)) return new IncomingForm;
+ if (!(this instanceof IncomingForm)) return new IncomingForm(opts);
EventEmitter.call(this);
opts=opts||{};
@@ -33,6 +33,7 @@ function IncomingForm(opts) {
this._parser = null;
this._flushing = 0;
this._fieldsSize = 0;
+ return this;
};
util.inherits(IncomingForm, EventEmitter);
exports.IncomingForm = IncomingForm;
@@ -88,6 +89,7 @@ IncomingForm.prototype.parse = function(req, cb) {
};
this.writeHeaders(req.headers);
+ this.openedFiles = [];
var self = this;
req
@@ -203,6 +205,7 @@ IncomingForm.prototype.handlePart = function(part) {
this.emit('fileBegin', part.name, file);
file.open();
+ this.openedFiles.push(file);
part.on('data', function(buffer) {
self.pause();
@@ -252,6 +255,11 @@ IncomingForm.prototype._error = function(err) {
this.error = err;
this.pause();
this.emit('error', err);
+
+ this.openedFiles.forEach(function(file) {
+ file._writeStream.destroy();
+ setTimeout(fs.unlink, 0, file.path);
+ });
};
IncomingForm.prototype._parseContentLength = function() {
@@ -336,7 +344,7 @@ IncomingForm.prototype._initMultipart = function(boundary) {
};
IncomingForm.prototype._fileName = function(headerValue) {
- var m = headerValue.match(/filename="(.*?)"($|; )/i)
+ var m = headerValue.match(/filename="(.*?)"($|; )/i);
if (!m) return;
var filename = m[1].substr(m[1].lastIndexOf('\\') + 1);
@@ -373,7 +381,7 @@ IncomingForm.prototype._uploadPath = function(filename) {
if (this.keepExtensions) {
var ext = path.extname(filename);
- ext = ext.replace(/(\.[a-z0-9]+).*/, '$1')
+ ext = ext.replace(/(\.[a-z0-9]+).*/, '$1');
name += ext;
}
View
@@ -13,13 +13,13 @@ var Buffer = require('buffer').Buffer,
PART_DATA_START: s++,
PART_DATA: s++,
PART_END: s++,
- END: s++,
+ END: s++
},
f = 1,
F =
{ PART_BOUNDARY: f,
- LAST_BOUNDARY: f *= 2,
+ LAST_BOUNDARY: f *= 2
},
LF = 10,
@@ -34,7 +34,7 @@ var Buffer = require('buffer').Buffer,
return c | 0x20;
};
-for (var s in S) {
+for (s in S) {
exports[s] = S[s];
}
@@ -207,7 +207,7 @@ MultipartParser.prototype.write = function(buffer) {
state = S.PART_DATA_START;
break;
case S.PART_DATA_START:
- state = S.PART_DATA
+ state = S.PART_DATA;
mark('partData');
case S.PART_DATA:
prevIndex = index;