From ca0764c2e2b61d1a0b5724f9e1021bf198d72134 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Wed, 23 Sep 2026 16:56:50 -0600 Subject: [PATCH] linux_demon: ptrace stop vs. clone notes - we cannot rely on the order! --- src/linux/demon/linux_demon.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/linux/demon/linux_demon.c b/src/linux/demon/linux_demon.c index b78c528d..acab5d68 100644 --- a/src/linux/demon/linux_demon.c +++ b/src/linux/demon/linux_demon.c @@ -2103,9 +2103,17 @@ dmn_ctrl_run(Arena *arena, DMN_CtrlCtx *ctx, DMN_RunCtrls *ctrls) // case PTRACE_EVENT_CLONE: { - // NOTE: kernel stopped the parent just before scheduling the child to - // give us a chance to prepare to trace it; next event for the child - // will be a PTRACE_EVENT_STOP + // NOTE(rjf): we get this event from the creator thread when it has + // cloned itself to create a child. immediately after creation, the + // child will raise a stop event. but importantly, the Linux kernel + // offers *NO GUARANTEES* about whether we receive the *clone* or + // the *stop* first. + // + // TODO(rjf): to account for this, upon STOP events of unrecognized + // threads, we need to accumulate a pending ID map, and then if a + // cloned thread's ID is in that map, we just immediately move it + // to a stopped state. + // pid_t new_tid = 0; if(LNX_RETRY_ON_EINTR(ptrace(PTRACE_GETEVENTMSG, wait_id, 0, &new_tid)) >= 0) {