I’m failing on Background Jobs - Reap one job #MA9 on the tester. When I try the tester, the jobs reads as getting terminated when it should still be running. Locally, the same series of events works as expected and matches the expected results:
Tester logs:
[tester::#MA9] Running tests for Stage #MA9 (Background Jobs - Reap one job)
[tester::#MA9] Running ./your_program.sh
[tester::#MA9] [setup] mkfifo /tmp/raspberry-27
[your-program] $ cat /tmp/raspberry-27 &
[your-program] [1] 62
[tester::#MA9] ✓ Found process with PID 62
[tester::#MA9] ✓ Expected executable path found for process with PID 62
[tester::#MA9] ✓ Output includes job number with PID
[your-program] $ jobs
[your-program] [1]+ Done cat /tmp/raspberry-27 &
[tester::#MA9] ^ Line does not match expected value.
[tester::#MA9] Expected: "[1]+ Running cat /tmp/raspberry-27 &"
[tester::#MA9] Received: "[1]+ Done cat /tmp/raspberry-27 &"
[your-program] $
[tester::#MA9] Test failed
My shell:
$ mkfifo /tmp/test-fifo
$ cat /tmp/test-fifo &
[1] 11868
$ jobs
[1]+ Running cat /tmp/test-fifo &
$ jobs
[1]+ Running cat /tmp/test-fifo &
$ jobs
[1]+ Running cat /tmp/test-fifo &
The way I determine if a job is finished:
int status;
waitpid(pid, &status, WNOHANG); // Check without waiting to finish
if (WIFEXITED(status)) {
// Add job to data structure containing finished jobs
}
Does anyone have any advice on how to trouble shoot this? The logs aren’t helpful when doing the exact same thing locally works as expected. If it matters, I am running this locally on macos
After a while, adding this before looping over the job processes solved my problem (JS Spoilers):
await new Promise(resolve => setImmediate(resolve));
Explanation: It was a race condition where the command was being executed before nodejs event loop reached the “poll” step, thus, looping over job process before the trigger for the callback for ‘exit’. This code forces nodejs to execute the event loop (until “check” step, which is after “poll” step) before looping over the job processes.
My solution doesnt apply for your code, but perhaps a race conditon may be happening there.