node-watch icon indicating copy to clipboard operation
node-watch copied to clipboard

Crash when trying to watch a non-existing network name on Windows

Open maksis opened this issue 1 year ago • 3 comments

I'm trying to watch a non-existing network name (\\INVALID\Directory\) and the application crashes with the following stack trace:

Error: \\INVALID\Directory\ does not exist.
    at webpack:\airdcpp-share-monitor\node_modules\node-watch\lib\watch.js:494:1
    at processTicksAndRejections (node:internal/process/task_queues:85:11)

I checked that fs.stat in is.file

https://github.com/yuanchuan/node-watch/blob/bd85b602be49437dc49db46dfd3fa2548075a3ac/lib/watch.js#L534

throws the following error so the error emitter method is never being exposed:

code = 'UNKNOWN'
errno =-4094
path = '\\\\INVALID\\Folder\\'
syscall ='stat'
message = "UNKNOWN: unknown error, stat '\\\\INVALID\\Folder\\'"

I couldn't figure out that why checkStat only handles certain error codes safely and crashes the app on others.

https://github.com/yuanchuan/node-watch/blob/bd85b602be49437dc49db46dfd3fa2548075a3ac/lib/is.js#L14

maksis avatar Oct 25 '24 18:10 maksis

I believe that the crash was avoided before https://github.com/yuanchuan/node-watch/commit/e22615667b0382925db2d46866ee76abb9e043b9 since I had the watch call inside a try..catch block but it no longer helps when process.nextTick is present

maksis avatar Oct 25 '24 18:10 maksis

Hi @maksis, sorry for the delay in reply.

How about adding an error handler?


const watcher = watch('...');

watcher.on('error', () => {
  // retry watch
});

yuanchuan avatar Jul 14 '25 07:07 yuanchuan

Hi @maksis, sorry for the delay in reply.

How about adding an error handler?

const watcher = watch('...');

watcher.on('error', () => { // retry watch });

As written earlier:

I checked that fs.stat in is.file

node-watch/lib/watch.js

Line 534 in bd85b60

if (is.file(fpath)) { throws the following error so the error emitter method is never being exposed:

code = 'UNKNOWN'
errno =-4094
path = '\\\\INVALID\\Folder\\'
syscall ='stat'
message = "UNKNOWN: unknown error, stat '\\\\INVALID\\Folder\\'"

I couldn't figure out that why checkStat only handles certain error codes safely and crashes the app on others.

node-watch/lib/is.js

Line 14 in bd85b60

if (/^(ENOENT|EPERM|EACCES)$/.test(err.code)) {

My implementation is open source: https://github.com/airdcpp-web/airdcpp-share-monitor/blob/c765eb48ef2801f8b9b158019f81cbeef6bf624a/src/monitor/monitor.ts#L123

maksis avatar Jul 14 '25 08:07 maksis