Background Jobs - Reap one job #MA9

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

I’m stuck on same problem, I even writed a script to check:


import { spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { mkdtemp } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

const fifoPath = join(tmpdir(), `blueberry-${randomUUID()}`);

function run(command, args, options = {}) {
    return spawn(command, args, {
        stdio: ['pipe', 'inherit', 'inherit'],
        ...options,
    });
}

const mkfifo = run('mkfifo', [fifoPath]);

mkfifo.on('error', (err) => {
    console.error('Failed to spawn mkfifo:', err);
    process.exitCode = 1;
});

mkfifo.on('close', (code) => {
    if (code !== 0) {
        console.error(`mkfifo exited with code ${code}`);
        process.exitCode = 1;
        return;
    }

    const program = run('bash', ['your_program.sh']);

    program.on('error', (err) => {
        console.error('Failed to spawn program:', err);
    });

    program.stdin.write(`cat ${fifoPath} &\n`);

    const writer = run('bash', [
        '-c',
        `echo -ne "" > "${fifoPath}"`,
    ]);

    writer.on('close', (writerCode) => {
        console.log('FIFO writer exited:', writerCode);

        program.stdin.write('jobs\n');
    });

    writer.on('error', (err) => {
        console.error('Failed to spawn writer:', err);
    });

});


this scripts shows that works, but when submitting, it shows that the process is still running

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.