Skip to content

Commit 3174dab

Browse files
laramielcopybara-github
authored andcommitted
Add a FATAL message upon fork()-ing once tensorstore has started any threads.
PiperOrigin-RevId: 716823316 Change-Id: Ifcf976769fdbbd9fc2af8f4d79718055c33b0a3d
1 parent 2140adf commit 3174dab

File tree

6 files changed

+129
-10
lines changed

6 files changed

+129
-10
lines changed

tensorstore/internal/thread/BUILD

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ tensorstore_cc_test(
4545

4646
tensorstore_cc_library(
4747
name = "thread",
48-
srcs = ["thread.cc"],
48+
srcs = [
49+
"thread.cc",
50+
"thread_on_fork.cc",
51+
],
4952
hdrs = ["thread.h"],
5053
deps = [
54+
"@com_google_absl//absl/base",
5155
"@com_google_absl//absl/functional:any_invocable",
5256
"@com_google_absl//absl/log:absl_check",
5357
],
@@ -62,6 +66,15 @@ tensorstore_cc_test(
6266
],
6367
)
6468

69+
tensorstore_cc_test(
70+
name = "thread_death_test",
71+
srcs = ["thread_death_test.cc"],
72+
deps = [
73+
":thread",
74+
"@com_google_googletest//:gtest_main",
75+
],
76+
)
77+
6578
THREAD_POOL_DEFINES = []
6679

6780
THREAD_POOL_DEPS = []

tensorstore/internal/thread/thread.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#if defined(__linux__) || defined(__APPLE__)
15+
#if defined(__has_include)
16+
#if __has_include(<pthread.h>)
17+
#define TENSORSTORE_INTERNAL_USE_PTHREAD
1618
#include <pthread.h>
1719
#endif
20+
#endif
1821

1922
#include <thread> // NOLINT
2023
#include <type_traits>
2124

2225
namespace tensorstore {
2326
namespace internal {
2427

25-
/// See
26-
/// https://stackoverflow.com/questions/2369738/how-to-set-the-name-of-a-thread-in-linux-pthreads
27-
2828
void TrySetCurrentThreadName(const char* name) {
2929
if (name == nullptr) return;
30-
#if defined(__linux__)
31-
pthread_setname_np(pthread_self(), name);
32-
#endif
30+
#if defined(TENSORSTORE_INTERNAL_USE_PTHREAD)
3331
#if defined(__APPLE__)
3432
pthread_setname_np(name);
33+
#else
34+
pthread_setname_np(pthread_self(), name);
35+
#endif
3536
#endif
3637
// TODO: Add windows via SetThreadDescription()
3738
}

tensorstore/internal/thread/thread.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ namespace internal {
2929
/// Helper functions to set the thread name.
3030
void TrySetCurrentThreadName(const char* name);
3131

32+
/// Helper function to log a fatal error if fork() is called.
33+
void SetupLogFatalOnFork();
34+
3235
// Tensorstore-specific Thread class to be used instead of std::thread.
3336
// This exposes a limited subset of the std::thread api.
3437
class Thread {
@@ -89,12 +92,13 @@ class Thread {
8992
// factory method.
9093
template <class Function, class... Args>
9194
Thread(private_t, Options options, Function&& f, Args&&... args)
92-
: thread_(
95+
: thread_((
96+
SetupLogFatalOnFork(),
9397
[name = options.name, fn = std::bind(std::forward<Function>(f),
9498
std::forward<Args>(args)...)] {
9599
TrySetCurrentThreadName(name);
96100
std::move(fn)();
97-
}) {}
101+
})) {}
98102

99103
std::thread thread_;
100104
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 The TensorStore Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if !defined(_WIN32) && !defined(__APPLE__) && defined(__has_include)
16+
#if __has_include(<pthread.h>)
17+
#define TENSORSTORE_INTERNAL_ENABLE_FORK_TEST 1
18+
// In order to run the death test, both pthreads and fork are required,
19+
// and the test must be run in gunit's thread-safe mode.
20+
#endif
21+
#endif
22+
23+
#if defined(TENSORSTORE_INTERNAL_ENABLE_FORK_TEST)
24+
25+
#include <pthread.h>
26+
#include <unistd.h>
27+
28+
#include <gtest/gtest.h>
29+
#include "tensorstore/internal/thread/thread.h"
30+
31+
namespace {
32+
33+
TEST(ThreadDeathTest, Fork) {
34+
GTEST_FLAG_SET(death_test_style, "threadsafe");
35+
// Span a thread and join it; this should register pthread_at_fork()
36+
// handler which will crash if fork() is called.
37+
int x = 0;
38+
tensorstore::internal::Thread my_thread({}, [&x]() { x = 1; });
39+
my_thread.Join();
40+
EXPECT_EQ(1, x);
41+
42+
// fork()-ing after starting a thread is not supported; forcibly crash.
43+
EXPECT_DEATH(fork(), "");
44+
}
45+
46+
} // namespace
47+
48+
#endif // TENSORSTORE_INTERNAL_ENABLE_FORK_TEST
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 The TensorStore Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if !defined(_WIN32)
16+
#if defined(__has_include)
17+
#if __has_include(<pthread.h>)
18+
#define TENSORSTORE_INTERNAL_USE_PTHREAD
19+
#include <pthread.h>
20+
#endif
21+
#endif
22+
#endif
23+
24+
#include <cstdio>
25+
#include <cstdlib>
26+
27+
#include "absl/base/call_once.h"
28+
29+
namespace tensorstore {
30+
namespace internal {
31+
namespace {
32+
33+
[[noreturn]] void LogFatalOnFork() {
34+
std::fprintf(stderr,
35+
"aborting: fork() is not allowed since tensorstore uses "
36+
"internal threading\n");
37+
std::fflush(stderr);
38+
std::abort();
39+
}
40+
41+
} // namespace
42+
43+
void SetupLogFatalOnFork() {
44+
#if defined(TENSORSTORE_INTERNAL_USE_PTHREAD)
45+
static absl::once_flag g_setup_pthread;
46+
absl::call_once(g_setup_pthread, &pthread_atfork, LogFatalOnFork, nullptr,
47+
nullptr);
48+
#endif
49+
}
50+
51+
} // namespace internal
52+
} // namespace tensorstore

tensorstore/util/status.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ absl::Status MaybeAnnotateStatusImpl(absl::Status source,
9090
SourceLocation loc) {
9191
std::fprintf(stderr, "%s:%d: %s: %s\n", loc.file_name(), loc.line(), message,
9292
status.ToString().c_str());
93+
std::fflush(stderr);
9394
std::terminate();
9495
}
9596

0 commit comments

Comments
 (0)