fix(cli): print the update-available notice once, not on every event-loop drain

`process.on("beforeExit", ...)` re-fires every time the event loop
drains, and the handler kicks off a fire-and-forget async telemetry
flush — so on a successful command the user sees the
"Update available: …" notice twice (once after the initial drain, again
after the flush settles). Using `process.once` detaches the listener
after first invocation, fixing the double-print and also preventing a
double-flush of telemetry.

Reported during local testing of `auth login`, but the bug affects every
command (any path where `_flush()` schedules work).
This commit is contained in:
James
2026-05-28 05:23:27 +00:00
parent 0c0cccec96
commit 7f755913a6
+6 -2
View File
@@ -193,8 +193,12 @@ if (!isHelp && !hasJsonFlag && command !== "upgrade") {
const commandStart = Date.now();
let commandFailed = false;
// Async flush for normal exit (beforeExit fires when the event loop drains)
process.on("beforeExit", () => {
// Async flush for normal exit. `beforeExit` re-fires every time the
// event loop drains, and the async `_flush()` itself schedules new
// work — so a plain `on` listener would print the update notice (and
// re-flush) once per drain (the user-reported double-print). `once`
// detaches after first invocation, which is what we want for both.
process.once("beforeExit", () => {
_flush?.().catch(() => {});
if (!hasJsonFlag) _printUpdateNotice?.();
});