Skip to content

Commit a1adc36

Browse files
committed
Fix late operations in Aws
1 parent 9f50260 commit a1adc36

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Series.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3296,11 +3296,14 @@ namespace internal
32963296
// we must not throw in a destructor
32973297
try
32983298
{
3299+
// The order of operations is important:
3300+
// close() might need to wait for a number of remaining Aws
3301+
// operations to finish, so the AwsAPI needs to stay open for that.
3302+
close();
32993303
if (m_manageAwsAPI.has_value())
33003304
{
33013305
Aws::ShutdownAPI(*m_manageAwsAPI);
33023306
}
3303-
close();
33043307
}
33053308
catch (std::exception const &ex)
33063309
{

src/toolkit/Aws.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ namespace openPMD::internal
3838
{
3939
void AwsAsyncCounter::wait()
4040
{
41-
// std::cerr << "Waiting for remaining tasks. Have " << completion_counter
42-
// << " of " << request_counter << std::endl;
4341
size_t target = this->request_counter;
4442
std::unique_lock lk(this->mutex);
4543
this->event.wait(
4644
lk, [this, target]() { return this->completion_counter >= target; });
47-
// std::cerr << "Finished waiting for remaining tasks" << std::endl;
4845
}
4946

5047
void AwsAsyncCounter::add_task()
@@ -62,7 +59,10 @@ void AwsAsyncCounter::add_and_notify_result()
6259

6360
AwsAsyncCounter::~AwsAsyncCounter()
6461
{
62+
std::cerr << "Waiting for remaining tasks. Have " << completion_counter
63+
<< " of " << request_counter << std::endl;
6564
this->wait();
65+
std::cerr << "Finished waiting for remaining tasks" << std::endl;
6666
}
6767

6868
ExternalBlockStorageAws::ExternalBlockStorageAws(
@@ -89,7 +89,11 @@ ExternalBlockStorageAws::ExternalBlockStorageAws(
8989
std::cout << "Bucket created: " << m_bucketName << std::endl;
9090
}
9191
}
92-
ExternalBlockStorageAws::~ExternalBlockStorageAws() = default;
92+
ExternalBlockStorageAws::~ExternalBlockStorageAws()
93+
{
94+
// We need to wait for late operations before doing anything else.
95+
m_async.reset();
96+
}
9397

9498
auto ExternalBlockStorageAws::put(
9599
std::string const &identifier, auxiliary::WriteBuffer data, size_t len)
@@ -130,14 +134,17 @@ auto ExternalBlockStorageAws::put(
130134
auto &async_counter = *std::visit(
131135
auxiliary::overloaded{
132136
[this](auxiliary::WriteBuffer::CopyableUniquePtr const &) {
137+
std::cout << "Using unique pointer" << std::endl;
133138
return &this->m_async->unique_ptr_operations;
134139
},
135140
[this](auxiliary::WriteBuffer::SharedPtr const &) {
141+
std::cout << "Using shared pointer" << std::endl;
136142
return &this->m_async->shared_ptr_operations;
137143
}},
138144
data.as_variant<auxiliary::WriteBufferTypes>());
139145
auto responseReceivedHandler =
140146
[&async_counter,
147+
sanitized,
141148
/*
142149
* Need to keep buffers alive until they have been asynchronously
143150
* read. Use the closure captures for this. Wrap the WriteBuffer
@@ -159,8 +166,8 @@ auto ExternalBlockStorageAws::put(
159166
}
160167
else
161168
{
162-
std::cerr << "Asynchronous upload failed: "
163-
<< put_outcome.GetError().GetMessage()
169+
std::cerr << "Asynchronous upload failed for '" << sanitized
170+
<< "': " << put_outcome.GetError().GetMessage()
164171
<< std::endl;
165172
}
166173
async_counter.add_and_notify_result();

0 commit comments

Comments
 (0)