Skip to content

Commit 3512d3c

Browse files
ysbaddadenAry Borenszweig
authored andcommitted
Fix: UNIXServer shouldn't delete path on bind failures
fixes #3764
1 parent ab4a68e commit 3512d3c

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

spec/std/socket_spec.cr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,23 @@ describe UNIXServer do
149149
end
150150
end
151151

152+
it "won't delete existing file on bind failure" do
153+
path = "/tmp/crystal-test-unix.sock"
154+
155+
File.write(path, "")
156+
File.exists?(path).should be_true
157+
158+
begin
159+
expect_raises Errno, /(already|Address) in use/ do
160+
UNIXServer.new(path)
161+
end
162+
163+
File.exists?(path).should be_true
164+
ensure
165+
File.delete(path) if File.exists?(path)
166+
end
167+
end
168+
152169
describe "accept" do
153170
it "returns the client UNIXSocket" do
154171
UNIXServer.open("/tmp/crystal-test-unix-sock") do |server|

src/socket/unix_server.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class UNIXServer < UNIXSocket
2929
super(Family::UNIX, type)
3030

3131
bind(UNIXAddress.new(path)) do |error|
32-
close
32+
close(delete: false)
3333
raise error
3434
end
3535

@@ -65,10 +65,10 @@ class UNIXServer < UNIXSocket
6565
end
6666

6767
# Closes the socket, then deletes the filesystem pathname if it exists.
68-
def close
69-
super
68+
def close(delete = true)
69+
super()
7070
ensure
71-
if path = @path
71+
if delete && (path = @path)
7272
File.delete(path) if File.exists?(path)
7373
@path = nil
7474
end

0 commit comments

Comments
 (0)