diff --git a/changelog.md b/changelog.md index 54bbeb6..a95fe8d 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,12 @@ ## Pending +### Fixes + +- improve lock heartbeat shutdown handling (heartbeat) + - Replaces the stop signal promise race with an explicit wake callback for the heartbeat wait loop + - Ensures the heartbeat loop can be interrupted cleanly when stopping without leaving the timer pending + ## 2026-05-19 - 1.4.0 ### Features diff --git a/ts/classes.smartmigration.ts b/ts/classes.smartmigration.ts index 88ab06c..d67e460 100644 --- a/ts/classes.smartmigration.ts +++ b/ts/classes.smartmigration.ts @@ -544,16 +544,22 @@ export class SmartMigration { private startLockHeartbeat(ledger: Ledger): ILockHeartbeat { const intervalMs = this.getLockHeartbeatMs(); let stopped = false; - let resolveStop!: () => void; + let wakeWaiter: (() => void) | null = null; let lockError: SmartMigrationError | null = null; - const stopSignal = new Promise((resolve) => { - resolveStop = resolve; - }); - const loopPromise = (async () => { while (!stopped) { - await Promise.race([this.sleep(intervalMs), stopSignal]); + await new Promise((resolve) => { + const timeout = setTimeout(() => { + wakeWaiter = null; + resolve(); + }, intervalMs); + wakeWaiter = () => { + clearTimeout(timeout); + wakeWaiter = null; + resolve(); + }; + }); if (stopped) { return; } @@ -589,7 +595,7 @@ export class SmartMigration { } stopped = true; - resolveStop(); + wakeWaiter?.(); await loopPromise; }, };