From 822a87623c9439ee75513c40748a786a9cc5ab5b Mon Sep 17 00:00:00 2001 From: Bradley Lowekamp Date: Fri, 4 Sep 2026 08:16:25 -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. Ported from InsightSoftwareConsortium/ITK#6833. Co-Authored-By: Claude Sonnet 5 --- src/metaForm.cxx | 2 -- src/metaObject.cxx | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/metaForm.cxx b/src/metaForm.cxx index 1eaca97..884cfec 100644 --- a/src/metaForm.cxx +++ b/src/metaForm.cxx @@ -591,8 +591,6 @@ MetaForm::ReadStream(METAIO_STREAM::ifstream * _stream) MetaForm::M_Destroy(); - fflush(nullptr); - Clear(); M_SetupReadFields(); diff --git a/src/metaObject.cxx b/src/metaObject.cxx index d21fc6e..07c3b15 100644 --- a/src/metaObject.cxx +++ b/src/metaObject.cxx @@ -357,8 +357,6 @@ MetaObject::ReadStream(int _nDims, METAIO_STREAM::ifstream * _stream) MetaObject::M_Destroy(); - fflush(nullptr); - Clear(); M_SetupReadFields();