@@ -708,15 +708,19 @@ void ADIOS2IOHandlerImpl::createFile(
708708 VERIFY (success, " [ADIOS2] Could not create directory." );
709709 }
710710
711+ // enforce opening the file
712+ // lazy opening is deathly in parallel situations
713+ auto &fileData =
714+ getFileData (shared_name, IfFileNotOpen::CreateImplicitly);
715+
716+ // only emplace the file into our structures after it has been
717+ // successfully opened. otherwise, errors will lead to undefined state.
718+
711719 associateWithFile (writable, shared_name);
712720 this ->m_dirty .emplace (shared_name);
713721
714722 writable->written = true ;
715723 writable->abstractFilePosition = std::make_shared<ADIOS2FilePosition>();
716- // enforce opening the file
717- // lazy opening is deathly in parallel situations
718- auto &fileData =
719- getFileData (shared_name, IfFileNotOpen::CreateImplicitly);
720724
721725 if (!printedWarningsAlready.noGroupBased &&
722726 m_writeAttributesFromThisRank &&
@@ -1028,14 +1032,8 @@ void ADIOS2IOHandlerImpl::openFile(
10281032 }
10291033
10301034 std::string name = parameters.name + fileSuffix ();
1031-
10321035 auto file = std::get<PE_InvalidatableFile>(getPossiblyExisting (name));
10331036
1034- associateWithFile (writable, file);
1035-
1036- writable->written = true ;
1037- writable->abstractFilePosition = std::make_shared<ADIOS2FilePosition>();
1038-
10391037 auto how_to_open = [&]() {
10401038 switch (parameters.reopen )
10411039 {
@@ -1050,8 +1048,18 @@ void ADIOS2IOHandlerImpl::openFile(
10501048 }();
10511049
10521050 // enforce opening the file
1053- // lazy opening is deathly in parallel situations
1051+ // lazy opening is deadly in parallel situations
10541052 auto &fileData = getFileData (file, how_to_open);
1053+
1054+ // the following calls present the new file to the IO handler's data
1055+ // structures. do this only after the file has been successfully open, to
1056+ // avoid invalid state.
1057+
1058+ associateWithFile (writable, file);
1059+
1060+ writable->written = true ;
1061+ writable->abstractFilePosition = std::make_shared<ADIOS2FilePosition>();
1062+
10551063 *parameters.out_parsePreference = fileData.parsePreference ;
10561064 m_dirty.emplace (std::move (file));
10571065}
0 commit comments