Skip to content

Commit 943e742

Browse files
authored
fix: Retry on timeout (#106)
* fix: do not set return value for timed out runs Commands that are killed manually due to timeout rarely returns a success status code (0). These codes should not be treated as errors but simply produced because of the timeout. * fix(windows): use variable to track timeout Use a variable to track timeout instead of relying on SIGTERM, as processes on Windows are not killed using signals.
1 parent 0711ba3 commit 943e742

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

dist/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,14 +1005,15 @@ function runRetryCmd(inputs) {
10051005
function runCmd(attempt, inputs) {
10061006
var _a, _b;
10071007
return __awaiter(this, void 0, void 0, function () {
1008-
var end_time, executable, child;
1008+
var end_time, executable, timeout, child;
10091009
return __generator(this, function (_c) {
10101010
switch (_c.label) {
10111011
case 0:
10121012
end_time = Date.now() + (0, inputs_1.getTimeout)(inputs);
10131013
executable = getExecutable(inputs);
10141014
exit = 0;
10151015
done = false;
1016+
timeout = false;
10161017
(0, core_1.debug)("Running command ".concat(inputs.command, " on ").concat(OS, " using shell ").concat(executable));
10171018
child = attempt > 1 && inputs.new_command_on_retry
10181019
? (0, child_process_1.spawn)(inputs.new_command_on_retry, { shell: executable })
@@ -1026,13 +1027,17 @@ function runCmd(attempt, inputs) {
10261027
child.on('exit', function (code, signal) {
10271028
(0, core_1.debug)("Code: ".concat(code));
10281029
(0, core_1.debug)("Signal: ".concat(signal));
1029-
if (code && code > 0) {
1030-
exit = code;
1031-
}
10321030
// timeouts are killed manually
10331031
if (signal === 'SIGTERM') {
10341032
return;
10351033
}
1034+
// On Windows signal is null.
1035+
if (timeout) {
1036+
return;
1037+
}
1038+
if (code && code > 0) {
1039+
exit = code;
1040+
}
10361041
done = true;
10371042
});
10381043
_c.label = 1;
@@ -1045,6 +1050,7 @@ function runCmd(attempt, inputs) {
10451050
_c.label = 4;
10461051
case 4:
10471052
if (!(!done && child.pid)) return [3 /*break*/, 6];
1053+
timeout = true;
10481054
(0, tree_kill_1.default)(child.pid);
10491055
return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))];
10501056
case 5:

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ async function runCmd(attempt: number, inputs: Inputs) {
7171

7272
exit = 0;
7373
done = false;
74+
let timeout = false;
7475

7576
debug(`Running command ${inputs.command} on ${OS} using shell ${executable}`);
7677
const child =
@@ -88,13 +89,21 @@ async function runCmd(attempt: number, inputs: Inputs) {
8889
child.on('exit', (code, signal) => {
8990
debug(`Code: ${code}`);
9091
debug(`Signal: ${signal}`);
91-
if (code && code > 0) {
92-
exit = code;
93-
}
92+
9493
// timeouts are killed manually
9594
if (signal === 'SIGTERM') {
9695
return;
9796
}
97+
98+
// On Windows signal is null.
99+
if (timeout) {
100+
return;
101+
}
102+
103+
if (code && code > 0) {
104+
exit = code;
105+
}
106+
98107
done = true;
99108
});
100109

@@ -103,6 +112,7 @@ async function runCmd(attempt: number, inputs: Inputs) {
103112
} while (Date.now() < end_time && !done);
104113

105114
if (!done && child.pid) {
115+
timeout = true;
106116
kill(child.pid);
107117
await retryWait(ms.seconds(inputs.retry_wait_seconds));
108118
throw new Error(`Timeout of ${getTimeout(inputs)}ms hit`);

0 commit comments

Comments
 (0)