grass icon indicating copy to clipboard operation
grass copied to clipboard

[Feat] lib/gis: Return the child exit status from G_popen_close

Open wenzeslaus opened this issue 4 months ago • 2 comments

G_popen_close() waits for the child process but discards the exit status and returns void, so callers of G_popen_read()/G_popen_write() cannot tell whether the child succeeded. POSIX pclose() returns the status for this reason.

Details (analysis generated with Claude Code)

lib/gis/popen.c:

void G_popen_close(struct Popen *state)
{
    if (state->fp)
        fclose(state->fp);

    if (state->pid != -1)
        G_wait(state->pid);
}

The value of G_wait() is dropped. Returning it (with something distinguishable when there was no child) lets callers check the child, like pclose() does.

This is one of the links that made #7734 silent: r.coin runs r.stats through G_popen_read(), and when r.stats failed, the exit status was discarded here, so r.coin printed an empty table and exited with success. Analysis in #7732.

wenzeslaus avatar Jul 16 '26 15:07 wenzeslaus

I'd like to work on this.

Some of what it touches I've already run into from the caller side: #7841 fixes the r.coin half of #7734, but I left the exit status check out of it precisely because it is this lib/gis change.

G_wait() already returns a usable status on both platforms — decoded WEXITSTATUS/WTERMSIG on POSIX, the process exit code on Windows — so the value is simply dropped at the call in G_popen_close(). I checked the 33 call sites of G_popen_close() outside popen.c and none of them use a return value today, so changing void to int should not break any existing caller.

Two things I'd rather agree on before writing it, since they are API decisions:

  1. What to return when there was no child (state->pid == -1, or fp never opened). pclose() returns -1 with errno set. But -1 is also what G_wait() returns when waitpid fails, and WTERMSIG values are small positives, so a plain -1 conflates "no child" with "wait failed". Would you prefer a distinct sentinel for the no-child case, or is collapsing them acceptable?

  2. Raw vs decoded status. pclose() returns the raw wait status, which callers then run through WIFEXITED/WEXITSTATUS. G_wait() already decodes it. Passing G_wait()'s value straight through keeps it consistent with the rest of the library but means it is not interchangeable with a pclose() result. I'd lean toward matching G_wait() for consistency — does that match what you had in mind?

Happy to go either way; just want to get the contract right before touching a public signature.

Pranav-error avatar Aug 24 '26 15:08 Pranav-error

Opened #7847 as a draft — easier to react to concrete code than to my two questions in the abstract.

I went with G_POPEN_NO_CHILD (-2) for the no-child case, since it sits outside the set G_wait() can return, and passed G_wait()'s decoded value straight through for consistency with the rest of the library. Both are easy to change.

It also clears fp and pid once handled, otherwise a second close waits on a reaped pid and reports a failure for a pipe that closed fine — that only becomes visible once the value is returned. Happy to drop that part if you'd rather keep the diff minimal.

Pranav-error avatar Aug 24 '26 19:08 Pranav-error