Skip to content

Commit 00bec19

Browse files
committed
Make profile-haskell-function.sh play nicely with script -qfc from simula launcher
1 parent f63d97e commit 00bec19

1 file changed

Lines changed: 51 additions & 1 deletion

File tree

‎utils/profile-haskell-function.sh‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,67 @@ restore_tty() {
105105
}
106106
trap restore_tty EXIT
107107

108+
matching_local_simula_pids() {
109+
local repo_cwd pid cwd
110+
repo_cwd="$(pwd -P)"
111+
pgrep -f 'simula-unwrapped --local|script -qfc .*godot\.x11\.tools\.64|godot\.x11\.tools\.64 -m "\./\."|godot\.x11\.tools\.64 -m \./\.' |
112+
while read -r pid; do
113+
[[ "$pid" == "$$" ]] && continue
114+
cwd="$(readlink -f "/proc/$pid/cwd" 2>/dev/null)" || continue
115+
[[ "$cwd" == "$repo_cwd" ]] && printf '%s\n' "$pid"
116+
done
117+
}
118+
119+
pid_in_list() {
120+
local needle="$1"
121+
local pid
122+
shift
123+
for pid in "$@"; do
124+
[[ "$pid" == "$needle" ]] && return 0
125+
done
126+
return 1
127+
}
128+
129+
cleanup_timed_out_simula_processes() {
130+
local pid
131+
local -a pids_to_kill=()
132+
mapfile -t current_matching_pids < <(matching_local_simula_pids)
133+
134+
for pid in "${current_matching_pids[@]}"; do
135+
if ! pid_in_list "$pid" "${preexisting_matching_pids[@]}"; then
136+
pids_to_kill+=("$pid")
137+
fi
138+
done
139+
140+
[[ "${#pids_to_kill[@]}" -eq 0 ]] && return 0
141+
142+
echo "profile-haskell-function.sh: timeout reached; terminating local Simula/Godot pids: ${pids_to_kill[*]}" >&2
143+
kill -TERM "${pids_to_kill[@]}" 2>/dev/null || true
144+
sleep 1
145+
146+
for pid in "${pids_to_kill[@]}"; do
147+
if kill -0 "$pid" 2>/dev/null; then
148+
kill -KILL "$pid" 2>/dev/null || true
149+
fi
150+
done
151+
}
152+
108153
set +e
154+
mapfile -t preexisting_matching_pids < <(matching_local_simula_pids)
109155
SIMULA_DEBUG_PROFILE_HUD=1 \
110156
SIMULA_DEBUG_PROFILE_ROOT="$profile_function" \
111-
timeout "$test_time" "${command[@]}"
157+
timeout --foreground "$test_time" "${command[@]}"
112158
exit_code=$?
159+
if [[ "$exit_code" -eq 124 ]]; then
160+
cleanup_timed_out_simula_processes
161+
fi
113162
restore_tty
114163

115164
ended_at="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
116165

117166
if [[ ! -s "$hud_live_path" ]]; then
118167
echo "profile-haskell-function.sh: $hud_live_path was not written or is empty" >&2
168+
echo "profile-haskell-function.sh: simula exit code was $exit_code" >&2
119169
exit 1
120170
fi
121171

0 commit comments

Comments
 (0)