Skip to content

Commit

Permalink
fix: Disable file watching when an "ENOSPC: System limit for number of
Browse files Browse the repository at this point in the history
file watchers reached" error is raised.
  • Loading branch information
symwell committed Dec 12, 2022
1 parent 6301252 commit 87b0a34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/cli/src/fingerprint/fingerprintWatchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ export default class FingerprintWatchCommand {
}
}

async watcherErrorFunction (error: Error) {
if (error.message.includes("ENOSPC: System limit for number of file watchers reached")) {
console.warn(error.stack);
console.warn("Will disable file watching. File polling will stay enabled.");
await this.watcher?.close();
this.watcher = undefined;
console.warn("File watching disabled.");
} else {
// let it crash if it's some other error, to learn what the error is
throw error;
}
};

async execute() {
this.fpQueue.process().then(() => {
this.fpQueue.handler.checkVersion = false;
Expand All @@ -68,7 +81,8 @@ export default class FingerprintWatchCommand {
})
.on('add', this.added.bind(this))
.on('change', this.changed.bind(this))
.on('unlink', this.removed.bind(this));
.on('unlink', this.removed.bind(this))
.on('error', error => this.watcherErrorFunction(error));

const watchReady = new Promise<void>((r) => this.watcher?.once('ready', r));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { once } from 'events';
import Fingerprinter from '../../../src/fingerprint/fingerprinter';
import { MaxMSBetween } from '../../../src/lib/eventAggregator';
import { mkdir } from 'fs/promises';
import { FSWatcher } from 'chokidar';

jest.mock('../../../src/telemetry');
const Telemetry = jest.mocked(OriginalTelemetry);
Expand Down Expand Up @@ -84,6 +85,14 @@ describe(FingerprintWatchCommand, () => {
return verifyIndexSuccess(200, 20);
});

it('does not raise if it hits the limit of the number of file watchers', async () => {
cmd = new FingerprintWatchCommand(appMapDir);
cmd.watcher = new FSWatcher();
expect(cmd.watcher).not.toBeUndefined();
await cmd.watcherErrorFunction(new Error("ENOSPC: System limit for number of file watchers reached"));
expect(cmd.watcher).toBeUndefined();
});

describe('telemetry', () => {
let handler: Fingerprinter;

Expand Down

0 comments on commit 87b0a34

Please sign in to comment.