mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(ssm): preserve command exits and terminate timed-out process groups (#7093)
This commit is contained in:
1 parent
b7e96c4be8
commit
b4cd98fb61
6 files changed
+469
-35
No files matched your search
@@ -0,0 +1,123 @@
|
||||
package io.github.hectorvent.floci.services.ssm;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
/** Separates the supervisor's deadline signal from an arbitrary user exit status. */
|
||||
final class SsmCommandTimeout {
|
||||
private final String marker = "\036floci-ssm-timeout-" + UUID.randomUUID() + "\037";
|
||||
private final byte[] markerBytes = marker.getBytes(StandardCharsets.UTF_8);
|
||||
private final BoundedOutputCollector stderr;
|
||||
private int matched;
|
||||
private boolean timedOut;
|
||||
|
||||
SsmCommandTimeout(int limit) {
|
||||
stderr = new BoundedOutputCollector(limit);
|
||||
}
|
||||
|
||||
String wrap(String script, int seconds) {
|
||||
String command = "sh -c " + quote(script);
|
||||
if (seconds < 1) {
|
||||
return command;
|
||||
}
|
||||
// Keep GNU timeout's monitored shell alive until -k kills the entire group, even if its child exits first.
|
||||
String completedCommand = "trap 'while :; do sleep 1; done' TERM; " + command
|
||||
+ " & child=$!; wait \"$child\" 2>/dev/null; result=$?; printf '%s\\n' \"$result\" > \"$1/completed\"; exit \"$result\"";
|
||||
return """
|
||||
if ! command -v mktemp >/dev/null 2>&1; then
|
||||
exec %s
|
||||
fi
|
||||
state=$(mktemp -d 2>/dev/null) || exec %s
|
||||
if ! command -v setsid >/dev/null 2>&1; then
|
||||
case "$(timeout --version 2>/dev/null)" in
|
||||
*"GNU coreutils"*)
|
||||
timeout -k 1s %s sh -c %s sh "$state"
|
||||
result=$?
|
||||
if [ -f "$state/completed" ] && read -r completed_result < "$state/completed"; then
|
||||
result=$completed_result
|
||||
else
|
||||
case "$result" in 124|137|143) printf '%%s' %s >&2 ;; esac
|
||||
fi
|
||||
rm -rf "$state"
|
||||
exit "$result"
|
||||
;;
|
||||
*) rmdir "$state"; exec %s ;;
|
||||
esac
|
||||
fi
|
||||
setsid %s &
|
||||
child=$!
|
||||
(
|
||||
timer=
|
||||
trap 'if [ -n "$timer" ]; then kill "$timer" 2>/dev/null; wait "$timer" 2>/dev/null; fi; exit 0' TERM
|
||||
sleep %s &
|
||||
timer=$!
|
||||
wait "$timer"
|
||||
if mkdir "$state/finished" 2>/dev/null; then
|
||||
: > "$state/timed-out"
|
||||
kill -TERM "-$child" 2>/dev/null
|
||||
sleep 1
|
||||
kill -KILL "-$child" 2>/dev/null
|
||||
fi
|
||||
) &
|
||||
watchdog=$!
|
||||
wait "$child" 2>/dev/null
|
||||
result=$?
|
||||
if mkdir "$state/finished" 2>/dev/null; then
|
||||
kill "$watchdog" 2>/dev/null
|
||||
fi
|
||||
wait "$watchdog" 2>/dev/null
|
||||
if [ -f "$state/timed-out" ]; then
|
||||
printf '%%s' %s >&2
|
||||
fi
|
||||
rm -rf "$state"
|
||||
exit "$result"
|
||||
""".formatted(command, command, seconds, quote(completedCommand), quote(marker), command,
|
||||
command, seconds, quote(marker));
|
||||
}
|
||||
|
||||
synchronized void writeError(byte[] payload) {
|
||||
int start = 0;
|
||||
for (int i = 0; i < payload.length; i++) {
|
||||
byte value = payload[i];
|
||||
if (value == markerBytes[matched]) {
|
||||
if (matched == 0 && i > start) {
|
||||
stderr.write(Arrays.copyOfRange(payload, start, i));
|
||||
}
|
||||
matched++;
|
||||
start = i + 1;
|
||||
if (matched == markerBytes.length) {
|
||||
timedOut = true;
|
||||
matched = 0;
|
||||
}
|
||||
} else if (matched > 0) {
|
||||
stderr.write(Arrays.copyOf(markerBytes, matched));
|
||||
matched = 0;
|
||||
start = i;
|
||||
if (value == markerBytes[0]) {
|
||||
matched = 1;
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (payload.length > start) {
|
||||
stderr.write(Arrays.copyOfRange(payload, start, payload.length));
|
||||
}
|
||||
}
|
||||
|
||||
synchronized String standardError() {
|
||||
if (matched > 0) {
|
||||
stderr.write(Arrays.copyOf(markerBytes, matched));
|
||||
matched = 0;
|
||||
}
|
||||
return stderr.content();
|
||||
}
|
||||
|
||||
synchronized boolean timedOut() {
|
||||
return timedOut;
|
||||
}
|
||||
|
||||
static String quote(String value) {
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'";
|
||||
}
|
||||
}
|
||||
+12
-28
@@ -110,7 +110,8 @@ public class SsmDirectCommandExecutor {
|
||||
|
||||
private ExecutionResult executeInContainer(String containerId, String script, String workingDirectory, int timeoutSeconds)
|
||||
throws InterruptedException {
|
||||
String[] cmd = {"sh", "-c", timeoutWrappedScript(script, timeoutSeconds)};
|
||||
SsmCommandTimeout timeout = new SsmCommandTimeout(SsmCommandService.MAX_STDERR_CHARS);
|
||||
String[] cmd = {"sh", "-c", timeout.wrap(script, timeoutSeconds)};
|
||||
ExecCreateCmd create = dockerClient.execCreateCmd(containerId)
|
||||
.withCmd(cmd)
|
||||
.withAttachStdout(true)
|
||||
@@ -122,7 +123,6 @@ public class SsmDirectCommandExecutor {
|
||||
String execId = create.exec().getId();
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
BoundedOutputCollector stdout = new BoundedOutputCollector(SsmCommandService.MAX_STDOUT_CHARS);
|
||||
BoundedOutputCollector stderr = new BoundedOutputCollector(SsmCommandService.MAX_STDERR_CHARS);
|
||||
Instant start = Instant.now();
|
||||
|
||||
ResultCallback<Frame> callback = dockerClient.execStartCmd(execId).exec(new ResultCallback.Adapter<Frame>() {
|
||||
@@ -132,8 +132,11 @@ public class SsmDirectCommandExecutor {
|
||||
if (payload == null) {
|
||||
return;
|
||||
}
|
||||
BoundedOutputCollector target = frame.getStreamType() == StreamType.STDERR ? stderr : stdout;
|
||||
target.write(payload);
|
||||
if (frame.getStreamType() == StreamType.STDERR) {
|
||||
timeout.writeError(payload);
|
||||
} else {
|
||||
stdout.write(payload);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -143,7 +146,7 @@ public class SsmDirectCommandExecutor {
|
||||
|
||||
@Override
|
||||
public void onError(Throwable throwable) {
|
||||
stderr.write((throwable.getMessage() != null ? throwable.getMessage() : throwable.toString())
|
||||
timeout.writeError((throwable.getMessage() != null ? throwable.getMessage() : throwable.toString())
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
latch.countDown();
|
||||
}
|
||||
@@ -161,8 +164,8 @@ public class SsmDirectCommandExecutor {
|
||||
Long exitCode = dockerClient.inspectExecCmd(execId).exec().getExitCodeLong();
|
||||
int responseCode = exitCode != null ? exitCode.intValue() : 1;
|
||||
String standardOutput = stdout.content();
|
||||
String standardError = stderr.content();
|
||||
if (isTimeoutExitCode(responseCode)) {
|
||||
String standardError = timeout.standardError();
|
||||
if (timeout.timedOut()) {
|
||||
logFailureDiagnostics(containerId);
|
||||
return ExecutionResult.timedOut(standardOutput, standardError, start);
|
||||
}
|
||||
@@ -174,22 +177,11 @@ public class SsmDirectCommandExecutor {
|
||||
}
|
||||
|
||||
static String timeoutWrappedScript(String script, int timeoutSeconds) {
|
||||
return userScriptCommand(script, timeoutSeconds);
|
||||
}
|
||||
|
||||
private static String userScriptCommand(String script, int timeoutSeconds) {
|
||||
String command = "sh -c " + shellSingleQuote(script);
|
||||
if (timeoutSeconds < 1) {
|
||||
return command;
|
||||
}
|
||||
String timeout = timeoutSeconds + "s";
|
||||
return "if command -v timeout >/dev/null 2>&1; then "
|
||||
+ "timeout --kill-after=1s " + shellSingleQuote(timeout) + " " + command + "; "
|
||||
+ "else " + command + "; fi";
|
||||
return new SsmCommandTimeout(SsmCommandService.MAX_STDERR_CHARS).wrap(script, timeoutSeconds);
|
||||
}
|
||||
|
||||
static String diagnosticWrappedScript(String script) {
|
||||
return "sh -c " + shellSingleQuote(script);
|
||||
return "sh -c " + SsmCommandTimeout.quote(script);
|
||||
}
|
||||
|
||||
private void logFailureDiagnostics(String containerId) {
|
||||
@@ -268,14 +260,6 @@ public class SsmDirectCommandExecutor {
|
||||
return timeoutSeconds + 2L;
|
||||
}
|
||||
|
||||
private static boolean isTimeoutExitCode(int responseCode) {
|
||||
return responseCode == 124 || responseCode == 137 || responseCode == 143;
|
||||
}
|
||||
|
||||
private static String shellSingleQuote(String value) {
|
||||
return "'" + value.replace("'", "'\"'\"'") + "'";
|
||||
}
|
||||
|
||||
private static void closeQuietly(Closeable closeable) {
|
||||
if (closeable == null) {
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user