From 183f3942e3495ef99e6edcaade94db2aa29cb986 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Thu, 3 Sep 2026 15:45:33 -0400 Subject: [PATCH] BUG: Remove unnecessary global fflush(nullptr) causing MetaIO read deadlocks MetaObject::ReadStream() and MetaForm::ReadStream() unconditionally call fflush(nullptr) before parsing, a leftover from the original MetaIO commit that predates any multithreading concerns for this library. fflush(NULL) flushes every open FILE* stream in the entire process, not just the one being read. On platforms such as macOS, this requires walking the process-wide list of all open FILE* streams and locking each one in turn. Under heavy concurrent MetaImage/MHA reads (many threads each opening/reading/closing their own files), this global flush-all creates severe lock contention that can grind concurrent reads to a halt indefinitely, since each thread's fflush(nullptr) call competes to lock every other thread's in-flight FILE* handle. Reading does not require flushing any previously written data, so this call serves no purpose and can simply be removed. Backport of the same fix on main (PR #6833). --- Modules/ThirdParty/MetaIO/src/MetaIO/src/metaForm.cxx | 2 -- Modules/ThirdParty/MetaIO/src/MetaIO/src/metaObject.cxx | 2 -- 2 files changed, 4 deletions(-) diff --git a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaForm.cxx b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaForm.cxx index 8ce1224933b..53050fd7746 100644 --- a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaForm.cxx +++ b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaForm.cxx @@ -595,8 +595,6 @@ MetaForm::ReadStream(std::ifstream * _stream) MetaForm::M_Destroy(); - fflush(nullptr); - Clear(); M_SetupReadFields(); diff --git a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaObject.cxx b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaObject.cxx index c105433ba07..8abd40e6bca 100644 --- a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaObject.cxx +++ b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaObject.cxx @@ -307,8 +307,6 @@ MetaObject::ReadStream(int _nDims, std::ifstream * _stream) MetaObject::M_Destroy(); - fflush(nullptr); - Clear(); M_SetupReadFields();