Waiting for a cloned baby assembly with sysall

I am trying to wait for the process that I cloned. However, when the parent does syscall before waitid, I get -1 ECHILDwhen using strace. This is despite the fact that the clone call returns the PID for the created child, as shown here:

clone(child_stack=0x7ffe2b412d10, flags=CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWPID) = 3735
waitid(P_PID, 3735, NULL, WEXITED, NULL) = -1 ECHILD (No child processes)

If I create a loop that repeatedly calls waitid, it eventually gives the expected result of waiting for the child. This makes me believe that there is some kind of race condition when the child has not started correctly yet, but has received PID.

The following is the appropriate assembler code:

_start: 
    mov rax, SYS_CLONE
    mov rdi, CLONE_FLAGS
    mov rsi, rsp
    mov rdx, 0
    mov r10, 0
    syscall

    cmp rax, 0
    je _clone   

    mov rdi, PPID       
    mov rsi, rax    ; pid
    mov rdx, 0
    mov r10, 4      ; exited
    mov rax, SYS_WAITID
    syscall

    mov rdi, OK_EXIT
    jmp _exit

_clone:
    mov rax, SYS_EXECVE
    mov rdi, [rsp + 16]
    lea rsi, [rsp + 16]
    lea rdx, [rsp + 40]
    syscall

    mov rdi, rax
    jmp _exit   

_exit:
    mov rax, SYS_EXIT
    syscall

, NULL (siginfo_t *infop) waitid, , , , , . ? , , ?

+4
1

flags=SIGCHLD | ... clone, SIGCHLD , include/uapi/linux/sched.h.

GAS SIGCHLD

.globl _start

.text
a:.quad 1
  .quad 0
b:.quad d

c:
 mov $56,%rax
#SIGCHLD
 mov $17,%rdi
 mov $b,%rsi
 syscall
 ret

d:
 mov $35,%rax
 mov $a,%rdi
 syscall
 mov $60,%rax
 syscall

_start:
 call c
 mov %rax,%rsi
 mov $247,%rax
 mov $1,%rdi
 mov $4,%r10
 syscall
 mov $60,%rax
 syscall
+1

Source: https://habr.com/ru/post/1661424/