Skip to content

Commit

Permalink
syscall: allow nacl's fake network code to Listen twice on the same a…
Browse files Browse the repository at this point in the history
…ddress

Noticed from nacl trybot failures on new tests in
https://golang.org/cl/16630

Related earlier fix of mine to nacl's listen code:

  syscall: fix nacl listener to not accept connections once closed
  https://go-review.googlesource.com/15940

Perhaps a better fix (in the future?) would be to remove the listener
from the map at close, but that didn't seem entirely straightforward
last time I looked into it. It's not my code, but it seems that the
map entry continues to have a purpose even after Listener close. (?)

But given that this code is only really used for running tests and the
playground, this seems fine.

Change-Id: I43bfedc57c07f215f4d79c18f588d3650687a48f
Reviewed-on: https://go-review.googlesource.com/16650
Run-TryBot: Brad Fitzpatrick <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
bradfitz committed Nov 4, 2015
1 parent dc9ad58 commit 8ee90fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,26 @@ func TestPacketConnClose(t *testing.T) {
}
}
}

// nacl was previous failing to reuse an address.
func TestListenCloseListen(t *testing.T) {
const maxTries = 10
for tries := 0; tries < maxTries; tries++ {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
addr := ln.Addr().String()
if err := ln.Close(); err != nil {
t.Fatal(err)
}
ln, err = Listen("tcp", addr)
if err == nil {
// Success. nacl couldn't do this before.
ln.Close()
return
}
t.Errorf("failed on try %d/%d: %v", tries+1, maxTries, err)
}
t.Fatal("failed to listen/close/listen on same address after %d tries", maxTries)
}
4 changes: 2 additions & 2 deletions src/syscall/net_nacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ func (f *netFile) listen(backlog int) error {
if f.listener != nil {
return EINVAL
}
_, ok := net.listener[netAddr{f.proto, f.sotype, f.addr.key()}]
if ok {
old, ok := net.listener[netAddr{f.proto, f.sotype, f.addr.key()}]
if ok && !old.listenerClosed() {
return EADDRINUSE
}
net.listener[netAddr{f.proto, f.sotype, f.addr.key()}] = f
Expand Down

0 comments on commit 8ee90fa

Please sign in to comment.