CTest and fork()
If you run the application and test it using ctest, you get some really messed up results:
auto
main() -> int
{
fmt::print("line: {}\n", __LINE__);
int exit_status;
if (fork() == 0) {
fmt::print("line: {}\n", __LINE__);
std::exit(0);
}
else {
fmt::print("line: {}\n", __LINE__);
wait(&exit_status);
fmt::print("line: {}\n", __LINE__);
}
if (fork() == 0) {
fmt::print("line: {}\n", __LINE__);
std::exit(1);
}
else {
fmt::print("line: {}\n", __LINE__);
wait(&exit_status);
fmt::print("line: {}\n", __LINE__);
}
return 0;
}
This outputs:
line: 30
line: 34
line: 30 <-- this should not be possible
line: 38
line: 40
line: 44
line: 30 <-- the insanity happens more than once
line: 38
line: 40
line: 48
line: 50
If I run this without CTest, I get the following as I would expect
line: 30
line: 38
line: 34
line: 40
line: 48
line: 44
line: 50
I have no idea what CTest is doing to cause the application to execute from the top of the application more than once, but something is really broken in CTest.