From 92343fa2044e8fe0bea7be22f73191e41f54b543 Mon Sep 17 00:00:00 2001 From: Diogenes Fernandes Date: Wed, 11 Mar 2026 23:34:50 -0300 Subject: [PATCH] fix: wrapper with a signal Signed-off-by: Diogenes Fernandes --- wrapper/tofu.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/wrapper/tofu.js b/wrapper/tofu.js index 2a6be5e..85120ed 100755 --- a/wrapper/tofu.js +++ b/wrapper/tofu.js @@ -6,7 +6,7 @@ const io = require('@actions/io'); const core = require('@actions/core'); -const { exec } = require('@actions/exec'); +const { spawn } = require('child_process'); const OutputListener = require('./lib/output-listener'); const pathToCLI = require('./lib/tofu-bin'); @@ -24,19 +24,27 @@ async function checkTofu () { // Create listeners to receive output (in memory) as well const stdout = new OutputListener(process.stdout); const stderr = new OutputListener(process.stderr); - const listeners = { - stdout: stdout.listener, - stderr: stderr.listener - }; // Execute tofu and capture output const args = process.argv.slice(2); - const options = { - listeners, - ignoreReturnCode: true, - silent: true // don't print "[command...]" into stdout: https://github.com/actions/toolkit/issues/649 - }; - const exitCode = await exec(pathToCLI, args, options); + const child = spawn(pathToCLI, args, { stdio: ['inherit', 'pipe', 'pipe'] }); + + // Forward signals to child so tofu can release state locks on cancellation + const forwardSigterm = () => { child.kill('SIGTERM'); }; + const forwardSigint = () => { child.kill('SIGINT'); }; + process.on('SIGTERM', forwardSigterm); + process.on('SIGINT', forwardSigint); + + child.stdout.on('data', stdout.listener); + child.stderr.on('data', stderr.listener); + + const exitCode = await new Promise((resolve) => { + // code is null when the process is killed by a signal; treat as failure + child.on('close', (code) => resolve(code ?? 1)); + }); + + process.removeListener('SIGTERM', forwardSigterm); + process.removeListener('SIGINT', forwardSigint); // Set outputs, result, exitcode, and stderr core.setOutput('stdout', stdout.contents);