@@ -166,22 +166,35 @@ void response_promise::state::cancel() {
166
166
167
167
void response_promise::state::deliver_impl (message msg) {
168
168
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
+ });
173
172
if (msg.empty () && id.is_async ()) {
174
173
CAF_LOG_DEBUG (" drop response: empty response to asynchronous input" );
175
- } else if (!stages.empty ()) {
174
+ return ;
175
+ }
176
+ if (source == nullptr ) {
177
+ CAF_LOG_DEBUG (" drop response: source is nullptr" );
178
+ return ;
179
+ }
180
+ auto self = weak_self.lock ();
181
+ if (self == nullptr ) {
182
+ auto element = make_mailbox_element (self, id.response_id (),
183
+ std::move (stages),
184
+ std::move (msg));
185
+ source->enqueue (std::move (element), nullptr );
186
+ return ;
187
+ }
188
+ auto local_self = static_cast <local_actor*>(self->get ());
189
+ if (!stages.empty ()) {
176
190
auto next = std::move (stages.back ());
177
191
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 ();
192
+ detail::profiled_send (local_self, std::move (source), next, id, std::move (stages),
193
+ local_self->context (), std::move (msg));
194
+ return ;
195
+ }
196
+ detail::profiled_send (local_self, local_self->ctrl (), source, id.response_id (),
197
+ forwarding_stack{}, local_self->context (), std::move (msg));
185
198
}
186
199
187
200
void response_promise::state::delegate_impl (abstract_actor* receiver,
0 commit comments