Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delivering response promises in actor state destructors #12

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions libcaf_core/src/response_promise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,32 @@ void response_promise::state::cancel() {

void response_promise::state::deliver_impl(message msg) {
CAF_LOG_TRACE(CAF_ARG(msg));
// Even though we are holding a weak pointer, we can access the pointer
// without any additional check here because only the actor itself is allowed
// to call this function.
auto self = static_cast<local_actor*>(weak_self.get()->get());
auto cancel_guard = detail::make_scope_guard([this] {
cancel();
});
if (msg.empty() && id.is_async()) {
CAF_LOG_DEBUG("drop response: empty response to asynchronous input");
} else if (!stages.empty()) {
return;
}
auto self = weak_self.lock();
if (self == nullptr) {
auto element = make_mailbox_element(self, id.response_id(),
std::move(stages),
std::move(msg));
source->enqueue(std::move(element), nullptr);
dominiklohmann marked this conversation as resolved.
Show resolved Hide resolved
return;
}
auto local_self = static_cast<local_actor*>(weak_self.get()->get());
dominiklohmann marked this conversation as resolved.
Show resolved Hide resolved
if (!stages.empty()) {
auto next = std::move(stages.back());
stages.pop_back();
detail::profiled_send(self, std::move(source), next, id, std::move(stages),
self->context(), std::move(msg));
} else if (source != nullptr) {
detail::profiled_send(self, self->ctrl(), source, id.response_id(),
forwarding_stack{}, self->context(), std::move(msg));
}
cancel();
detail::profiled_send(local_self, std::move(source), next, id, std::move(stages),
local_self->context(), std::move(msg));
return;
}
CAF_ASSERT(source != nullptr);
dominiklohmann marked this conversation as resolved.
Show resolved Hide resolved
detail::profiled_send(local_self, local_self->ctrl(), source, id.response_id(),
forwarding_stack{}, local_self->context(), std::move(msg));
}

void response_promise::state::delegate_impl(abstract_actor* receiver,
Expand Down
Loading