pitrou commented on code in PR #49462:
URL: https://github.com/apache/arrow/pull/49462#discussion_r3071794432
##########
cpp/src/arrow/util/thread_pool.cc:
##########
@@ -630,9 +631,67 @@ void ThreadPool::CollectFinishedWorkersUnlocked() {
state_->finished_workers_.clear();
}
+// MinGW's __emutls implementation for C++ thread_local has known race
conditions
+// during thread creation. When a new worker thread is spawned and immediately
+// writes to a thread_local variable (here: current_thread_pool_), __emutls may
+// not have finished initializing TLS for that thread, causing it to
dereference
+// a stale/invalid pointer and segfault. This is a known upstream GCC/MinGW
bug:
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78605
+// The crash surfaces specifically in arrow-json-test
(ReaderTest.MultipleChunksParallel)
+// because that test creates a fresh ThreadPool and immediately dispatches
work,
+// hitting the narrow startup race before __emutls can initialize. Other tests
+// that rely on the already-warmed global thread pool do not trigger this
window.
+// Use native Win32 TLS (TlsAlloc/TlsGetValue/TlsSetValue) to bypass __emutls.
+// See also: https://github.com/apache/arrow/issues/49272
+# ifdef __MINGW32__
+
+namespace {
+DWORD GetPoolTlsIndex() {
+ static DWORD index = [] {
+ DWORD i = TlsAlloc();
+ if (i == TLS_OUT_OF_INDEXES) {
+ ARROW_LOG(FATAL) << "TlsAlloc failed for thread pool TLS: "
+ << WinErrorMessage(GetLastError());
+ }
+ return i;
+ }();
+ return index;
+}
+} // namespace
+
+static ThreadPool* GetCurrentThreadPool() {
+ // Preserve the caller's last-error value while also detecting TLS failures.
+ DWORD original_error = GetLastError();
+ // Ensure a successful TlsGetValue() leaves GetLastError() == 0.
+ SetLastError(0);
+ auto* pool = static_cast<ThreadPool*>(TlsGetValue(GetPoolTlsIndex()));
+ DWORD tls_error = GetLastError();
+ if (tls_error != 0) {
+ // Restore the original error before logging a fatal TLS failure.
+ SetLastError(original_error);
Review Comment:
This doesn't seem useful, since we're going to abort the process anyway.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]