Skip to content

Commit

Permalink
Fix cleanup on failure/interruption
Browse files Browse the repository at this point in the history
  • Loading branch information
rhaschke committed Feb 15, 2025
2 parents 82bdec9 + e5ecc5e commit a857e83
Show file tree
Hide file tree
Showing 43 changed files with 2,484 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
# override/add settings for specific distros
- { DEB_DISTRO: bookworm, DEBS: true, VERBOSE: bloom }
- { DEB_DISTRO: noble, VERBOSE: true }
- { DEB_DISTRO: jammy, ARCH: arm64, BUILD_TIMEOUT: 3, ROS_SOURCES: good, DEBS: true, EXPECT_EXIT_CODE: 143 }
- { DEB_DISTRO: jammy, ARCH: arm64, BUILD_TIMEOUT: 4, ROS_SOURCES: good, DEBS: true, EXPECT_EXIT_CODE: 130 }
# add failing jobs
- { DEB_DISTRO: focal, ARCH: x64, ROS_SOURCES: broken, EXPECT_EXIT_CODE: 2, CONTINUE_ON_ERROR: false, DEBS: true }
- { DEB_DISTRO: focal, ARCH: x64, ROS_SOURCES: broken, EXPECT_EXIT_CODE: 1, CONTINUE_ON_ERROR: true, COLCON_PKG_SELECTION: '--packages-up-to rostime' }
Expand Down
29 changes: 19 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const fs = require('fs')
const child_process = require('child_process')
const core = require('@actions/core')
const ps = require('ps-node')

// find paths of generic.sh and action .sh script
function script_paths() {
Expand All @@ -27,22 +28,28 @@ function script_paths() {
var [generic, action] = script_paths()
var child = child_process.spawn(generic, [action], { stdio: 'inherit' })

// kill child on signals SIGINT and SIGTERM
function forward(signal) {
child.kill(signal)

ps.lookup({ ppid: child.pid }, function (err, resultList) {
if (err) { throw new Error(err) }
resultList.forEach(function (p) {
try { process.kill(p.pid, signal) } catch (error) { }
})
})
}
function handle(signal) {
console.log('Forwarding signal ' + signal + ' to child process')
// Escalate signal INT -> TERM -> KILL
// https://github.com/ringerc/github-actions-signal-handling-demo
if (signal == 'SIGINT') { signal = 'SIGTERM' }
else if (signal == 'SIGTERM') { signal = 'SIGKILL' }
child.kill(signal)
forward(signal)
}
// kill child (and sub processes) on signals SIGINT and SIGTERM
process.on('SIGINT', handle)
process.on('SIGTERM', handle)

// exit if child exits
child.on('exit', function (exit_code, signal) {
const expect = core.getInput('EXPECT_EXIT_CODE') || 0 // expected exit code
exit_code = exit_code !== null ? exit_code : 143
exit_code = exit_code !== null ? exit_code : 130
const suffix = exit_code == expect ? ' (as expected)' : ' != ' + expect
const msg = 'Process finished with code ' + exit_code + suffix
exit_code == expect ? core.debug(msg) : core.warning(msg)
Expand All @@ -52,9 +59,11 @@ child.on('exit', function (exit_code, signal) {
// cancel build after given timout (github default: 6h - 20min slack)
const timeout_minutes = core.getInput('BUILD_TIMEOUT') || (6 * 60 - 20)
function cancel() {
console.log('')
core.warning('Cancelling build due to timeout')
child.kill('SIGTERM')
// escalate to SIGKILL after 5s
setTimeout(function () { child.kill('SIGKILL') }, 5 * 1000)
forward('SIGINT')
// escalate to SIGTERM after 5s
// https://github.com/ringerc/github-actions-signal-handling-demo
setTimeout(function () { forward('SIGTERM') }, 5 * 1000)
}
setTimeout(cancel, timeout_minutes * 60 * 1000)
21 changes: 21 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions node_modules/connected-domain/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/connected-domain/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions node_modules/connected-domain/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/connected-domain/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a857e83

Please sign in to comment.