Skip to content

Commit fb0812c

Browse files
authored
fix(qa): prevent false update-restart package failures (#120300)
* test(qa): expose update restart process exits * test(qa): supervise update restart gateway * test(qa): isolate supervised gateway environment * test(qa): diagnose restart plugin convergence * test(qa): mount trusted upgrade harness * test(qa): include upgrade runtime companions * test(qa): remove temporary restart diagnostics * test(qa): enforce systemd restart budget * test(qa): honor systemd stop timeout * test(qa): tighten package fixture ownership * test(qa): preserve unrelated lane shapes * test(qa): complete service fixture boundaries
1 parent 9195bd5 commit fb0812c

9 files changed

Lines changed: 681 additions & 28 deletions

‎scripts/e2e/lib/upgrade-survivor/run.sh‎

Lines changed: 154 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ cleanup() {
230230
if [ -n "${plugin_registry_pid:-}" ]; then
231231
kill "$plugin_registry_pid" >/dev/null 2>&1 || true
232232
fi
233+
if [ -s "$SYSTEMCTL_SHIM_PID_FILE" ]; then
234+
systemctl --user stop openclaw-gateway.service >/dev/null 2>&1 || true
235+
fi
233236
openclaw_e2e_terminate_gateways "${gateway_pid:-}"
234237
if [ -s "$SYSTEMCTL_SHIM_PID_FILE" ]; then
235238
local shim_pid
@@ -784,6 +787,7 @@ set -euo pipefail
784787
log_file="${OPENCLAW_UPGRADE_SURVIVOR_SYSTEMCTL_SHIM_LOG:-/tmp/openclaw-systemctl-shim.log}"
785788
pid_file="${OPENCLAW_UPGRADE_SURVIVOR_SYSTEMCTL_SHIM_PID_FILE:-/tmp/openclaw-systemctl-shim.pid}"
786789
daemon_log="${OPENCLAW_UPGRADE_SURVIVOR_SYSTEMCTL_SHIM_DAEMON_LOG:-/tmp/openclaw-systemctl-shim-gateway.log}"
790+
supervisor_script="${pid_file}.supervisor.mjs"
787791
printf '%s\n' "$*" >>"$log_file"
788792
789793
filtered=()
@@ -815,24 +819,27 @@ command="${filtered[0]:-status}"
815819
is_running() {
816820
[ -s "$pid_file" ] || return 1
817821
local pid
822+
local process_state
818823
pid="$(cat "$pid_file" 2>/dev/null || true)"
819824
[ -n "$pid" ] || return 1
820-
kill -0 "$pid" >/dev/null 2>&1
825+
kill -0 "$pid" >/dev/null 2>&1 || return 1
826+
process_state="$(awk '{ print $3 }' "/proc/$pid/stat" 2>/dev/null || true)"
827+
[ "$process_state" != "Z" ]
821828
}
822829
823830
stop_gateway() {
824-
[ -s "$pid_file" ] || return 0
825-
local pid
831+
local pid=""
826832
pid="$(cat "$pid_file" 2>/dev/null || true)"
827833
if [[ "$pid" =~ ^[0-9]+$ ]] && [ "$pid" -gt 1 ] && kill -0 "$pid" >/dev/null 2>&1; then
828834
kill "$pid" >/dev/null 2>&1 || true
829-
for _ in $(seq 1 100); do
830-
kill -0 "$pid" >/dev/null 2>&1 || break
835+
# The supervisor gives its child 30s, so keep this outer deadline comfortably longer.
836+
for _ in $(seq 1 350); do
837+
is_running || break
831838
sleep 0.1
832839
done
833840
kill -9 "$pid" >/dev/null 2>&1 || true
834841
fi
835-
rm -f "$pid_file"
842+
rm -f "$pid_file" "$supervisor_script"
836843
}
837844
838845
unit_path() {
@@ -873,9 +880,149 @@ start_gateway() {
873880
echo "systemctl shim could not find ExecStart in $unit" >&2
874881
return 1
875882
}
883+
rm -f "$pid_file" "$supervisor_script"
884+
cat >"$supervisor_script" <<'SUPERVISOR'
885+
import fs from "node:fs";
886+
import { spawn } from "node:child_process";
887+
888+
const command = process.env.OPENCLAW_SYSTEMCTL_SHIM_EXEC_START;
889+
const daemonLog = process.env.OPENCLAW_SYSTEMCTL_SHIM_DAEMON_LOG;
890+
if (!command || !daemonLog) {
891+
process.exit(2);
892+
}
893+
894+
const output = fs.openSync(daemonLog, "a");
895+
const childEnv = { ...process.env };
896+
delete childEnv.OPENCLAW_SYSTEMCTL_SHIM_EXEC_START;
897+
delete childEnv.OPENCLAW_SYSTEMCTL_SHIM_DAEMON_LOG;
898+
// systemd does not pass transient systemctl-caller update state into the service.
899+
for (const key of Object.keys(childEnv)) {
900+
if (key.startsWith("OPENCLAW_UPDATE_")) {
901+
delete childEnv[key];
902+
}
903+
}
904+
delete childEnv.OPENCLAW_COMPATIBILITY_HOST_VERSION;
905+
const restartDelayMs = 5_000;
906+
const restartWindowMs = 60_000;
907+
const restartBurst = 5;
908+
const stopTimeoutMs = 30_000;
909+
const starts = [];
910+
let child;
911+
let activeGroupPid;
912+
let drainingGroupPid;
913+
let stopping = false;
914+
915+
const finish = () => {
916+
try {
917+
fs.closeSync(output);
918+
} catch {}
919+
process.exit(0);
920+
};
921+
922+
const signalProcessGroup = (pid, signal) => {
923+
try {
924+
process.kill(-pid, signal);
925+
} catch (error) {
926+
if (error?.code !== "ESRCH") {
927+
fs.writeSync(output, `[systemctl-shim] gateway process group ${signal} failed: ${String(error)}\n`);
928+
}
929+
}
930+
};
931+
932+
const isProcessGroupRunning = (pid) => {
933+
try {
934+
process.kill(-pid, 0);
935+
return true;
936+
} catch (error) {
937+
return error?.code !== "ESRCH";
938+
}
939+
};
940+
941+
const drainProcessGroup = (pid, onStopped) => {
942+
if (!pid) return onStopped();
943+
if (drainingGroupPid === pid) return;
944+
drainingGroupPid = pid;
945+
let completed = false;
946+
const complete = () => {
947+
if (completed) return;
948+
completed = true;
949+
if (drainingGroupPid === pid) drainingGroupPid = undefined;
950+
if (activeGroupPid === pid) activeGroupPid = undefined;
951+
onStopped();
952+
};
953+
signalProcessGroup(pid, "SIGTERM");
954+
const forceKill = setTimeout(() => {
955+
signalProcessGroup(pid, "SIGKILL");
956+
complete();
957+
}, stopTimeoutMs);
958+
const finishWhenStopped = () => {
959+
if (completed) return;
960+
if (isProcessGroupRunning(pid)) {
961+
setTimeout(finishWhenStopped, 25);
962+
return;
963+
}
964+
clearTimeout(forceKill);
965+
complete();
966+
};
967+
finishWhenStopped();
968+
};
969+
970+
const stop = () => {
971+
if (stopping) return;
972+
stopping = true;
973+
if (drainingGroupPid) return;
974+
if (activeGroupPid) {
975+
drainProcessGroup(activeGroupPid, finish);
976+
return;
977+
}
978+
if (child) {
979+
child.kill("SIGTERM");
980+
return;
981+
}
982+
finish();
983+
};
984+
985+
const start = () => {
986+
if (stopping) return finish();
987+
const now = Date.now();
988+
while (starts.length > 0 && starts[0] <= now - restartWindowMs) {
989+
starts.shift();
990+
}
991+
if (starts.length >= restartBurst) {
992+
fs.writeSync(output, "[systemctl-shim] gateway restart limit reached\n");
993+
return finish();
994+
}
995+
starts.push(now);
996+
child = spawn("bash", ["-lc", `exec ${command}`], {
997+
detached: true,
998+
env: childEnv,
999+
stdio: ["ignore", output, output],
1000+
});
1001+
activeGroupPid = child.pid;
1002+
const childGroupPid = activeGroupPid;
1003+
child.on("error", (error) => {
1004+
fs.writeSync(output, `[systemctl-shim] gateway spawn failed: ${String(error)}\n`);
1005+
});
1006+
child.once("close", (code) => {
1007+
child = undefined;
1008+
drainProcessGroup(childGroupPid, () => {
1009+
if (stopping) return finish();
1010+
// Match the generated systemd unit's RestartPreventExitStatus contract.
1011+
if (code === 78) return finish();
1012+
setTimeout(start, restartDelayMs);
1013+
});
1014+
});
1015+
};
1016+
1017+
process.on("SIGINT", stop);
1018+
process.on("SIGTERM", stop);
1019+
start();
1020+
SUPERVISOR
8761021
(
8771022
load_unit_environment "$unit"
878-
nohup bash -lc "exec $exec_start" >>"$daemon_log" 2>&1 &
1023+
OPENCLAW_SYSTEMCTL_SHIM_EXEC_START="$exec_start" \
1024+
OPENCLAW_SYSTEMCTL_SHIM_DAEMON_LOG="$daemon_log" \
1025+
nohup node "$supervisor_script" </dev/null >/dev/null 2>&1 &
8791026
printf '%s\n' "$!" >"$pid_file"
8801027
)
8811028
}

‎scripts/e2e/lib/upgrade-survivor/update-restart-auth.sh‎

Lines changed: 151 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set -euo pipefail
1010
log_file="${OPENCLAW_UPGRADE_SURVIVOR_SYSTEMCTL_SHIM_LOG:-/tmp/openclaw-systemctl-shim.log}"
1111
pid_file="${OPENCLAW_UPGRADE_SURVIVOR_SYSTEMCTL_SHIM_PID_FILE:-/tmp/openclaw-systemctl-shim.pid}"
1212
daemon_log="${OPENCLAW_UPGRADE_SURVIVOR_SYSTEMCTL_SHIM_DAEMON_LOG:-/tmp/openclaw-systemctl-shim-gateway.log}"
13+
supervisor_script="${pid_file}.supervisor.mjs"
1314
printf '%s\n' "$*" >>"$log_file"
1415
1516
filtered=()
@@ -41,24 +42,27 @@ command="${filtered[0]:-status}"
4142
is_running() {
4243
[ -s "$pid_file" ] || return 1
4344
local pid
45+
local process_state
4446
pid="$(cat "$pid_file" 2>/dev/null || true)"
4547
[ -n "$pid" ] || return 1
46-
kill -0 "$pid" >/dev/null 2>&1
48+
kill -0 "$pid" >/dev/null 2>&1 || return 1
49+
process_state="$(awk '{ print $3 }' "/proc/$pid/stat" 2>/dev/null || true)"
50+
[ "$process_state" != "Z" ]
4751
}
4852
4953
stop_gateway() {
50-
[ -s "$pid_file" ] || return 0
51-
local pid
54+
local pid=""
5255
pid="$(cat "$pid_file" 2>/dev/null || true)"
5356
if [[ "$pid" =~ ^[0-9]+$ ]] && [ "$pid" -gt 1 ] && kill -0 "$pid" >/dev/null 2>&1; then
5457
kill "$pid" >/dev/null 2>&1 || true
55-
for _ in $(seq 1 100); do
56-
kill -0 "$pid" >/dev/null 2>&1 || break
58+
# The supervisor gives its child 30s, so keep this outer deadline comfortably longer.
59+
for _ in $(seq 1 350); do
60+
is_running || break
5761
sleep 0.1
5862
done
5963
kill -9 "$pid" >/dev/null 2>&1 || true
6064
fi
61-
rm -f "$pid_file"
65+
rm -f "$pid_file" "$supervisor_script"
6266
}
6367
6468
unit_path() {
@@ -99,9 +103,149 @@ start_gateway() {
99103
echo "systemctl shim could not find ExecStart in $unit" >&2
100104
return 1
101105
}
106+
rm -f "$pid_file" "$supervisor_script"
107+
cat >"$supervisor_script" <<'SUPERVISOR'
108+
import fs from "node:fs";
109+
import { spawn } from "node:child_process";
110+
111+
const command = process.env.OPENCLAW_SYSTEMCTL_SHIM_EXEC_START;
112+
const daemonLog = process.env.OPENCLAW_SYSTEMCTL_SHIM_DAEMON_LOG;
113+
if (!command || !daemonLog) {
114+
process.exit(2);
115+
}
116+
117+
const output = fs.openSync(daemonLog, "a");
118+
const childEnv = { ...process.env };
119+
delete childEnv.OPENCLAW_SYSTEMCTL_SHIM_EXEC_START;
120+
delete childEnv.OPENCLAW_SYSTEMCTL_SHIM_DAEMON_LOG;
121+
// systemd does not pass transient systemctl-caller update state into the service.
122+
for (const key of Object.keys(childEnv)) {
123+
if (key.startsWith("OPENCLAW_UPDATE_")) {
124+
delete childEnv[key];
125+
}
126+
}
127+
delete childEnv.OPENCLAW_COMPATIBILITY_HOST_VERSION;
128+
const restartDelayMs = 5_000;
129+
const restartWindowMs = 60_000;
130+
const restartBurst = 5;
131+
const stopTimeoutMs = 30_000;
132+
const starts = [];
133+
let child;
134+
let activeGroupPid;
135+
let drainingGroupPid;
136+
let stopping = false;
137+
138+
const finish = () => {
139+
try {
140+
fs.closeSync(output);
141+
} catch {}
142+
process.exit(0);
143+
};
144+
145+
const signalProcessGroup = (pid, signal) => {
146+
try {
147+
process.kill(-pid, signal);
148+
} catch (error) {
149+
if (error?.code !== "ESRCH") {
150+
fs.writeSync(output, `[systemctl-shim] gateway process group ${signal} failed: ${String(error)}\n`);
151+
}
152+
}
153+
};
154+
155+
const isProcessGroupRunning = (pid) => {
156+
try {
157+
process.kill(-pid, 0);
158+
return true;
159+
} catch (error) {
160+
return error?.code !== "ESRCH";
161+
}
162+
};
163+
164+
const drainProcessGroup = (pid, onStopped) => {
165+
if (!pid) return onStopped();
166+
if (drainingGroupPid === pid) return;
167+
drainingGroupPid = pid;
168+
let completed = false;
169+
const complete = () => {
170+
if (completed) return;
171+
completed = true;
172+
if (drainingGroupPid === pid) drainingGroupPid = undefined;
173+
if (activeGroupPid === pid) activeGroupPid = undefined;
174+
onStopped();
175+
};
176+
signalProcessGroup(pid, "SIGTERM");
177+
const forceKill = setTimeout(() => {
178+
signalProcessGroup(pid, "SIGKILL");
179+
complete();
180+
}, stopTimeoutMs);
181+
const finishWhenStopped = () => {
182+
if (completed) return;
183+
if (isProcessGroupRunning(pid)) {
184+
setTimeout(finishWhenStopped, 25);
185+
return;
186+
}
187+
clearTimeout(forceKill);
188+
complete();
189+
};
190+
finishWhenStopped();
191+
};
192+
193+
const stop = () => {
194+
if (stopping) return;
195+
stopping = true;
196+
if (drainingGroupPid) return;
197+
if (activeGroupPid) {
198+
drainProcessGroup(activeGroupPid, finish);
199+
return;
200+
}
201+
if (child) {
202+
child.kill("SIGTERM");
203+
return;
204+
}
205+
finish();
206+
};
207+
208+
const start = () => {
209+
if (stopping) return finish();
210+
const now = Date.now();
211+
while (starts.length > 0 && starts[0] <= now - restartWindowMs) {
212+
starts.shift();
213+
}
214+
if (starts.length >= restartBurst) {
215+
fs.writeSync(output, "[systemctl-shim] gateway restart limit reached\n");
216+
return finish();
217+
}
218+
starts.push(now);
219+
child = spawn("bash", ["-lc", `exec ${command}`], {
220+
detached: true,
221+
env: childEnv,
222+
stdio: ["ignore", output, output],
223+
});
224+
activeGroupPid = child.pid;
225+
const childGroupPid = activeGroupPid;
226+
child.on("error", (error) => {
227+
fs.writeSync(output, `[systemctl-shim] gateway spawn failed: ${String(error)}\n`);
228+
});
229+
child.once("close", (code) => {
230+
child = undefined;
231+
drainProcessGroup(childGroupPid, () => {
232+
if (stopping) return finish();
233+
// Match the generated systemd unit's RestartPreventExitStatus contract.
234+
if (code === 78) return finish();
235+
setTimeout(start, restartDelayMs);
236+
});
237+
});
238+
};
239+
240+
process.on("SIGINT", stop);
241+
process.on("SIGTERM", stop);
242+
start();
243+
SUPERVISOR
102244
(
103245
load_unit_environment "$unit"
104-
nohup bash -lc "exec $exec_start" >>"$daemon_log" 2>&1 &
246+
OPENCLAW_SYSTEMCTL_SHIM_EXEC_START="$exec_start" \
247+
OPENCLAW_SYSTEMCTL_SHIM_DAEMON_LOG="$daemon_log" \
248+
nohup node "$supervisor_script" </dev/null >/dev/null 2>&1 &
105249
printf '%s\n' "$!" >"$pid_file"
106250
)
107251
}

0 commit comments

Comments
 (0)