diff --git a/src/IO/ADIOS/ADIOS2IOHandler.cpp b/src/IO/ADIOS/ADIOS2IOHandler.cpp index 0ee10a77c4..2f5a698289 100644 --- a/src/IO/ADIOS/ADIOS2IOHandler.cpp +++ b/src/IO/ADIOS/ADIOS2IOHandler.cpp @@ -708,15 +708,19 @@ void ADIOS2IOHandlerImpl::createFile( VERIFY(success, "[ADIOS2] Could not create directory."); } + // enforce opening the file + // lazy opening is deathly in parallel situations + auto &fileData = + getFileData(shared_name, IfFileNotOpen::CreateImplicitly); + + // only emplace the file into our structures after it has been + // successfully opened. otherwise, errors will lead to undefined state. + associateWithFile(writable, shared_name); this->m_dirty.emplace(shared_name); writable->written = true; writable->abstractFilePosition = std::make_shared(); - // enforce opening the file - // lazy opening is deathly in parallel situations - auto &fileData = - getFileData(shared_name, IfFileNotOpen::CreateImplicitly); if (!printedWarningsAlready.noGroupBased && m_writeAttributesFromThisRank && @@ -1028,14 +1032,8 @@ void ADIOS2IOHandlerImpl::openFile( } std::string name = parameters.name + fileSuffix(); - auto file = std::get(getPossiblyExisting(name)); - associateWithFile(writable, file); - - writable->written = true; - writable->abstractFilePosition = std::make_shared(); - auto how_to_open = [&]() { switch (parameters.reopen) { @@ -1050,8 +1048,18 @@ void ADIOS2IOHandlerImpl::openFile( }(); // enforce opening the file - // lazy opening is deathly in parallel situations + // lazy opening is deadly in parallel situations auto &fileData = getFileData(file, how_to_open); + + // the following calls present the new file to the IO handler's data + // structures. do this only after the file has been successfully open, to + // avoid invalid state. + + associateWithFile(writable, file); + + writable->written = true; + writable->abstractFilePosition = std::make_shared(); + *parameters.out_parsePreference = fileData.parsePreference; m_dirty.emplace(std::move(file)); }