Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only close epoller FD at most once. #34

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}