| Issue |
234890
|
| Summary |
[lldb][debugserver] process detach can leave the process stopped on macOS
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
im-lowbyte
|
On macOS, `process detach` can leave the process stopped. lldb prints `Process <pid> detached` and exits 0, debugserver exits, and the process stays stopped (`T` in `ps`), no longer traced, until something sends it SIGCONT.
I see this on macOS 27.0.1 (`xnu-13432.1.9`) on Apple silicon with 12 logical CPUs, using the lldb from Xcode 27.0 (27A266a), whose `lldb --version` is `lldb-2103.0.34.103`. It fails the same way with Xcode's own debugserver and with debugserver built from `main` at 2e750162eb43 (Release with assertions, Xcode 27.0 clang, MacOSX27.0 SDK, ad-hoc signed). Supplied debugserver logs from macOS 26.6.2 (`xnu-12377.161.14`) with Apple's debugserver show the same log signature as below.
Running the process at background QoS alongside one background-tier `yes` per logical CPU makes it frequent. This reproduces it with Xcode 27.0's lldb, run in an empty directory:
```sh
cat > hb.c <<'EOF'
#include <stdio.h>
#include <unistd.h>
int hb_value(void) { return 7; }
int main(void) {
for (int n = 0;; n++) { printf("%d\n", n); fflush(stdout); usleep(100000); }
}
EOF
cc -g -o hb hb.c
load=()
for i in $(seq "$(sysctl -n hw.logicalcpu)"); do taskpolicy -b yes > /dev/null & load+=($!); done
taskpolicy -b ./hb > hb.log & hb=$!
sleep 1
/Applications/Xcode.app/Contents/Developer/usr/bin/lldb --no-lldbinit -b -p "$hb" -o 'expr hb_value()' -o 'process detach'
sleep 2
ps -o stat= -p "$hb"
kill "${load[@]}"; kill -CONT "$hb"; kill "$hb"
```
`ps` prints `T` when the process was left stopped and `S` when it resumed. I ran this 10 times per debugserver with Xcode 27.0's lldb, a fresh process each time. The recorded runs added an lldb log to the block, `-O 'log enable -f <file> gdb-remote process'`, to record which debugserver lldb launched. They also sent lldb's output to a file with stdin from `/dev/null`, counted heartbeat lines, and read `ps` again 2 s later. With Xcode's debugserver the process was left stopped 10 of 10 times. With debugserver from `main` at 2e750162eb43, selected by setting `LLDB_DEBUGSERVER_PATH` to its absolute path, also 10 of 10. With the fix on top, 0 of 10. With in-tree lldb (the fix does not change it) and debugserver from 2e750162eb43, the API test that comes with the fix failed 10 of 10 runs under the same load. Its method that only attaches and detaches, with no _expression_, failed in all 10 of those runs too.
A longer A/B run on the same setup used 50 interleaved pairs, Xcode's lldb for both arms, and a fresh process per attempt (attach, read a global, detach). Unpatched debugserver left the process stopped and untraced in 48 of 50 attempts, in a read 2 s after debugserver acknowledged the detach and again 2 s later; lldb and debugserver had both exited by the end of the attempt. In all 48 the parent's `waitpid(WUNTRACED)` reported the SIGSTOP (*confirmed*). With the fix, 0 of 50 were left stopped, and the added wait took 1.6 ms median and 5.2 ms at most.
With debugserver's log on (`LLDB_DEBUGSERVER_LOG_FILE` with `LLDB_DEBUGSERVER_LOG_FLAGS=0x240f`), the detaches that left the process stopped share one signature. After `ptrace(PT_DETACH)`, debugserver reads the task's suspend count and calls `task_resume` only if it is above 0. In the A/B run the count was 0 in each of the 48 failures, so `task_resume` was skipped, and 1 in each of the other 52 detaches (*confirmed*).
`MachProcess::Detach()` sends SIGSTOP, replies to the resulting exception with thread signal -1, shuts down the exception thread and calls `PT_DETACH`. Nothing orders the stopped thread's consumption of that reply against `PT_DETACH`.
- With the order forced by temporary instrumentation, on an earlier `main` whose debugserver sources are identical to 2e750162eb43, detaching before the reply was consumed left the process stopped 5 of 5 times, and consuming it first left it not stopped 5 of 5 times (*confirmed*).
- In public XNU (`bsd/kern/mach_process.c` in apple-oss-distributions/xnu@f6217f891ac0), when `sigwait` is still set, `PT_DETACH` calls `task_resume()` itself (from source). That matches the suspend count of 0 the logs above show.
- The same path writes `p_xstat = data` (0), which would drop the pending stop. On `xnu-13432.1.9`, `p_xstat` (`kp_proc.p_xstat` from `sysctl` `KERN_PROC_PID`) still read 17 (SIGSTOP) after a successful `PT_DETACH` with data 0 in all 84 healthy detaches of my earlier runs, with Xcode's debugserver and with debugserver built from that earlier `main`. So the thread, leaving the exception wait untraced, *likely* takes the SIGSTOP as a real stop. The source of `xnu-13432.1.9` is not public.
I have a fix that makes `Detach()` wait, while still traced, until the reply is consumed, and will open a PR for it.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs