Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions spec/cable/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ describe Cable::Server do
end
end

describe "#shutdown" do
it "closes the fiber_channel so the subscribed messages fiber doesn't leak" do
Cable.reset_server
Cable.temp_config(backend_class: Cable::DevBackend) do
server = Cable.server
server.fiber_channel.closed?.should be_false

server.shutdown

server.fiber_channel.closed?.should be_true
# Sending to the closed channel must raise rather than silently leak.
expect_raises(::Channel::ClosedError) do
server.fiber_channel.send({"foo", "bar"})
end
end
Cable.reset_server
end
end

describe "#active_connections_for" do
it "accurately returns active connections for a specificic token" do
Cable.reset_server
Expand Down
5 changes: 4 additions & 1 deletion src/cable/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ module Cable
connections_to_close.each do |connection|
connection.close
end
# Close the channel so the `process_subscribed_messages` fiber stops
# blocking on `receive` and exits cleanly instead of leaking across restarts.
fiber_channel.close
end

def restart?
Expand All @@ -210,7 +213,7 @@ module Cable
private def process_subscribed_messages
server = self
spawn(name: "Cable::Server - process_subscribed_messages") do
while received = fiber_channel.receive
while received = fiber_channel.receive?
channel, message = received
if channel.starts_with?("cable_internal")
identifier = channel.split('/').last
Expand Down
Loading