From 66d7daabf819d7c699230fe5ad0f1c4475cd54a5 Mon Sep 17 00:00:00 2001 From: Daniel Amar Date: Mon, 5 May 2025 16:03:51 -0400 Subject: [PATCH] Add timeout control to prevent long waits during buildkit initialization Signed-off-by: Daniel Amar --- src/main.ts | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main.ts b/src/main.ts index f7c451b..ce83109 100644 --- a/src/main.ts +++ b/src/main.ts @@ -209,19 +209,35 @@ actionsToolkit.run( try { await retryWithBackoff( async () => { - const res = await Exec.getExecOutput(inspectCmd.command, inspectCmd.args, { - ignoreReturnCode: true + // Create a promise that will timeout after 15 seconds + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => { + reject(new Error('Timeout exceeded while waiting for buildkit to initialize')); + }, 15000); // 15 second timeout }); - if (res.stderr.length > 0 && res.exitCode != 0) { - throw new Error(res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'); - } - return res; + // Create the actual command execution promise + const execPromise = Exec.getExecOutput(inspectCmd.command, inspectCmd.args, { + ignoreReturnCode: true + }).then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + throw new Error(res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'); + } + return res; + }); + + // Race the timeout against the actual command + // If the command takes too long, we'll get the timeout error instead + return Promise.race([execPromise, timeoutPromise]); }, 5, // maxRetries - retry up to 5 times for buildkit initialization 1000, // initialDelay - start with 1 second 15000, // maxDelay - cap at 15 seconds - isBuildkitSocketError // only retry on buildkit socket errors + (error) => { + // Retry on buildkit socket errors or timeouts + return isBuildkitSocketError(error) || + error.message.includes('Timeout exceeded while waiting for buildkit'); + } ); } catch (error) { // Log the warning but continue - this matches current behavior where builds still succeed