-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a FATAL message upon fork()-ing once tensorstore has started any …
…threads. PiperOrigin-RevId: 716823316 Change-Id: Ifcf976769fdbbd9fc2af8f4d79718055c33b0a3d
- Loading branch information
1 parent
2140adf
commit 3174dab
Showing
6 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2025 The TensorStore Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if !defined(_WIN32) && !defined(__APPLE__) && defined(__has_include) | ||
#if __has_include(<pthread.h>) | ||
#define TENSORSTORE_INTERNAL_ENABLE_FORK_TEST 1 | ||
// In order to run the death test, both pthreads and fork are required, | ||
// and the test must be run in gunit's thread-safe mode. | ||
#endif | ||
#endif | ||
|
||
#if defined(TENSORSTORE_INTERNAL_ENABLE_FORK_TEST) | ||
|
||
#include <pthread.h> | ||
#include <unistd.h> | ||
|
||
#include <gtest/gtest.h> | ||
#include "tensorstore/internal/thread/thread.h" | ||
|
||
namespace { | ||
|
||
TEST(ThreadDeathTest, Fork) { | ||
GTEST_FLAG_SET(death_test_style, "threadsafe"); | ||
// Span a thread and join it; this should register pthread_at_fork() | ||
// handler which will crash if fork() is called. | ||
int x = 0; | ||
tensorstore::internal::Thread my_thread({}, [&x]() { x = 1; }); | ||
my_thread.Join(); | ||
EXPECT_EQ(1, x); | ||
|
||
// fork()-ing after starting a thread is not supported; forcibly crash. | ||
EXPECT_DEATH(fork(), ""); | ||
} | ||
|
||
} // namespace | ||
|
||
#endif // TENSORSTORE_INTERNAL_ENABLE_FORK_TEST |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2025 The TensorStore Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if !defined(_WIN32) | ||
#if defined(__has_include) | ||
#if __has_include(<pthread.h>) | ||
#define TENSORSTORE_INTERNAL_USE_PTHREAD | ||
#include <pthread.h> | ||
#endif | ||
#endif | ||
#endif | ||
|
||
#include <cstdio> | ||
#include <cstdlib> | ||
|
||
#include "absl/base/call_once.h" | ||
|
||
namespace tensorstore { | ||
namespace internal { | ||
namespace { | ||
|
||
[[noreturn]] void LogFatalOnFork() { | ||
std::fprintf(stderr, | ||
"aborting: fork() is not allowed since tensorstore uses " | ||
"internal threading\n"); | ||
std::fflush(stderr); | ||
std::abort(); | ||
} | ||
|
||
} // namespace | ||
|
||
void SetupLogFatalOnFork() { | ||
#if defined(TENSORSTORE_INTERNAL_USE_PTHREAD) | ||
static absl::once_flag g_setup_pthread; | ||
absl::call_once(g_setup_pthread, &pthread_atfork, LogFatalOnFork, nullptr, | ||
nullptr); | ||
#endif | ||
} | ||
|
||
} // namespace internal | ||
} // namespace tensorstore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters