Skip to content

Commit 8ee90fa

Browse files
committed
syscall: allow nacl's fake network code to Listen twice on the same address
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]>
1 parent dc9ad58 commit 8ee90fa

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/net/net_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,26 @@ func TestPacketConnClose(t *testing.T) {
261261
}
262262
}
263263
}
264+
265+
// nacl was previous failing to reuse an address.
266+
func TestListenCloseListen(t *testing.T) {
267+
const maxTries = 10
268+
for tries := 0; tries < maxTries; tries++ {
269+
ln, err := newLocalListener("tcp")
270+
if err != nil {
271+
t.Fatal(err)
272+
}
273+
addr := ln.Addr().String()
274+
if err := ln.Close(); err != nil {
275+
t.Fatal(err)
276+
}
277+
ln, err = Listen("tcp", addr)
278+
if err == nil {
279+
// Success. nacl couldn't do this before.
280+
ln.Close()
281+
return
282+
}
283+
t.Errorf("failed on try %d/%d: %v", tries+1, maxTries, err)
284+
}
285+
t.Fatal("failed to listen/close/listen on same address after %d tries", maxTries)
286+
}

src/syscall/net_nacl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,8 @@ func (f *netFile) listen(backlog int) error {
526526
if f.listener != nil {
527527
return EINVAL
528528
}
529-
_, ok := net.listener[netAddr{f.proto, f.sotype, f.addr.key()}]
530-
if ok {
529+
old, ok := net.listener[netAddr{f.proto, f.sotype, f.addr.key()}]
530+
if ok && !old.listenerClosed() {
531531
return EADDRINUSE
532532
}
533533
net.listener[netAddr{f.proto, f.sotype, f.addr.key()}] = f

0 commit comments

Comments
 (0)