Skip to content

Commit 39e705f

Browse files
committed
Fix shared_ptr cycle leak in websocket_connection
Add RAII guard in start_read_loop() that clears the on_message callback when the coroutine frame is destroyed. This breaks the reference cycle created when users capture shared_ptr<websocket_connection> in the callback (the standard and expected usage pattern).
1 parent 57b0db3 commit 39e705f

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

thinger/http/server/websocket_connection.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ namespace thinger::http{
3333
{
3434
co_spawn(ws_->get_io_context(),
3535
[this, self = shared_from_this()]() -> awaitable<void> {
36+
// RAII guard: clears the on_message callback when the coroutine
37+
// frame is destroyed (normal exit, exception, or io_context
38+
// teardown). This breaks shared_ptr cycles that occur when the
39+
// user captures shared_ptr<websocket_connection> in the callback.
40+
struct cycle_guard {
41+
std::function<void(std::string, bool)>& ref;
42+
~cycle_guard() { ref = nullptr; }
43+
} guard{on_frame_callback_};
44+
3645
co_await read_loop();
3746
},
3847
detached);

0 commit comments

Comments
 (0)