Skip to content

Commit

Permalink
Wrap application code in Rails executor to prevent autoloading proble…
Browse files Browse the repository at this point in the history
…ms (#50)

Broadcasting happens from a thread and involves a `SolidCable::Message` model
that is auto-reloaded by Rails in development. This can result in race conditions
where the ActionCable thread tries to access the model class but it can't resolve
it.

https://guides.rubyonrails.org/threading_and_code_execution.html
  • Loading branch information
jorgemanrubia authored Dec 5, 2024
1 parent c696aaa commit 1459262
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/action_cable/subscription_adapter/solid_cable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ def channels
end

def broadcast_messages
::SolidCable::Message.broadcastable(channels, last_id).
each do |message|
broadcast(message.channel, message.payload)
self.last_id = message.id
Rails.application.reloader.wrap do
::SolidCable::Message.broadcastable(channels, last_id).
each do |message|
broadcast(message.channel, message.payload)
self.last_id = message.id
end
end
end

def with_polling_volume
Expand Down
10 changes: 10 additions & 0 deletions test/config_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def initialize(**)
def config_for(_file)
@config
end

def reloader
@reloader ||= ReloaderStub.new
end

class ReloaderStub
def wrap(&block)
block.call
end
end
end

def with_cable_config(**)
Expand Down

0 comments on commit 1459262

Please sign in to comment.