Skip to content

Commit f8adc32

Browse files
authored
If an exception happens during initial setup ensure everything is cleaned up properly to avoid a memory leak. Fixes #97 (#101)
1 parent e230aef commit f8adc32

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/cable/handler.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ module Cable
1616
end
1717

1818
ws = HTTP::WebSocketHandler.new do |socket, ws_ctx|
19+
connection_id : String? = nil
20+
ws_pinger : Cable::WebsocketPinger? = nil
21+
1922
connection = T.new(ws_ctx.request, socket)
2023

2124
next if connection.closed? || connection.connection_rejected?
@@ -76,6 +79,11 @@ module Cable
7679
Cable::Logger.info { "Finished \"#{path}\" [WebSocket] for #{remote_address} at #{Time.utc}" }
7780
end
7881
rescue e : Exception
82+
# Cleanup everything after the exception to prevent memory leak
83+
ws_pinger.try(&.stop)
84+
if conn_id = connection_id
85+
Cable.server.remove_connection(conn_id)
86+
end
7987
Cable.settings.on_error.call(e, "Cable::Handler#call -> HTTP::WebSocketHandler")
8088
raise e
8189
end

0 commit comments

Comments
 (0)