Stream: brlcad

Topic: wait and subprocess return code


view this post on Zulip starseeker (Dec 06 2023 at 01:05):

I have a rather vexing problem with the POSIX wait(&retcode) function, and I'm not sure how to zero in on it. I've defined a test for the libbu process wrappers to verify that I can detect a non-zero child exit code:

https://github.com/BRL-CAD/brlcad/blob/facetize/src/libbu/tests/process.c#L96
https://github.com/BRL-CAD/brlcad/blob/facetize/src/libbu/tests/subprocess.cpp#L62

It should return 2, but on Linux when I run this via bu_test I get a "0" return code. However, if I run the subprocess exec directly, "echo $?" does indeed report "2" for an exit code.

Even more vexing, if I put the bu_test call into gdb and break on bu_process_wait, the program behavior changes and it DOES end up with 2 as the return code.

Am I setting this up incorrectly somehow?

view this post on Zulip starseeker (Dec 06 2023 at 01:48):

OK, some progress - closing the stdout and stderr of the child process before waiting was an issue, from the looks of things.

view this post on Zulip Alexis Naveros (Dec 06 2023 at 04:15):

Perhaps not the issue, but you should only use WEXITSTATUS on a return code that passes the WIFEXITED() macro test

view this post on Zulip Alexis Naveros (Dec 06 2023 at 04:16):

And when a program behaves differently when launched in gdb than standalone, what I usually do is to put a sleep() at the top of main(), launch it standalone and attach gdb to it before the sleep() finishes

view this post on Zulip starseeker (Dec 06 2023 at 15:17):

It's looking like there are multiple issues at play... it never fails.

view this post on Zulip starseeker (Dec 06 2023 at 15:40):

I'm commencing to think it's time to dissect CTest and identify the subset of their capabilities that we need for libbu process control - a cursory inspection suggests that they're leveraging libuv and put quite a bit of work into this, so trying to custom roll our own solution without reusing their code is probably going to be both a lot of work and error prone. The core of what CTest does is almost exactly what we need bu_process to do - launch processes, support timeouts and tracking return codes, and collect output.

view this post on Zulip starseeker (Dec 06 2023 at 18:43):

Hmm. Looks like there are also a few small libraries for doing similar things. Experiment time

view this post on Zulip Christopher (Jan 10 2024 at 14:30):

starseeker said:

Hmm. Looks like there are also a few small libraries for doing similar things. Experiment time

I think I'm running into something similar.. Did anything come of your experiments?

view this post on Zulip starseeker (Jan 10 2024 at 20:40):

Yes, actually. One of them worked in the facetize branch.

view this post on Zulip starseeker (Jan 10 2024 at 20:43):

The subprocess bit is here: https://github.com/BRL-CAD/brlcad/commit/c3eefb86430b7bb85e586412d0a26bf3e1dd585c

view this post on Zulip starseeker (Jan 10 2024 at 20:43):

https://github.com/sheredom/subprocess.h

view this post on Zulip starseeker (Jan 10 2024 at 20:44):

I was focused on facetize, so I haven't audited whether it can straight up replace libbu's process bits completely

view this post on Zulip Christopher (Jan 10 2024 at 20:46):

Gotcha. I'll see if that helps the errors I'm seeing in rtweight.

view this post on Zulip Christopher (Jan 10 2024 at 20:47):

starseeker said:

I was focused on facetize, so I haven't audited whether it can straight up replace libbu's process bits completely

Should probably set up a test suite for the process stuff's eventually

view this post on Zulip starseeker (Jan 10 2024 at 20:48):

There is this: https://github.com/BRL-CAD/brlcad/blob/main/src/libbu/tests/process.c


Last updated: Oct 09 2024 at 00:44 UTC