From d193c0e74293cb1c149a34678a27db08940ae87d Mon Sep 17 00:00:00 2001 From: Maciej Mensfeld Date: Sun, 20 Nov 2022 21:19:25 +0100 Subject: [PATCH] Make registry fully thread safe Without this change potential incrementation can "go away" and can actually mismatch by 1 if run in multiple threads the same time. This can lead to super weird errors where counter is not as expected (been there, took me ages to debug). ref: https://github.com/ruby-concurrency/concurrent-ruby/issues/970 --- lib/rom/factory/sequences.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rom/factory/sequences.rb b/lib/rom/factory/sequences.rb index a4aabe1..9a76ce2 100644 --- a/lib/rom/factory/sequences.rb +++ b/lib/rom/factory/sequences.rb @@ -29,7 +29,7 @@ def next(key) # @api private def reset - @registry = Concurrent::Map.new { |h, k| h[k] = 0 } + @registry = Concurrent::Map.new { |h, k| h.compute_if_absent(k) { 0 } } self end end