Skip to content

Commit a8302f9

Browse files
committed
fix: fix heartbeat startup/shutdown timing bug
The heartbeat code sets a timeout to start the heartbeat then sets an interval to perform the heartbeat. If the node is shut down after the timeout is set but before it fires, the interval is set and the process never ends. This PR refactors the heatbeat shutdown to clear the timeout as well as the interval.
1 parent 71cb905 commit a8302f9

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/heartbeat.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@ class Heartbeat {
1919
throw errcode(new Error(errMsg), 'ERR_HEARTBEAT_ALREADY_RUNNING')
2020
}
2121

22-
const heartbeatTimer = {
22+
const heartbeat = this._heartbeat.bind(this)
23+
24+
const timeout = setTimeout(() => {
25+
heartbeat()
26+
this._heartbeatTimer.runPeriodically(heartbeat, constants.GossipSubHeartbeatInterval)
27+
}, constants.GossipSubHeartbeatInitialDelay)
28+
29+
this._heartbeatTimer = {
2330
_onCancel: null,
24-
_timeoutId: null,
31+
_intervalId: null,
2532
runPeriodically: (fn, period) => {
26-
heartbeatTimer._timeoutId = setInterval(fn, period)
33+
this._heartbeatTimer._intervalId = setInterval(fn, period)
2734
},
2835
cancel: () => {
29-
clearTimeout(heartbeatTimer._timeoutId)
36+
clearTimeout(timeout)
37+
clearInterval(this._heartbeatTimer._intervalId)
3038
}
3139
}
32-
33-
const heartbeat = this._heartbeat.bind(this)
34-
35-
setTimeout(() => {
36-
heartbeat()
37-
heartbeatTimer.runPeriodically(heartbeat, constants.GossipSubHeartbeatInterval)
38-
}, constants.GossipSubHeartbeatInitialDelay)
39-
40-
this._heartbeatTimer = heartbeatTimer
4140
}
4241

4342
/**

0 commit comments

Comments
 (0)