Skip to content

Commit

Permalink
Merge pull request #34 from sipsma/close-once
Browse files Browse the repository at this point in the history
Only close epoller FD at most once.
  • Loading branch information
estesp authored Dec 19, 2019
2 parents 02ecf6a + 38c5469 commit 8375c34
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion console_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Epoller struct {
efd int
mu sync.Mutex
fdMapping map[int]*EpollConsole
closeOnce sync.Once
}

// NewEpoller returns an instance of epoller with a valid epoll fd.
Expand Down Expand Up @@ -151,7 +152,11 @@ func (e *Epoller) getConsole(sysfd int) *EpollConsole {

// Close closes the epoll fd
func (e *Epoller) Close() error {
return unix.Close(e.efd)
closeErr := os.ErrClosed // default to "file already closed"
e.closeOnce.Do(func() {
closeErr = unix.Close(e.efd)
})
return closeErr
}

// EpollConsole acts like a console but registers its file descriptor with an
Expand Down
8 changes: 8 additions & 0 deletions console_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ func TestEpollConsole(t *testing.T) {
if out := b.String(); out != expectedOutput {
t.Errorf("unexpected output %q", out)
}

// make sure multiple Close calls return os.ErrClosed after the first
if err := epoller.Close(); err != nil {
t.Fatal(err)
}
if err := epoller.Close(); err != os.ErrClosed {
t.Fatalf("unexpected error returned from second call to epoller.Close(): %v", err)
}
}

0 comments on commit 8375c34

Please sign in to comment.