Skip to content

Commit e48b01a

Browse files
committed
test,util: fix flaky test-util-sigint-watchdog
Fix parallel/test-util-sigint-watchdog by polling until the signal has definitely been received instead of just using a timeout. Fixes: #7919
1 parent 8fafdf7 commit e48b01a

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/node_util.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ void StopSigintWatchdog(const FunctionCallbackInfo<Value>& args) {
113113
args.GetReturnValue().Set(had_pending_signals);
114114
}
115115

116+
117+
void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
118+
bool ret = SigintWatchdogHelper::GetInstance()->HasPendingSignal();
119+
args.GetReturnValue().Set(ret);
120+
}
121+
122+
116123
void Initialize(Local<Object> target,
117124
Local<Value> unused,
118125
Local<Context> context) {
@@ -138,6 +145,7 @@ void Initialize(Local<Object> target,
138145

139146
env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog);
140147
env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog);
148+
env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint);
141149
}
142150

143151
} // namespace util

src/node_watchdog.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ bool SigintWatchdogHelper::Stop() {
258258
}
259259

260260

261+
bool SigintWatchdogHelper::HasPendingSignal() {
262+
Mutex::ScopedLock lock(list_mutex_);
263+
264+
return has_pending_signal_;
265+
}
266+
267+
261268
void SigintWatchdogHelper::Register(SigintWatchdog* wd) {
262269
Mutex::ScopedLock lock(list_mutex_);
263270

src/node_watchdog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class SigintWatchdogHelper {
6363
static SigintWatchdogHelper* GetInstance() { return &instance; }
6464
void Register(SigintWatchdog* watchdog);
6565
void Unregister(SigintWatchdog* watchdog);
66+
bool HasPendingSignal();
6667

6768
int Start();
6869
bool Stop();

test/parallel/test-util-sigint-watchdog.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,41 @@ if (common.isWindows) {
2020
// Test with one call to the watchdog, one signal.
2121
binding.startSigintWatchdog();
2222
process.kill(process.pid, 'SIGINT');
23-
setTimeout(common.mustCall(() => {
23+
waitForPendingSignal(common.mustCall(() => {
2424
const hadPendingSignals = binding.stopSigintWatchdog();
2525
assert.strictEqual(hadPendingSignals, true);
2626
next();
27-
}), common.platformTimeout(100));
27+
}));
2828
},
2929
(next) => {
3030
// Nested calls are okay.
3131
binding.startSigintWatchdog();
3232
binding.startSigintWatchdog();
3333
process.kill(process.pid, 'SIGINT');
34-
setTimeout(common.mustCall(() => {
34+
waitForPendingSignal(common.mustCall(() => {
3535
const hadPendingSignals1 = binding.stopSigintWatchdog();
3636
const hadPendingSignals2 = binding.stopSigintWatchdog();
3737
assert.strictEqual(hadPendingSignals1, true);
3838
assert.strictEqual(hadPendingSignals2, false);
3939
next();
40-
}), common.platformTimeout(100));
40+
}));
4141
},
4242
() => {
4343
// Signal comes in after first call to stop.
4444
binding.startSigintWatchdog();
4545
binding.startSigintWatchdog();
4646
const hadPendingSignals1 = binding.stopSigintWatchdog();
4747
process.kill(process.pid, 'SIGINT');
48-
setTimeout(common.mustCall(() => {
48+
waitForPendingSignal(common.mustCall(() => {
4949
const hadPendingSignals2 = binding.stopSigintWatchdog();
5050
assert.strictEqual(hadPendingSignals1, false);
5151
assert.strictEqual(hadPendingSignals2, true);
52-
}), common.platformTimeout(100));
52+
}));
5353
}].reduceRight((a, b) => common.mustCall(b).bind(null, a))();
54+
55+
function waitForPendingSignal(cb) {
56+
if (binding.watchdogHasPendingSigint())
57+
cb();
58+
else
59+
setTimeout(waitForPendingSignal, 10, cb);
60+
}

0 commit comments

Comments
 (0)