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

Commit 8f91a7b

Browse files
authored
Fix for socket file not being reused even when not bound (#70)
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
1 parent 56fcfe8 commit 8f91a7b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

lib/async/io/unix_endpoint.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def bind(&block)
3535
Socket.bind(@address, **@options, &block)
3636
rescue Errno::EADDRINUSE
3737
# 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.
38-
if !bound? && File.exist?(@path)
39-
File.unlink(@path)
38+
if !bound?
39+
FileUtils.safe_unlink(@path)
4040
retry
4141
else
4242
raise

spec/async/io/unix_endpoint_spec.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
require 'async/io/unix_endpoint'
77
require 'async/io/stream'
8-
require 'fileutils'
98

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

17-
before(:each) do
18-
FileUtils.rm_f path
19-
end
20-
21-
after do
22-
FileUtils.rm_f path
23-
end
24-
2516
it "should echo data back to peer" do
2617
server_task = reactor.async do
2718
subject.accept do |peer|
@@ -39,6 +30,20 @@
3930

4031
server_task.stop
4132
end
33+
34+
it "should not fail to bind if there are no existing bindings on the socket" do
35+
server_task1 = reactor.async do
36+
subject.bind
37+
end
38+
server_task1.stop
39+
40+
server_task2 = reactor.async do
41+
expect do
42+
subject.bind
43+
end.to_not raise_error
44+
end
45+
server_task2.stop
46+
end
4247

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

0 commit comments

Comments
 (0)