Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Graceful close on shared bound endpoints. (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Mar 25, 2024
1 parent 262e295 commit dad2651
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
13 changes: 3 additions & 10 deletions lib/async/io/shared_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,9 @@ def bind
task = Async::Task.current

@wrappers.each do |server|
server = server.dup

task.async do |task|
task.annotate "binding to #{server.inspect}"

begin
yield server, task
ensure
server.close
end
yield server, task
end
end
end
Expand All @@ -109,9 +102,9 @@ def connect
end
end

def accept(backlog = nil, &block)
def accept(backlog = nil, **options, &block)
bind do |server|
server.accept_each(&block)
server.accept_each(**options, &block)
end
end

Expand Down
37 changes: 36 additions & 1 deletion spec/async/io/shared_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
include_context Async::RSpec::Reactor

describe '#bound' do
let(:endpoint) {Async::IO::Endpoint.udp("localhost", 5123, timeout: 10)}
let(:endpoint) {Async::IO::Endpoint.tcp("localhost", 5123, timeout: 10)}

it "can bind to shared endpoint" do
bound_endpoint = described_class.bound(endpoint)
Expand All @@ -33,6 +33,41 @@

bound_endpoint.close
end

it "can close a bound endpoint to terminate accept loop" do
bound_endpoint = described_class.bound(endpoint)
expect(bound_endpoint.wrappers).to_not be_empty

server_task = Async do
bound_endpoint.accept do |io|
io.close
end
end

connect = proc do
endpoint.connect do |io|
io.write "Hello World"
io.close
end
end

connect.call

wrapper = bound_endpoint.wrappers.first
expect(wrapper).to be_a Async::IO::Socket

bound_endpoint.close
expect(wrapper).to be_closed

expect do
begin
# Either ECONNRESET or ECONNREFUSED can be raised here.
connect.call
rescue Errno::ECONNRESET
raise Errno::ECONNREFUSED
end
end.to raise_error(Errno::ECONNREFUSED)
end
end

describe '#connected' do
Expand Down

0 comments on commit dad2651

Please sign in to comment.