Skip to content

Commit 50444fb

Browse files
committed
Address review feedback: port overflow, multi-PID kill, stale log
- Handle checked_add overflow gracefully instead of panicking when starting port is near u16::MAX. - Use mapfile to collect multiple lsof PIDs (IPv4/IPv6) and kill them all when replacing an old instance. - Truncate SPICEIO_LOG_FILE before starting and use tail -1 to pick the most recent "listening on" line, preventing stale endpoint discovery.
1 parent af689eb commit 50444fb

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

.github/actions/setup/action.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ runs:
9494
exit 0
9595
fi
9696
# Old or mismatched version — kill it so we can replace it
97-
OLD_PID=$(lsof -ti "tcp:${SPICEIO_BIND##*:}" -sTCP:LISTEN 2>/dev/null || true)
98-
if [[ -n "$OLD_PID" ]]; then
99-
echo "replacing spiceio ${RUNNING_VERSION:-unknown} (PID $OLD_PID) with $WANT_VERSION"
100-
kill "$OLD_PID" 2>/dev/null || true
97+
mapfile -t OLD_PIDS < <(lsof -ti "tcp:${SPICEIO_BIND##*:}" -sTCP:LISTEN 2>/dev/null || true)
98+
if (( ${#OLD_PIDS[@]} > 0 )); then
99+
echo "replacing spiceio ${RUNNING_VERSION:-unknown} (PID(s) ${OLD_PIDS[*]}) with $WANT_VERSION"
100+
kill "${OLD_PIDS[@]}" 2>/dev/null || true
101101
for i in $(seq 1 10); do
102-
lsof -ti "tcp:${SPICEIO_BIND##*:}" -sTCP:LISTEN 2>/dev/null || break
102+
lsof -ti "tcp:${SPICEIO_BIND##*:}" -sTCP:LISTEN >/dev/null 2>&1 || break
103103
sleep 1
104104
done
105105
fi
@@ -144,6 +144,7 @@ runs:
144144
export SPICEIO_BUCKET SPICEIO_REGION SPICEIO_BIND
145145
146146
SPICEIO_LOG="${RUNNER_TEMP}/spiceio.log"
147+
: > "$SPICEIO_LOG"
147148
export SPICEIO_LOG_FILE="$SPICEIO_LOG"
148149
149150
spiceio &
@@ -158,7 +159,7 @@ runs:
158159
echo "::error::spiceio exited unexpectedly"
159160
exit 1
160161
fi
161-
ENDPOINT=$(grep 'listening on' "$SPICEIO_LOG" 2>/dev/null | grep -o 'http://[^ ]*' | head -1 || true)
162+
ENDPOINT=$(grep 'listening on' "$SPICEIO_LOG" 2>/dev/null | grep -o 'http://[^ ]*' | tail -1 || true)
162163
if [[ -n "$ENDPOINT" ]] && curl -sf -I "$ENDPOINT/" 2>/dev/null | grep -qi "server: spiceio"; then
163164
echo "spiceio ready at $ENDPOINT (PID $PID)"
164165
echo "endpoint=$ENDPOINT" >> "$GITHUB_OUTPUT"

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,16 @@ async fn main() {
101101
match TcpListener::bind(addr).await {
102102
Ok(l) => break (l, addr),
103103
Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => {
104-
let next = addr.port().checked_add(1).expect("port range exhausted");
105-
if next - start_port > 100 {
106-
serr!("no available port in range {start_port}–{}", next - 1);
107-
std::process::exit(1);
108-
}
104+
let next = match addr.port().checked_add(1) {
105+
Some(n) if n - start_port <= 100 => n,
106+
_ => {
107+
serr!(
108+
"no available port in range {start_port}–{}",
109+
addr.port()
110+
);
111+
std::process::exit(1);
112+
}
113+
};
109114
addr.set_port(next);
110115
}
111116
Err(e) => {

0 commit comments

Comments
 (0)