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

windows: protect access to isClosed with mutex #454

Merged
merged 1 commit into from Jul 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions windows.go
Expand Up @@ -21,14 +21,16 @@ import (

// Watcher watches a set of files, delivering events to a channel.
type Watcher struct {
Events chan Event
Errors chan error
isClosed bool // Set to true when Close() is first called
mu sync.Mutex // Map access
port syscall.Handle // Handle to completion port
watches watchMap // Map of watches (key: i-number)
input chan *input // Inputs to the reader are sent on this channel
quit chan chan<- error
Events chan Event
Errors chan error

port syscall.Handle // Handle to completion port
input chan *input // Inputs to the reader are sent on this channel
quit chan chan<- error

mu sync.Mutex // Protects access to watches, isClosed
watches watchMap // Map of watches (key: i-number)
isClosed bool // Set to true when Close() is first called
}

// NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
Expand All @@ -51,6 +53,9 @@ func NewWatcher() (*Watcher, error) {

// Close removes all watches and closes the events channel.
func (w *Watcher) Close() error {
w.mu.Lock()
defer w.mu.Unlock()

if w.isClosed {
return nil
}
Expand All @@ -67,9 +72,12 @@ func (w *Watcher) Close() error {

// Add starts watching the named file or directory (non-recursively).
func (w *Watcher) Add(name string) error {
w.mu.Lock()
if w.isClosed {
return errors.New("watcher already closed")
}
w.mu.Unlock()

in := &input{
op: opAddWatch,
path: filepath.Clean(name),
Expand Down