You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
posix_spawn with POSIX_SPAWN_SETPGROUP does not handle pgroup = 0 correctly #2997
It seems that posix_spawn does not assign a new child its own pid as its pgrp if POSIX_SPAWN_SETPGROUP is specified and posix_spawnattr_setpgroup(&attr, 0) is called.
The posix_spawnattr_getpgroup() function shall obtain the value of the spawn-pgroup attribute from the attributes object referenced by attr.
The posix_spawnattr_setpgroup() function shall set the spawn-pgroup attribute in an initialized attributes object referenced by attr.
The spawn-pgroup attribute represents the process group to be joined by the new process image in a spawn operation (if POSIX_SPAWN_SETPGROUP is set in the spawn-flags attribute). The default value of this attribute shall be zero.
As a special case, if pid is 0, the process ID of the calling process shall be used. Also, if pgid is 0, the process ID of the indicated process shall be used.
This attached sample code, when run under Windows, returns the following:
own pid: 29611
own pgid: 29610
child pid: 29612
child pgid: 29610
Test failed: child_pgid != child_pid though `posix_spawnattr_setpgroup(&attr, 0)` was specified!
while on FreeBSD and macOS, the following is returned:
QBook:~ mqudsi$ ./spawntest
own pid: 68920
own pgid: 68920
child pid: 68921
child pgid: 68921
Test passed
Interesting, just tried this myself and the test works when the binary is being "straced":
root@BENHILL-X1:/mnt/c/Users/benhill# ./a.out
own pid: 243
own pgid: 243
child pid: 244
child pgid: 243
Test failed: child_pgid != child_pid though `posix_spawnattr_setpgroup(&attr, 0)` was specified!
root@BENHILL-X1:/mnt/c/Users/benhill# strace -o clone ./a.out
own pid: 247
own pgid: 245
child pid: 248
child pgid: 248
Test passed.
Specifically it looks like we are not handling setpgid(0,0) correctly: If pgid is zero, then the PGID of the process specified by pid is made the same as its process ID
Mahmoud Al-Qudsi (@mqudsi) - I'm curious how you found this issue. I believe this might just be a race condition in your test code and/or the pthread library. POSIX_SPAWN_SETPGROUP under the covers is clone syscall followed by a setpgrp syscall in the child after the clone has completed. It does not look like the pthread library has any kind of synchronization to ensure that the setpgrp syscall in the child completes before the posix spawn returns. If I add a tiny sleep to your test before querying the child's pgid this test passes (which also explains why when stracing it changes the behavior because it changes the timing).
Linux thread scheduling is very different on Linux vs Windows so possibly this doesn't happen.
Hi Ben Hillis (@benhillis), thanks for the speedy reply. Unfortunately I ran into this in the wild with code that isn't as contrived as the test I uploaded alongside this issue.
Basically, after some recent commits to remove unnecessary code in fish, the removal of an explicit setpgid on the child process spawned as the result of a call to posix_spawn() caused this issue to surface: fish-shell/fish-shell#4778
We assign control of the terminal to the process group that should be created after calling posix_spawn with POSIX_SPAWN_SETPGROUP to allow child processes to read/write to the terminal, and wait on the process group to exit before ending the job. After this change, executing something as simple as cat & would cause the background process to end immediately.
I re-added an explicit call to setpgid from the parent to the child assigning it a pgid equal to its pid if we detect that we're running under WSL.
After stress testing non-WSL machines for quite some time I was unable to get this to reproduce (multiple processes running while ./spawntest; end;)
It does not look like the pthread library has any kind of synchronization to ensure that the setpgrp syscall in the child completes before the posix spawn returns
Ben is this the code you are talking about? Per the commentposix_spawn() uses CLONE_VFORK, which means the parent shouldn't continue until exec on cat happens -- after the __setpgid(). That behaviour is fixed though, so if it isn't working on 17114 I'm missing something. Or looking at the wrong code, because moving glibc target. Or reading the code incorrectly.
therealkenc - In my strace I am not seeing CLONE_VFORK. I am seeing flags = (CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | SIGCHLD). Perhaps this is a glibc bug that has been fixed.
Looks that way. Or at least I have mostly convinced myself as much. In glibc 2.23 code looks like this. It just calls __fork() in the case that POSIX_SPAWN_SETPGROUP is set, which waits for no one. The arch dependent __fork() is just a Linux clone() with the flags you cite above here. Calling the __setpgid() doesn't happen until later at line 164. If the parent resumed and called getpgrp() first, that's pretty much what you'd expect.
Guh. The test case fails on Real Linux™ right out of the gate for me on Xenial with glibc 2.23. I should have done that before chasing the source code. Linux-behaviour AFAICT.
ken@xenial-vm:~/Devel/spawni$ gcc spawni.c
ken@xenial-vm:~/Devel/spawni$ ./a.out
own pid: 5313
own pgid: 5313
child pid: 5314
child pgid: 5313
Test failed: child_pgid != child_pid though `posix_spawnattr_setpgroup(&attr, 0)` was specified!
therealkenc - I agree, seems like this is "by design", albeit a bug in glibc. Might just be hitting more often on WSL due to differencing between thread scheduling.
Just to clarify it's not a "more often" thing, here anyway. It's a 100% thing. In the OP it says "while on FreeBSD and macOS, the following..." (without mentioning Linux), which is subtle and I missed it. Well yeah, those work. They use a different libc.
The scheduling is going to be the pretty much the same (to a first order) no matter what OS with that sequence of fork/setpgrp/getpgrp. The parent has no good reason to lose its thread slot. The child needs to find a new home. If you ran the same sequence (extracted from glibc 2.23), pretty sure it would go south on FreeBSD and MacOS in exactly the same way. Or if it didn't go south, it would be a fluke.
That's odd, I posted a comment here already. Reposting.
therealkenc and Ben Hillis (@benhillis): thank you both kindly for your attentive replies and time. It does seem that this behavior might change in future Ubuntu releasaes using glibc 2.24+, at least per glibc#10354 once posix_spawn uses vfork()/CLONE_VFORK in real world cases.
Activity
Add temporary fix for #4778 (background processes on WSL)
benhillis commentedon Mar 5, 2018
Interesting, just tried this myself and the test works when the binary is being "straced":
Specifically it looks like we are not handling setpgid(0,0) correctly:
If pgid is zero, then the PGID of the process specified by pid is made the same as its process ID
benhillis commentedon Mar 5, 2018
Mahmoud Al-Qudsi (@mqudsi) - I'm curious how you found this issue. I believe this might just be a race condition in your test code and/or the pthread library. POSIX_SPAWN_SETPGROUP under the covers is clone syscall followed by a setpgrp syscall in the child after the clone has completed. It does not look like the pthread library has any kind of synchronization to ensure that the setpgrp syscall in the child completes before the posix spawn returns. If I add a tiny sleep to your test before querying the child's pgid this test passes (which also explains why when stracing it changes the behavior because it changes the timing).
Linux thread scheduling is very different on Linux vs Windows so possibly this doesn't happen.
mqudsi commentedon Mar 5, 2018
Hi Ben Hillis (@benhillis), thanks for the speedy reply. Unfortunately I ran into this in the wild with code that isn't as contrived as the test I uploaded alongside this issue.
Basically, after some recent commits to remove unnecessary code in fish, the removal of an explicit
setpgidon the child process spawned as the result of a call toposix_spawn()caused this issue to surface: fish-shell/fish-shell#4778We assign control of the terminal to the process group that should be created after calling
posix_spawnwithPOSIX_SPAWN_SETPGROUPto allow child processes to read/write to the terminal, and wait on the process group to exit before ending the job. After this change, executing something as simple ascat &would cause the background process to end immediately.I re-added an explicit call to
setpgidfrom the parent to the child assigning it a pgid equal to its pid if we detect that we're running under WSL.After stress testing non-WSL machines for quite some time I was unable to get this to reproduce (multiple processes running
while ./spawntest; end;)therealkenc commentedon Mar 6, 2018
Ben is this the code you are talking about? Per the comment
posix_spawn()usesCLONE_VFORK, which means the parent shouldn't continue untilexeconcathappens -- after the__setpgid(). That behaviour is fixed though, so if it isn't working on 17114 I'm missing something. Or looking at the wrong code, because moving glibc target. Or reading the code incorrectly.benhillis commentedon Mar 6, 2018
therealkenc - In my strace I am not seeing CLONE_VFORK. I am seeing flags = (CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | SIGCHLD). Perhaps this is a glibc bug that has been fixed.
therealkenc commentedon Mar 6, 2018
Looks that way. Or at least I have mostly convinced myself as much. In glibc 2.23 code looks like this. It just calls
__fork()in the case thatPOSIX_SPAWN_SETPGROUPis set, which waits for no one. The arch dependent__fork()is just a Linuxclone()with the flags you cite above here. Calling the__setpgid()doesn't happen until later at line 164. If the parent resumed and calledgetpgrp()first, that's pretty much what you'd expect.therealkenc commentedon Mar 6, 2018
Guh. The test case fails on Real Linux™ right out of the gate for me on Xenial with glibc 2.23. I should have done that before chasing the source code. Linux-behaviour AFAICT.
benhillis commentedon Mar 6, 2018
therealkenc - I agree, seems like this is "by design", albeit a bug in glibc. Might just be hitting more often on WSL due to differencing between thread scheduling.
therealkenc commentedon Mar 6, 2018
Just to clarify it's not a "more often" thing, here anyway. It's a 100% thing. In the OP it says "while on FreeBSD and macOS, the following..." (without mentioning Linux), which is subtle and I missed it. Well yeah, those work. They use a different libc.
The scheduling is going to be the pretty much the same (to a first order) no matter what OS with that sequence of
fork/setpgrp/getpgrp. The parent has no good reason to lose its thread slot. The child needs to find a new home. If you ran the same sequence (extracted from glibc 2.23), pretty sure it would go south on FreeBSD and MacOS in exactly the same way. Or if it didn't go south, it would be a fluke.mqudsi commentedon Mar 9, 2018
That's odd, I posted a comment here already. Reposting.
therealkenc and Ben Hillis (@benhillis): thank you both kindly for your attentive replies and time. It does seem that this behavior might change in future Ubuntu releasaes using glibc 2.24+, at least per glibc#10354 once
posix_spawnusesvfork()/CLONE_VFORKin real world cases.