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

Commit

Permalink
Fix for socket file not being reused even when not bound (#70)
Browse files Browse the repository at this point in the history
In my machine, when I `Ctrl+C` (using falcon supervisor), supervisor.ipc remains and when I restart the server, I get EADDRINUSE

Found that `File.exist?(@path)` is returning false for such a file even though `ls -al` lists the file

Using `FileUtils.safe_unlink` instead of `File.unlink` to safely remove the file when it exists without any error
  • Loading branch information
mintuhouse authored Oct 27, 2023
1 parent 56fcfe8 commit 8f91a7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/async/io/unix_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def bind(&block)
Socket.bind(@address, **@options, &block)
rescue Errno::EADDRINUSE
# If you encounter EADDRINUSE from `bind()`, you can check if the socket is actually accepting connections by attempting to `connect()` to it. If the socket is still bound by an active process, the connection will succeed. Otherwise, it should be safe to `unlink()` the path and try again.
if !bound? && File.exist?(@path)
File.unlink(@path)
if !bound?
FileUtils.safe_unlink(@path)
retry
else
raise
Expand Down
23 changes: 14 additions & 9 deletions spec/async/io/unix_endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

require 'async/io/unix_endpoint'
require 'async/io/stream'
require 'fileutils'

RSpec.describe Async::IO::UNIXEndpoint do
include_context Async::RSpec::Reactor
Expand All @@ -14,14 +13,6 @@
let(:path) {File.join(__dir__, "unix-socket")}
subject {described_class.unix(path)}

before(:each) do
FileUtils.rm_f path
end

after do
FileUtils.rm_f path
end

it "should echo data back to peer" do
server_task = reactor.async do
subject.accept do |peer|
Expand All @@ -39,6 +30,20 @@

server_task.stop
end

it "should not fail to bind if there are no existing bindings on the socket" do
server_task1 = reactor.async do
subject.bind
end
server_task1.stop

server_task2 = reactor.async do
expect do
subject.bind
end.to_not raise_error
end
server_task2.stop
end

it "should fails to bind if there is an existing binding" do
condition = Async::Condition.new
Expand Down

0 comments on commit 8f91a7b

Please sign in to comment.