From c09bfd81b7e9a6f8fc0909e2eca5c1b35152811e 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. PR-URL: https://github.com/nodejs/node/pull/20985 Fixes: https://github.com/nodejs/node/issues/20738 Fixes: https://github.com/nodejs/node/issues/20297 Reviewed-By: Anna Henningsen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Ruben Bridgewater Reviewed-By: Ron Korving Reviewed-By: Sakthipriyan Vairamani Reviewed-By: James M Snell Reviewed-By: Tiancheng "Timothy" Gu Backport-PR-URL: https://github.com/nodejs/node/pull/21172 --- 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 e492660bae2f2a..02bf4abdf8b13b 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'