@@ -166,22 +166,32 @@ 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
+ 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 ()) {
176
186
auto next = std::move (stages.back ());
177
187
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) );
185
195
}
186
196
187
197
void response_promise::state::delegate_impl (abstract_actor* receiver,
0 commit comments