fix(shutdown): arm the teardown timer before the callback that clears it
`runTeardown` declared `finish` above the `timer` it clears, so the reference held only because every caller — the timer's own callback and `teardown.run().then()` — happens to be asynchronous. That is correct by timing rather than by construction, which is what #20 asked to remove from this file; `closeApp` twenty lines below already arms its timer first and carries a comment explaining why that direction is the safe one. Reversing the order here leaves both shutdown paths resting the same forward reference on the same guarantee: setTimeout cannot invoke its callback synchronously. No behaviour changes. Closes #20 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
1 file changed
+11
-7
+11
-7
@@ -1095,13 +1095,9 @@ function runTeardown(teardown: TeardownRegistry, logger: Logger, budgetMs: numbe
|
|||||||
return new Promise<void>((resolve) => {
|
return new Promise<void>((resolve) => {
|
||||||
let settled = false;
|
let settled = false;
|
||||||
|
|
||||||
const finish = (): void => {
|
// Armed before `finish` exists, for the reason spelled out in `closeApp` below: setTimeout
|
||||||
if (settled) return;
|
// cannot fire synchronously, so the forward reference to `finish` rests on a guarantee, while
|
||||||
settled = true;
|
// the reverse ordering would rest on `teardown.run().then()` deferring.
|
||||||
clearTimeout(timer);
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
|
|
||||||
const timer = setTimeout(
|
const timer = setTimeout(
|
||||||
() => {
|
() => {
|
||||||
if (settled) return;
|
if (settled) return;
|
||||||
@@ -1114,6 +1110,14 @@ function runTeardown(teardown: TeardownRegistry, logger: Logger, budgetMs: numbe
|
|||||||
);
|
);
|
||||||
|
|
||||||
timer.unref?.();
|
timer.unref?.();
|
||||||
|
|
||||||
|
const finish = (): void => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
clearTimeout(timer);
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
|
||||||
void teardown.run(logger).then(finish, finish);
|
void teardown.run(logger).then(finish, finish);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user