Skip to content

Commit 9d276f1

Browse files
committed
Merge pull request #121 from Klowner/file-descriptor-exhaustion-issue
Fix file descriptor exhaustion when streaming many files
2 parents 00ece7e + 1f3af33 commit 9d276f1

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/src/getContents/streamFile.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
var fs = require('graceful-fs');
44
var stripBom = require('strip-bom-stream');
5+
var lazystream = require('lazystream');
56

67
function streamFile(file, opt, cb) {
7-
file.contents = fs.createReadStream(file.path);
8+
var filePath = file.path;
9+
10+
file.contents = new lazystream.Readable(function() {
11+
return fs.createReadStream(filePath);
12+
});
813

914
if (opt.stripBOM) {
1015
file.contents = file.contents.pipe(stripBom());

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"graceful-fs": "^4.0.0",
1717
"gulp-sourcemaps": "^1.5.2",
1818
"is-valid-glob": "^0.3.0",
19+
"lazystream": "^1.0.0",
1920
"merge-stream": "^1.0.0",
2021
"mkdirp": "^0.5.0",
2122
"object-assign": "^4.0.0",

test/dest.js

+33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var should = require('should');
1919
require('mocha');
2020

2121
var wipeOut = function() {
22+
this.timeout(20000);
2223
spies.setError('false');
2324
statSpy.reset();
2425
chmodSpy.reset();
@@ -1366,4 +1367,36 @@ describe('dest stream', function() {
13661367
.once('finish', done);
13671368
});
13681369

1370+
it('should not exhaust available file descriptors when streaming thousands of files', function(done) {
1371+
// This can be a very slow test on boxes with slow disk i/o
1372+
this.timeout(0);
1373+
1374+
// Make a ton of hard links
1375+
var numFiles = 6000;
1376+
var srcFile = path.join(__dirname, './fixtures/test.coffee');
1377+
fs.mkdirSync(path.join(__dirname, './out-fixtures'));
1378+
fs.mkdirSync(path.join(__dirname, './out-fixtures/in/'));
1379+
1380+
for (var idx = 0; idx < numFiles; idx++) {
1381+
fs.linkSync(srcFile, path.join(__dirname, './out-fixtures/in/test' + idx + '.coffee'));
1382+
}
1383+
1384+
var srcStream = vfs.src(path.join(__dirname, './out-fixtures/in/*.coffee'), { buffer: false });
1385+
var destStream = vfs.dest('./out-fixtures/out/', { cwd: __dirname });
1386+
1387+
var fileCount = 0;
1388+
1389+
srcStream
1390+
.pipe(through.obj(function(file, enc, cb) {
1391+
fileCount++;
1392+
1393+
cb(null, file);
1394+
}))
1395+
.pipe(destStream)
1396+
.once('finish', function() {
1397+
fileCount.should.equal(numFiles);
1398+
done();
1399+
});
1400+
});
1401+
13691402
});

0 commit comments

Comments
 (0)