Skip to content

Commit

Permalink
Fix events map initialization to prevent race conditions
Browse files Browse the repository at this point in the history
Initialization upon miss can lead to hard to debug scenarios where potentially a concurrent array will leak out but the value in the `@events` will be different.

as described here: ruby-concurrency/concurrent-ruby#970
  • Loading branch information
mensfeld authored Nov 20, 2022
1 parent e788218 commit ccccc81
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/graphql/subscriptions/action_cable_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ def initialize(serializer: Serialize, namespace: '', action_cable: ActionCable,
# A per-process map of subscriptions to deliver.
# This is provided by Rails, so let's use it
@subscriptions = Concurrent::Map.new
@events = Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new { |h2, k2| h2[k2] = Concurrent::Array.new } }
@events = Concurrent::Map.new do |h, k|
h.compute_if_absent(k) do
Concurrent::Map.new do |h2, k2|
h2.compute_if_absent(k2) { Concurrent::Array.new }
end
end
end
@action_cable = action_cable
@action_cable_coder = action_cable_coder
@serializer = serializer
Expand Down

0 comments on commit ccccc81

Please sign in to comment.