defunkt/resque — Issue #481: Worker parent process waits for any children to finish
Author: rafaelspereira
Created: 2011-12-21 21:21:22+00:00
State: open

When worker executes the fork to create its child, which will perform the job, it waits for the child with a "Process.wait" , without any arguments. When no arguments are sent, the wait assumes pid=-1 by default, which means that the parent waits for any child process. Is there any reason to not use the child pid in wait?

```ruby
Process.wait @child
```

We are experiencing problems because the parent resumes its execution while its child is still running, since we perform another fork in parent process, to collect some data. The problem is detailed bellow:

```
--- ps fax just after the worker fork:
17492 ? Sl 0:12 resque-1.17.1: Forked 17880 at 1324410958
17880 ? Sl 0:00 \_ resque-1.17.1: Processing test_job since 1324410958

---strace (note that wait is resumed by the parent but child is still running, and parent receive a wrong PID)
[webmediaapi@cittavld59 webmediaapi]$ strace -p 17492 -p 17880
Process 17492 attached - interrupt to quit
Process 17880 attached - interrupt to quit
[pid 17880] clock_gettime(CLOCK_REALTIME,
[pid 17492] wait4(-1,
[pid 17880] <... clock_gettime resumed> {1324410971, 479069000}) = 0
[pid 17880] futex(0xcbed9cc, FUTEX_WAIT_PRIVATE, 93, {296, 897222000}
[pid 17492] <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 17970
[pid 17492] futex(0xcbed438, FUTEX_WAIT_PRIVATE, 2, NULL) = 0
[pid 17492] futex(0xcbed438, FUTEX_WAKE_PRIVATE, 1) = 0
[pid 17492] sched_yield() = 0
[pid 17492] clock_gettime(CLOCK_REALTIME, {1324410986, 650424000}) = 0
[pid 17492] select(9, NULL, [8], NULL, NULL) = 1 (out [8])

--- ps fax after parent resume
17492 ? Sl 0:12 resque-1.17.1: Waiting for test_job
17880 ? Sl 0:00 \_ resque-1.17.1: Processing test_job since 1324410958
```