Skip to content

Commit

Permalink
Fix: UNIXServer shouldn't delete path on bind failures
Browse files Browse the repository at this point in the history
fixes #3764
  • Loading branch information
ysbaddaden authored and asterite committed Dec 26, 2016
1 parent ab4a68e commit 3512d3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 17 additions & 0 deletions spec/std/socket_spec.cr
Expand Up @@ -149,6 +149,23 @@ describe UNIXServer do
end
end

it "won't delete existing file on bind failure" do
path = "/tmp/crystal-test-unix.sock"

File.write(path, "")
File.exists?(path).should be_true

begin
expect_raises Errno, /(already|Address) in use/ do
UNIXServer.new(path)
end

File.exists?(path).should be_true
ensure
File.delete(path) if File.exists?(path)
end
end

describe "accept" do
it "returns the client UNIXSocket" do
UNIXServer.open("/tmp/crystal-test-unix-sock") do |server|
Expand Down
8 changes: 4 additions & 4 deletions src/socket/unix_server.cr
Expand Up @@ -29,7 +29,7 @@ class UNIXServer < UNIXSocket
super(Family::UNIX, type)

bind(UNIXAddress.new(path)) do |error|
close
close(delete: false)
raise error
end

Expand Down Expand Up @@ -65,10 +65,10 @@ class UNIXServer < UNIXSocket
end

# Closes the socket, then deletes the filesystem pathname if it exists.
def close
super
def close(delete = true)
super()
ensure
if path = @path
if delete && (path = @path)
File.delete(path) if File.exists?(path)
@path = nil
end
Expand Down

0 comments on commit 3512d3c

Please sign in to comment.