Skip to content

Commit e3bbfbf

Browse files
author
Valtteri Heikkila
committed
Fixed linux_client leak. It was caused by cancellation registration lambda that took shared ownership of the request context object.
1 parent 254ebcd commit e3bbfbf

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Release/src/http/client/http_linux.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,18 @@ namespace web { namespace http
431431
// Register for notification on cancellation to abort this request.
432432
if(request_ctx->m_request._cancellation_token() != pplx::cancellation_token::none())
433433
{
434-
ctx->m_cancellationRegistration = request_ctx->m_request._cancellation_token().register_callback([ctx]()
434+
// weak_ptr prevents lambda from taking shared ownership of the context.
435+
// Otherwise context replacement in the handle_status_line() would leak the objects.
436+
std::weak_ptr<linux_client_request_context> ctx_weak(ctx);
437+
ctx->m_cancellationRegistration = request_ctx->m_request._cancellation_token().register_callback([ctx_weak]()
435438
{
436-
// Cancel operations and all asio async handlers.
437-
ctx->m_connection->cancel();
438-
// Shut down transmissions, close the socket and prevent connection from being pooled.
439-
ctx->m_connection->close();
439+
if (auto ctx_lock = ctx_weak.lock())
440+
{
441+
// Cancel operations and all asio async handlers.
442+
ctx_lock->m_connection->cancel();
443+
// Shut down transmissions, close the socket and prevent connection from being pooled.
444+
ctx_lock->m_connection->close();
445+
}
440446
});
441447
}
442448
}

0 commit comments

Comments
 (0)