Skip to content

Commit

Permalink
release dynamically allocated channel/timer after use
Browse files Browse the repository at this point in the history
Signed-off-by: Jun.S.Shen <[email protected]>
  • Loading branch information
Jun.S.Shen committed Nov 17, 2023
1 parent b78488c commit 20b8d22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ func (f *win32File) closeHandle() {
// Close closes a win32File.
func (f *win32File) Close() error {
f.closeHandle()
if f.readDeadline.channel != nil {
close(f.readDeadline.channel)
f.readDeadline.channel = nil
}
if f.readDeadline.timer != nil {
f.readDeadline.timer.Stop()
f.readDeadline.timer = nil
}
if f.writeDeadline.channel != nil {
close(f.writeDeadline.channel)
f.writeDeadline.channel = nil
}
if f.writeDeadline.timer != nil {
f.writeDeadline.timer.Stop()
f.writeDeadline.timer = nil
}
return nil
}

Expand Down Expand Up @@ -311,6 +327,8 @@ func (d *deadlineHandler) set(deadline time.Time) error {
select {
case <-d.channel:
d.channelLock.Lock()
close(d.channel)
d.channel = nil
d.channel = make(chan struct{})
d.channelLock.Unlock()
default:
Expand All @@ -323,11 +341,16 @@ func (d *deadlineHandler) set(deadline time.Time) error {
timeoutIO := func() {
d.timedout.setTrue()
close(d.channel)
d.channel = nil
}

now := time.Now()
duration := deadline.Sub(now)
if deadline.After(now) {
if d.timer != nil {
d.timer.Stop()
d.timer = nil
}
// Deadline is in the future, set a timer to wait
d.timer = time.AfterFunc(duration, timeoutIO)
} else {
Expand Down
2 changes: 2 additions & 0 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ func (l *win32PipeListener) makeConnectedServerPipe() (*win32File, error) {
err = ErrPipeListenerClosed
}
}
close(ch)
ch = nil
return p, err
}

Expand Down
Empty file removed test.txt
Empty file.

0 comments on commit 20b8d22

Please sign in to comment.