Skip to content

Commit

Permalink
Fix deadlock when closing pipe
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Champlon <[email protected]>
  • Loading branch information
mat007 committed Dec 13, 2024
1 parent bdc6c11 commit 90fdf20
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,11 @@ func (l *win32PipeListener) Accept() (net.Conn, error) {

func (l *win32PipeListener) Close() error {
select {
case l.closeCh <- 1:
<-l.doneCh
case <-l.doneCh:
case <-l.closeCh:
default:
close(l.closeCh)
<-l.doneCh
}
return nil
}
Expand Down
40 changes: 40 additions & 0 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,43 @@ func TestListenConnectRace(t *testing.T) {
wg.Wait()
}
}

func TestCloseRace(t *testing.T) {
for i := 0; i < 200 && !t.Failed(); i++ {
l, err := ListenPipe(testPipeName, &PipeConfig{MessageMode: true})
if err != nil {
t.Fatal(err)
}
go func() {
for {
c, err := l.Accept()
if err != nil {
return
}
b, err := io.ReadAll(c)
if err != nil {
t.Error(err)
return
}
_, _ = c.Write(b)
_ = c.Close()
}
}()

c, err := DialPipe(testPipeName, nil)
if err != nil {
t.Fatal(err)
}
if _, err = c.Write([]byte("hello")); err != nil {
t.Fatal(err)
}
if err := c.(CloseWriter).CloseWrite(); err != nil {
t.Fatal(err)
}
if _, err := io.ReadAll(c); err != nil {
t.Fatal(err)
}
_ = c.Close()
_ = l.Close()
}
}

0 comments on commit 90fdf20

Please sign in to comment.