Skip to content

Commit 67dc826

Browse files
Fix: Late unique_ptr puts without CLOSE_FILE or ADVANCE operations (#1744)
* Add failing test * Add failing test * Revert "Add failing test" This reverts commit 5e04ece. * Reactivate writing from unique_ptr in finalize()
1 parent 0a7e0a7 commit 67dc826

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ if(openPMD_BUILD_TESTING)
803803
list(APPEND ${out_list}
804804
test/Files_SerialIO/close_and_reopen_test.cpp
805805
test/Files_SerialIO/filebased_write_test.cpp
806+
test/Files_SerialIO/issue_1744_unique_ptrs_at_close_time.cpp
806807
)
807808
elseif(${test_name} STREQUAL "ParallelIO" AND openPMD_HAVE_MPI)
808809
list(APPEND ${out_list}

src/IO/ADIOS/ADIOS2File.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,18 @@ void ADIOS2File::finalize()
225225
{
226226
return;
227227
}
228-
if (!m_uniquePtrPuts.empty())
229-
{
230-
throw error::Internal(
231-
"[ADIOS2 backend] Orphaned unique-ptr put operations found "
232-
"when closing file.");
233-
}
234228
// if write accessing, ensure that the engine is opened
235-
if (!m_engine && writeOnly(m_mode))
229+
// and that all datasets are written
230+
// (attributes and unique_ptr datasets are written upon closing a step
231+
// or a file which users might never do)
232+
bool needToWrite = !m_uniquePtrPuts.empty();
233+
if ((needToWrite || !m_engine) && writeOnly(m_mode))
236234
{
237235
getEngine();
236+
for (auto &entry : m_uniquePtrPuts)
237+
{
238+
entry.run(*this);
239+
}
238240
}
239241
if (m_engine)
240242
{
@@ -250,6 +252,7 @@ void ADIOS2File::finalize()
250252
m_ADIOS.RemoveIO(m_IOName);
251253
}
252254
}
255+
m_uniquePtrPuts.clear();
253256
finalized = true;
254257
}
255258

test/Files_SerialIO/SerialIOTests.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ namespace close_and_reopen_test
88
{
99
auto close_and_reopen_test() -> void;
1010
}
11+
namespace issue_1744_unique_ptrs_at_close_time
12+
{
13+
auto issue_1744_unique_ptrs_at_close_time() -> void;
14+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "SerialIOTests.hpp"
2+
3+
#include "openPMD/Dataset.hpp"
4+
#include "openPMD/openPMD.hpp"
5+
6+
#include <memory>
7+
#include <numeric>
8+
9+
// clang-format off
10+
/*
11+
* Tests regression introduced with #1743:
12+
* terminate called after throwing an instance of 'openPMD::error::Internal'
13+
* what(): Internal error: [ADIOS2 backend] Orphaned unique-ptr put operations found when closing file.
14+
* This is a bug. Please report at ' https://github.com/openPMD/openPMD-api/issues'.
15+
*/
16+
// clang-format on
17+
18+
namespace issue_1744_unique_ptrs_at_close_time
19+
{
20+
auto issue_1744_unique_ptrs_at_close_time() -> void
21+
{
22+
openPMD::Series write(
23+
"../samples/issue_1744_unique_ptrs_at_close_time.bp4",
24+
openPMD::Access::CREATE,
25+
R"({"iteration_encoding": "group_based"})");
26+
std::unique_ptr<int[]> data_unique(new int[10]);
27+
std::iota(data_unique.get(), data_unique.get() + 10, 0);
28+
auto E_x = write.snapshots()[0].meshes["E"]["x"];
29+
E_x.resetDataset({openPMD::Datatype::INT, {10}});
30+
E_x.storeChunk(std::move(data_unique), {0}, {10});
31+
write.close();
32+
}
33+
} // namespace issue_1744_unique_ptrs_at_close_time

test/SerialIOTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,14 @@ TEST_CASE("close_and_copy_attributable_test", "[serial]")
760760
}
761761
}
762762

763+
TEST_CASE("issue_1744_unique_ptrs_at_close_time", "[serial]")
764+
{
765+
#if openPMD_HAVE_ADIOS2
766+
issue_1744_unique_ptrs_at_close_time::
767+
issue_1744_unique_ptrs_at_close_time();
768+
#endif
769+
}
770+
763771
#if openPMD_HAVE_ADIOS2
764772
TEST_CASE("close_and_reopen_test", "[serial]")
765773
{

0 commit comments

Comments
 (0)