forked from ruby-amqp/bunny
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid race between multi-message ack and publish
PR ruby-amqp#617 introduced an optimization for multi-message acks but failed to protect @unconfirmed_set. In edge cases #basic_publish would attempt to modify it during iteration with Enumerable#min: <ruby>/lib/ruby/3.1.0/set.rb:522:in `add': can't add a new key into hash during iteration (RuntimeError) from <bunny>/lib/bunny/channel.rb:555:in `block in basic_publish' from <bunny>/lib/bunny/channel.rb:554:in `synchronize' from <bunny>/lib/bunny/channel.rb:554:in `basic_publish' from <bunny>/lib/bunny/exchange.rb:141:in `publish'
- Loading branch information
1 parent
138a8bf
commit ccb99a4
Showing
2 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "spec_helper" | ||
|
||
unless ENV["CI"] | ||
describe "Subscription acknowledging multi-messages" do | ||
before :all do | ||
@connection = Bunny.new(username: "bunny_gem", password: "bunny_password", | ||
vhost: "bunny_testbed", automatically_recover: false) | ||
@connection.start | ||
end | ||
|
||
let(:max_messages) { 100_000 } | ||
|
||
it "successfully completes" do | ||
body = "." | ||
|
||
ch = @connection.create_channel | ||
ch.confirm_select | ||
|
||
q = ch.queue("multi-messages") | ||
|
||
m = Mutex.new | ||
acks = 0 | ||
pubs = 0 | ||
last = Time.now | ||
|
||
q.subscribe(manual_ack: true) do |delivery_info, _, _| | ||
sleep(0) if rand < 0.01 | ||
ch.ack(delivery_info.delivery_tag) | ||
|
||
m.synchronize do | ||
acks += 1 | ||
now = Time.now | ||
if now - last > 0.5 | ||
puts "Ack multi-message: acks=#{acks} pubs=#{pubs}" | ||
last = now | ||
end | ||
end | ||
end | ||
|
||
(1..max_messages).each do | ||
q.publish(".") | ||
m.synchronize { pubs += 1 } | ||
end | ||
|
||
sleep 0.1 while acks < pubs | ||
end | ||
end | ||
end |