From 5e7304991da93d7fec88d4170bb846ee11677c97 Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Thu, 3 Sep 2026 15:43:12 -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. --- 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 1eaca976163..884cfec956a 100644 --- a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaForm.cxx +++ b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaForm.cxx @@ -591,8 +591,6 @@ MetaForm::ReadStream(METAIO_STREAM::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 f0f36b9d5b3..0ca694a9da6 100644 --- a/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaObject.cxx +++ b/Modules/ThirdParty/MetaIO/src/MetaIO/src/metaObject.cxx @@ -357,8 +357,6 @@ MetaObject::ReadStream(int _nDims, METAIO_STREAM::ifstream * _stream) MetaObject::M_Destroy(); - fflush(nullptr); - Clear(); M_SetupReadFields();