[Feat] lib/gis: Return the child exit status from G_popen_close
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.
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:
-
What to return when there was no child (
state->pid == -1, orfpnever opened).pclose()returns -1 with errno set. But -1 is also whatG_wait()returns whenwaitpidfails, andWTERMSIGvalues 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? -
Raw vs decoded status.
pclose()returns the rawwaitstatus, which callers then run throughWIFEXITED/WEXITSTATUS.G_wait()already decodes it. PassingG_wait()'s value straight through keeps it consistent with the rest of the library but means it is not interchangeable with apclose()result. I'd lean toward matchingG_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.
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.