Skip to content

Commit

Permalink
Merge pull request #1 from mat007/close-race
Browse files Browse the repository at this point in the history
Fix deadlock when closing pipe
  • Loading branch information
mat007 authored Dec 12, 2024
2 parents bdc6c11 + 855df55 commit 542ebda
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,10 @@ func (l *win32PipeListener) Accept() (net.Conn, error) {

func (l *win32PipeListener) Close() error {
select {
case l.closeCh <- 1:
<-l.doneCh
case <-l.doneCh:
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 542ebda

Please sign in to comment.