Skip to content

Commit dfbcee2

Browse files
committed
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
1 parent 0e08374 commit dfbcee2

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

third-party/unittest/UnitTestMain/TestMain.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ int main(int argc, char **argv) {
2929
true /* Disable crash reporting */);
3030
}
3131

32+
// Use the "threadsafe" test style for death tests -- the "fast" test style
33+
// can cause deadlocks.
34+
testing::GTEST_FLAG(death_test_style) = "threadsafe";
35+
3236
// Initialize both gmock and gtest.
3337
testing::InitGoogleMock(&argc, argv);
3438

0 commit comments

Comments
 (0)