Fix restarting provisioner loop

This commit is contained in:
Fedor Indutny
2025-04-03 16:34:07 -07:00
committed by GitHub
parent b9e3985058
commit 84eb305b9a

View File

@@ -236,6 +236,7 @@ export class Provisioner {
}
log.info(`Provisioner: stopping, reason=${reason}`);
this.#sockets = [];
this.#abortController?.abort();
this.#abortController = undefined;
this.#isRunning = false;
@@ -272,6 +273,11 @@ export class Provisioner {
log.info(`${logId}: connected, refreshing in ${delay}ms`);
} catch (error) {
// New loop is running
if (signal !== this.#abortController?.signal) {
return;
}
// The only active socket has failed, notify subscribers and shutdown
if (this.#sockets.length === 0) {
if (error === TIMEOUT_ERROR || error instanceof ConnectTimeoutError) {
@@ -314,6 +320,11 @@ export class Provisioner {
// eslint-disable-next-line no-await-in-loop
await sleep(delay, signal);
} catch (error) {
// New loop is running
if (signal !== this.#abortController?.signal) {
return;
}
// Sleep aborted
strictAssert(
this.#subscribers.size === 0,
@@ -439,23 +450,29 @@ export class Provisioner {
code: number,
reason: string
): void {
log.info(`Provisioner: socket closed, code=${code}, reason=${reason}`);
const index = this.#sockets.indexOf(resource);
if (index === -1) {
log.info(
'Provisioner: ignoring socket closed, ' +
`code=${code}, reason=${reason}`
);
return;
}
log.info(`Provisioner: socket closed, code=${code}, reason=${reason}`);
// Is URL from the socket displayed as a QR code?
const isActive = index === this.#sockets.length - 1;
this.#sockets.splice(index, 1);
// Graceful closure
if (state === SocketState.Done) {
log.info('Provisioner: socket closed gracefully');
return;
}
if (isActive) {
log.info('Provisioner: active socket closed');
this.#notify({
kind:
state === SocketState.WaitingForUuid