fix(guesttools): decouple fallback file sync from control loop

Run fallback sync_files in a non-overlapping background subshell with a completion marker and zombie-state fallback so large shared directories never starve apply_host_config, report_status (30s keymap freshness), or sync_clipboard. Also prioritize sync_keymap_carrier before drive/folder mounts in apply_host_config and run keymap teardown before killing/reaping the background sync worker in cleanup().
This commit is contained in:
棠渚听潮 committed 2026-12-02 11:28:12 +08:00
1 parent 70e65d931b
commit b108f58cb0
3 files changed
+194 -5

No files matched your search

+2 -2
View File
@@ -2,6 +2,6 @@
"group": "fixes",
"order": 0,
"authors": ["reiasu2"],
"zh": "修复默认共享文件夹 WebDAV 认证挂载与失效重连、兜底同步同名覆盖和单文件异常中止,以及便携目录移动后的共享路径漂移(需在虚拟机内重新安装 Guest Tools 升级至 v11)。",
"en": "Fix default shared-folder WebDAV credential mounting and stale-mount recovery, fallback sync same-name overwrites and single-file stat aborts, and shared-folder path drift after moving a portable directory (reinstall Guest Tools in macOS to upgrade to v11)."
"zh": "修复默认共享文件夹 WebDAV 认证挂载与失效重连、兜底同步同名覆盖与单文件异常中止、大目录兜底同步阻塞心跳与修饰键映射,以及便携目录移动后的共享路径漂移(需在虚拟机内重新安装 Guest Tools 升级至 `v13`)。",
"en": "Fix default shared-folder WebDAV credential mounting and stale-mount recovery, fallback sync same-name overwrites and single-file stat aborts, large-directory fallback sync blocking status heartbeats and modifier key remapping, and shared-folder path drift after moving a portable directory (reinstall Guest Tools in macOS to upgrade to `v13`)."
}
+154
View File
@@ -0,0 +1,154 @@
package guesttools
import "testing"
func TestGuestAgentFileSyncControlLoop(t *testing.T) {
agent := string(GuestAgentScript())
state := scriptSection(t, agent, "\nFILE_SYNC_PID=", "\nkeymap_active=")
cleanup := scriptSection(t, agent, "\ncleanup() {", "\n\n")
worker := scriptSection(t, agent, "\nreap_file_sync() {", "\ntext_input_loop &")
config := scriptSection(t, agent, "\napply_host_config() {", "\nreport_status() {")
loop := scriptSection(t, agent, "\nwhile :; do\n", "\ndone") + "done\n"
fixture := fileSyncControlSetup + state + cleanup + worker + fileSyncControlStubs
t.Run("control_loop", func(t *testing.T) {
runShareShell(t, fixture+config+fileSyncControlExercise+loop, nil)
})
t.Run("cleanup_running_worker", func(t *testing.T) {
runShareShell(t, fixture+`
report_status() { [ "$1" = 1 ]; }
sync_files() {
: > "$TMP_DIR/sync-download.part"
sleep 30 >/dev/null 2>&1 &
child=$!
printf '%s\n' "$child" > child.pid
: > ready-0
command wait "$child"
}
run_file_sync_async
await_file ready-0
worker_pid=$FILE_SYNC_PID
start_sec=$(date +%s)
cleanup
elapsed=$(( $(date +%s) - start_sec ))
trap - EXIT
child_pid=$(cat child.pid)
command kill "$child_pid" 2>/dev/null || true
[ "$elapsed" -lt 5 ] || fail "cleanup blocked for ${elapsed}s waiting on foreground child"
check "$(cat reaped)" "$worker_pid" "cleanup reaped the worker"
check "$(cat parent-cleanup)" disable "parent cleanup disabled the keymap once"
[ ! -f "$FILE_SYNC_DONE" ] && [ ! -f "$TMP_DIR/sync-download.part" ] ||
fail "cleanup left files from the terminated worker"
[ ! -f "$INJECT_LOCK" ] || fail "cleanup left the injection lock"
if command kill -0 "$worker_pid" 2>/dev/null; then fail "worker survived cleanup"; fi
`, nil)
})
}
const fileSyncControlSetup = `set -eu
TMP_DIR=.
INJECT_LOCK=./injecting
CAFFEINATE_PID=""
TEXT_INPUT_PID=""
keymap_applied=true
tick=0
: > "$INJECT_LOCK"
: > parent-cleanup
`
const fileSyncControlStubs = `
fail() { printf '%s\n' "$*" >&2; exit 1; }
check() { [ "$1" = "$2" ] || fail "$3: expected '$2', got '$1'"; }
await_file() {
tries=0
until [ -f "$1" ]; do
tries=$((tries + 1))
[ "$tries" -le 200 ] || fail "timed out waiting for $1"
sleep 0.05
done
}
apply_hidutil_carrier() { printf '%s\n' "$1" >> parent-cleanup; }
# Model a completed child that still answers kill -0 until wait.
kill() {
[ "$1" != -0 ] || return 0
command kill "$@"
}
wait() {
printf '%s\n' "$1" >> reaped
command wait "$@"
}
sync_files() {
printf '%s\n' "$tick" >> started
: > "$TMP_DIR/sync-download.part"
: > "ready-$tick"
until [ -f "release-$tick" ]; do
command kill -0 "$$" 2>/dev/null || return 1
sleep 0.05
done
}
`
const fileSyncControlExercise = `
REIMS_HOST_URL=http://stub
REIMS_TOKEN=token
clip_enabled=1
mount_mode=sync
: > "$FILE_SYNC_DONE"
load_conf() { :; }
ensure_webdav_mount() {
if [ "$tick" -eq 15 ]; then mount_mode=webdav; fi
}
curl() { printf '%s\n' '{"shared_folders_count":2}'; }
sync_keymap_carrier() { printf '%s keymap\n' "$tick" >> config; }
sync_drive_mounts() { printf '%s drives\n' "$tick" >> config; }
sync_folder_mounts() { printf '%s folders=%s\n' "$tick" "$1" >> config; }
sync_timezone() { :; }
report_status() { printf '%s\n' "$tick" >> status; }
sync_clipboard() {
printf '%s\n' "$tick" >> clipboard
case "$tick" in
1)
first_pid=$FILE_SYNC_PID
[ -n "$first_pid" ] || fail "first fallback tick did not start a worker"
await_file ready-0
[ ! -f "$FILE_SYNC_DONE" ] || fail "worker started with a stale completion marker"
;;
10)
check "$FILE_SYNC_PID" "$first_pid" "only one worker ran while the first was blocked"
check "$(cat started)" 0 "fallback runs before the gate opens"
check "$(cat status)" "$(printf '0\n5\n10')" "status ticks while the worker is blocked"
check "$(cat clipboard)" "$(printf '%s\n' 0 1 2 3 4 5 6 7 8 9 10)" "clipboard ticks while the worker is blocked"
check "$(cat config)" "$(printf '%s\n' '0 keymap' '0 drives' '0 folders=2' \
'5 keymap' '5 drives' '5 folders=2' '10 keymap' '10 drives' '10 folders=2')" "host config order"
: > release-0
await_file "$FILE_SYNC_DONE"
[ ! -s parent-cleanup ] && [ -f "$INJECT_LOCK" ] || fail "worker ran parent cleanup"
[ ! -f "$TMP_DIR/sync-download.part" ] || fail "worker left a partial download"
;;
11)
check "$FILE_SYNC_PID" "" "first worker PID after reaping"
check "$(cat reaped)" "$first_pid" "first worker was waited on"
;;
13)
second_pid=$FILE_SYNC_PID
[ -n "$second_pid" ] || fail "no second fallback worker started"
await_file ready-12
check "$(cat started)" "$(printf '0\n12')" "fallback restarted after reaping"
;;
15)
check "$mount_mode" webdav "the mount recovered during the second sync"
check "$FILE_SYNC_PID" "$second_pid" "second worker was still running at the mode switch"
: > release-12
await_file "$FILE_SYNC_DONE"
;;
17)
check "$FILE_SYNC_PID" "" "completed worker was cleared in WebDAV mode"
[ ! -f "$FILE_SYNC_DONE" ] || fail "completion marker remained after reaping in WebDAV mode"
check "$(cat reaped)" "$(printf '%s\n' "$first_pid" "$second_pid")" "both workers were waited on"
check "$(cat started)" "$(printf '0\n12')" "WebDAV did not start a fallback worker"
exit 0
;;
esac
}
`
+38 -3
View File
@@ -143,6 +143,8 @@ rm -f "$INJECT_LOCK" 2>/dev/null || true
CAFFEINATE_PID=""
TEXT_INPUT_PID=""
FILE_SYNC_PID=""
FILE_SYNC_DONE="$TMP_DIR/file-sync.done"
keymap_active="false"
keymap_applied="false"
` + guestKeymapCarrier + guestTimezoneAgent + guestTimezoneConfig + `
@@ -152,13 +154,20 @@ cleanup() {
apply_hidutil_carrier disable
report_status 1
fi
if [ -n "${FILE_SYNC_PID:-}" ]; then
if [ ! -f "$FILE_SYNC_DONE" ]; then
pkill -P "$FILE_SYNC_PID" 2>/dev/null || true
kill "$FILE_SYNC_PID" 2>/dev/null || true
fi
wait "$FILE_SYNC_PID" 2>/dev/null || true
fi
if [ -n "$CAFFEINATE_PID" ]; then
kill "$CAFFEINATE_PID" 2>/dev/null || true
fi
if [ -n "$TEXT_INPUT_PID" ]; then
kill "$TEXT_INPUT_PID" 2>/dev/null || true
fi
rm -f "$INJECT_LOCK" 2>/dev/null || true
rm -f "$INJECT_LOCK" ${FILE_SYNC_DONE:+"$FILE_SYNC_DONE"} ${TMP_DIR:+"$TMP_DIR/sync-download.part"} 2>/dev/null || true
}
trap cleanup EXIT
trap 'exit 0' INT TERM
@@ -482,10 +491,10 @@ apply_host_config() {
[ -n "$REIMS_HOST_URL" ] && [ -n "$REIMS_TOKEN" ] || return 0
cfg_json=$(curl -fsS -m 3 -H "Authorization: Bearer $REIMS_TOKEN" "$REIMS_HOST_URL/v1/config" 2>/dev/null || true)
[ -n "$cfg_json" ] || return 0
sync_keymap_carrier "$cfg_json"
sync_drive_mounts "$cfg_json"
folders_count=$(printf '%s\n' "$cfg_json" | sed -n 's/.*"shared_folders_count"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p')
sync_folder_mounts "${folders_count:-0}"
sync_keymap_carrier "$cfg_json"
sync_timezone "$cfg_json"
if printf '%s' "$cfg_json" | grep -q '"clipboard_enabled"[[:space:]]*:[[:space:]]*false'; then
clip_enabled="0"
@@ -748,10 +757,36 @@ text_input_loop() {
done
}
reap_file_sync() {
[ -n "$FILE_SYNC_PID" ] || return 0
# A completed child can still answer kill -0 until it is reaped.
if [ ! -f "$FILE_SYNC_DONE" ] && kill -0 "$FILE_SYNC_PID" 2>/dev/null; then
case "$(ps -o stat= -p "$FILE_SYNC_PID" 2>/dev/null || true)" in
*Z*) ;;
*) return 0 ;;
esac
fi
wait "$FILE_SYNC_PID" 2>/dev/null || true
FILE_SYNC_PID=""
rm -f "$FILE_SYNC_DONE" 2>/dev/null || true
}
run_file_sync_async() {
[ -z "$FILE_SYNC_PID" ] || return 0
rm -f "$FILE_SYNC_DONE" || return
(
trap 'rm -f "$TMP_DIR/sync-download.part" 2>/dev/null || true; : > "$FILE_SYNC_DONE"' EXIT
trap - INT TERM
sync_files
) &
FILE_SYNC_PID=$!
}
text_input_loop &
TEXT_INPUT_PID=$!
while :; do
reap_file_sync
if [ $((tick % 5)) -eq 0 ]; then
load_conf
ensure_webdav_mount
@@ -764,7 +799,7 @@ while :; do
sleep 3
fi
if [ "$mount_mode" != "webdav" ] && [ $((tick % 2)) -eq 0 ]; then
sync_files
run_file_sync_async
fi
tick=$((tick + 1))
done