Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: improve fs.watch ENOSPC error message #21846

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fs: improve fs.watch ENOSPC error message
Providing `No space left on device` is misleading in this case.
Replace it with something that describes it more accurately.

Refs: https://stackoverflow.com/questions/22475849/node-js-error-enospc/32600959
  • Loading branch information
addaleax committed Sep 11, 2018
commit 73f4325a5eb06bc5f2bc9f12840f1df8359056ad
2 changes: 1 addition & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function getMessage(key, args) {
*/
function uvException(ctx) {
const [ code, uvmsg ] = errmap.get(ctx.errno);
let message = `${code}: ${uvmsg}, ${ctx.syscall}`;
let message = `${code}: ${ctx.description || uvmsg}, ${ctx.syscall}`;

let path;
let dest;
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
StatWatcher: _StatWatcher
} = process.binding('fs');
const { FSEvent } = internalBinding('fs_event_wrap');
const { UV_ENOSPC } = internalBinding('uv');
const { EventEmitter } = require('events');
const {
getStatsFromBinding,
Expand Down Expand Up @@ -165,7 +166,9 @@ FSWatcher.prototype.start = function(filename,
const error = errors.uvException({
errno: err,
syscall: 'watch',
path: filename
path: filename,
description: err === UV_ENOSPC ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Might want to call it message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to call it message. By the way if the key is message it currently will not appear in the final error object as an additional property whereas description will.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @addaleax

'System limit for number of file watchers reached' : ''
});
error.filename = filename;
throw error;
Expand Down
50 changes: 50 additions & 0 deletions test/sequential/test-fs-watch-system-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');
const stream = require('stream');

if (!common.isLinux)
common.skip('The fs watch limit is OS-dependent');

const processes = [];
const gatherStderr = new stream.PassThrough();
gatherStderr.setEncoding('utf8');
gatherStderr.setMaxListeners(Infinity);

let finished = false;
function spawnProcesses() {
for (let i = 0; i < 10; ++i) {
const proc = child_process.spawn(
process.execPath,
[ '-e',
`process.chdir(${JSON.stringify(__dirname)});
for (const file of fs.readdirSync('.'))
fs.watch(file, () => {});`
], { stdio: ['inherit', 'inherit', 'pipe'] });
proc.stderr.pipe(gatherStderr);
processes.push(proc);
}

setTimeout(() => {
if (!finished && processes.length < 200)

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

spawnProcesses();
}, 100);
}

spawnProcesses();

let accumulated = '';
gatherStderr.on('data', common.mustCallAtLeast((chunk) => {
accumulated += chunk;
// console.log(chunk);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBD (to be deleted)

if (accumulated.includes('Error:') && !finished) {
assert(
accumulated.includes('ENOSPC: System limit for number ' +
'of file watchers reached'),
accumulated);
console.log(`done after ${processes.length} processes, cleaning up`);
finished = true;
processes.forEach((proc) => proc.kill());
}
}, 1));