Skip to content

posix_spawn with POSIX_SPAWN_SETPGROUP does not handle pgroup = 0 correctly #2997

Closed

Description

Running build 17114.

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.

Per IEEE Std 1003.1-2008, 2016 Edition:

From posix_spawnattr_getpgroup:

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.

And from setpgid:

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

Test: spawntest.c

Activity

  1. added a commit that references this issue on Mar 5, 2018
  2. benhillis commented on Mar 5, 2018

    @benhillis
    Member

    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

  3. benhillis commented on Mar 5, 2018

    @benhillis
    Member

    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.

  4. mqudsi commented on Mar 5, 2018

    @mqudsi
    Author

    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;)

  5. therealkenc commented on Mar 6, 2018

    @therealkenc

    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 comment posix_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.

  6. benhillis commented on Mar 6, 2018

    @benhillis
    Member

    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.

  7. therealkenc commented on Mar 6, 2018

    @therealkenc

    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.

  8. therealkenc commented on Mar 6, 2018

    @therealkenc

    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!
    
  9. benhillis commented on Mar 6, 2018

    @benhillis
    Member

    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.

  10. therealkenc commented on Mar 6, 2018

    @therealkenc

    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.

  11. mqudsi commented on Mar 9, 2018

    @mqudsi
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions