Skip to content

Commit 827ce03

Browse files
Fix delivering response promises in actor state destructors
1 parent 261a6b3 commit 827ce03

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

libcaf_core/src/response_promise.cpp

+22-12
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,32 @@ void response_promise::state::cancel() {
166166

167167
void response_promise::state::deliver_impl(message msg) {
168168
CAF_LOG_TRACE(CAF_ARG(msg));
169-
// Even though we are holding a weak pointer, we can access the pointer
170-
// without any additional check here because only the actor itself is allowed
171-
// to call this function.
172-
auto self = static_cast<local_actor*>(weak_self.get()->get());
169+
auto cancel_guard = detail::make_scope_guard([this] {
170+
cancel();
171+
});
173172
if (msg.empty() && id.is_async()) {
174173
CAF_LOG_DEBUG("drop response: empty response to asynchronous input");
175-
} else if (!stages.empty()) {
174+
return;
175+
}
176+
auto self = weak_self.lock();
177+
if (self == nullptr) {
178+
auto element = make_mailbox_element(self, id.response_id(),
179+
std::move(stages),
180+
std::move(msg));
181+
source->enqueue(std::move(element), nullptr);
182+
return;
183+
}
184+
auto local_self = static_cast<local_actor*>(weak_self.get()->get());
185+
if (!stages.empty()) {
176186
auto next = std::move(stages.back());
177187
stages.pop_back();
178-
detail::profiled_send(self, std::move(source), next, id, std::move(stages),
179-
self->context(), std::move(msg));
180-
} else if (source != nullptr) {
181-
detail::profiled_send(self, self->ctrl(), source, id.response_id(),
182-
forwarding_stack{}, self->context(), std::move(msg));
183-
}
184-
cancel();
188+
detail::profiled_send(local_self, std::move(source), next, id, std::move(stages),
189+
local_self->context(), std::move(msg));
190+
return;
191+
}
192+
CAF_ASSERT(source != nullptr);
193+
detail::profiled_send(local_self, local_self->ctrl(), source, id.response_id(),
194+
forwarding_stack{}, local_self->context(), std::move(msg));
185195
}
186196

187197
void response_promise::state::delegate_impl(abstract_actor* receiver,

0 commit comments

Comments
 (0)