Skip to content

Commit

Permalink
Merge pull request #53 from darrenstahlmsft/wgDone
Browse files Browse the repository at this point in the history
Move f.wg.Done() to Read and Write to avoid leaking handles
  • Loading branch information
darstahl authored May 19, 2017
2 parents d311c76 + a64f284 commit 706941b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 5 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func (f *win32File) Close() error {
return nil
}

// prepareIo prepares for a new IO operation
// prepareIo prepares for a new IO operation.
// The caller must call f.wg.Done() when the IO is finished, prior to Close() returning.
func (f *win32File) prepareIo() (*ioOperation, error) {
f.wg.Add(1)
if f.closing {
Expand Down Expand Up @@ -155,7 +156,6 @@ func ioCompletionProcessor(h syscall.Handle) {
// the operation has actually completed.
func (f *win32File) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, err error) (int, error) {
if err != syscall.ERROR_IO_PENDING {
f.wg.Done()
return int(bytes), err
}

Expand Down Expand Up @@ -192,7 +192,6 @@ func (f *win32File) asyncIo(c *ioOperation, d *deadlineHandler, bytes uint32, er
// code to ioCompletionProcessor, c must remain alive
// until the channel read is complete.
runtime.KeepAlive(c)
f.wg.Done()
return int(r.bytes), err
}

Expand All @@ -202,6 +201,7 @@ func (f *win32File) Read(b []byte) (int, error) {
if err != nil {
return 0, err
}
defer f.wg.Done()

if f.readDeadline.timedout.isSet() {
return 0, ErrTimeout
Expand All @@ -228,6 +228,8 @@ func (f *win32File) Write(b []byte) (int, error) {
if err != nil {
return 0, err
}
defer f.wg.Done()

if f.writeDeadline.timedout.isSet() {
return 0, ErrTimeout
}
Expand Down
2 changes: 2 additions & 0 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ func connectPipe(p *win32File) error {
if err != nil {
return err
}
defer p.wg.Done()

err = connectNamedPipe(p.handle, &c.o)
_, err = p.asyncIo(c, nil, 0, err)
if err != nil && err != cERROR_PIPE_CONNECTED {
Expand Down

0 comments on commit 706941b

Please sign in to comment.