From 5f7e0565bb22a33a99fcfecad063dc7b15873bb5 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 27 May 2018 06:07:29 +0800 Subject: [PATCH] fs: do not crash when using a closed fs event watcher Before this commit, when the user calls methods on a closed or errored fs event watcher, they could hit a crash since the FSEventWrap in C++ land may have already been destroyed with the internal pointer set to nullptr. This commit makes sure that the user cannot hit crashes like that, instead the methods calling on a closed watcher will be noops. Also explicitly documents that the watchers should not be used in `close` and `error` event handlers. --- doc/api/fs.md | 6 ++- lib/internal/fs/watchers.js | 21 ++++++++-- .../test-fs-watch-close-when-destroyed.js | 38 +++++++++++++++++++ test/parallel/test-fs-watch.js | 19 ++++++++-- 4 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 test/parallel/test-fs-watch-close-when-destroyed.js diff --git a/doc/api/fs.md b/doc/api/fs.md index 724f1b419c78ac..a07c4e6ce46446 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -325,7 +325,8 @@ fs.watch('./tmp', { encoding: 'buffer' }, (eventType, filename) => { added: v10.0.0 --> -Emitted when the watcher stops watching for changes. +Emitted when the watcher stops watching for changes. The closed +`fs.FSWatcher` object is no longer usable in the event handler. ### Event: 'error'