Prevent deadlocks in death tests.
authorMartin Braenne <mboehme@google.com>
Tue, 13 Jun 2023 07:05:15 +0000 (07:05 +0000)
committerMartin Braenne <mboehme@google.com>
Thu, 15 Jun 2023 04:29:00 +0000 (04:29 +0000)
commitdfbcee286b9b96751014ebc5ba5290e42796be37
tree85576de202f4ffa817001df166dc209d14006894
parent0e08374abb2c61a3ae5c24d5f60be3b548da9778
Prevent deadlocks in death tests.

We have recently started seeing deadlocks in death tests while running in an internal test environment.

Per the documentation here, there are issues with death tests in the presence of threads:

https://github.com/google/googletest/blob/main/docs/advanced.md#death-tests-and-threads

To avoid the deadlocks, I first tried appending `DeathTest` to the relevant test suite names, which has the effect of running these test suites before all other tests. However, this did not prevent the deadlocks.

This patch therefore uses the option of setting the `death_test_style` flag to `"threadsafe"` (see description in the page linked above under "Death Test Styles"), and this prevents the deadlocks.

The documentation notes that the "threadsafe" death test style "trades increased test execution time (potentially dramatically so) for improved thread safety". This is because, to execute a death test, "threadsafe" does a "fork + exec", then re-executes the current test in the child process, whereas the default "fast" death test style does only a fork (on those platforms that support it). However, as we have relatively few death tests, the increased execution time does not make a big difference in total test execution time in my testing.

Note that other projects, such as Chromium, also choose to set the "threadsafe" death test style globally:

https://source.chromium.org/chromium/chromium/src/+/main:base/test/test_suite.cc;l=367

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D152696
third-party/unittest/UnitTestMain/TestMain.cpp