Skip to content

Commit 503bee4

Browse files
trivikrsxa
authored andcommitted
test: deflake async-hooks statwatcher test
Wait for the initial missing-file poll before writing watched files. Otherwise uv_fs_poll can observe the created file as its baseline and never emit the expected change event. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: #63396 Refs: https://github.com/nodejs/reliability/blob/main/reports/2026-05-15.md#jstest-failure Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent cccc7c3 commit 503bee4

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

‎test/async-hooks/test-statwatcher.js‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,32 @@ checkInvocations(statwatcher1, { init: 1 },
5959
'watcher1: when started to watch second file');
6060
checkWatcherStart('watcher2',statwatcher2);
6161

62-
setTimeout(()=>fs.writeFileSync(file1,'foo++'),
63-
common.platformTimeout(100));
62+
letw2Initialized=false;
63+
letwriteFile2AfterW1=false;
64+
65+
constonW2Initialized=(curr,prev)=>{
66+
if(curr.nlink!==0||prev.nlink!==0)
67+
return;
68+
69+
w2.removeListener('change',onW2Initialized);
70+
w2Initialized=true;
71+
if(writeFile2AfterW1){
72+
setTimeout(()=>fs.writeFileSync(file2,'bar++'),
73+
common.platformTimeout(100));
74+
}
75+
};
76+
w2.on('change',onW2Initialized);
77+
6478
w1.on('change',common.mustCallAtLeast((curr,prev)=>{
6579
console.log('w1 change to',curr,'from',prev);
80+
// Wait for the initial ENOENT poll before creating the file. Otherwise the
81+
// first stat can race with the write and use the created file as the baseline.
82+
if(curr.nlink===0&&prev.nlink===0){
83+
setTimeout(()=>fs.writeFileSync(file1,'foo++'),
84+
common.platformTimeout(100));
85+
return;
86+
}
87+
6688
// Wait until we get the write above.
6789
if(prev.size!==0||curr.size!==5)
6890
return;
@@ -74,8 +96,11 @@ w1.on('change', common.mustCallAtLeast((curr, prev) => {
7496
checkInvocations(statwatcher2,{init: 1},
7597
'watcher2: when unwatched first file');
7698

77-
setTimeout(()=>fs.writeFileSync(file2,'bar++'),
78-
common.platformTimeout(100));
99+
writeFile2AfterW1=true;
100+
if(w2Initialized){
101+
setTimeout(()=>fs.writeFileSync(file2,'bar++'),
102+
common.platformTimeout(100));
103+
}
79104
w2.on('change',common.mustCallAtLeast((curr,prev)=>{
80105
console.log('w2 change to',curr,'from',prev);
81106
// Wait until we get the write above.

0 commit comments

Comments
 (0)