Skip to content

Commit

Permalink
internal/jsonrpc2_v2: add sync.Once for "panic: close of closed chann…
Browse files Browse the repository at this point in the history
…el" in idleListener.run

When timeout, closing timeout channel in idleListener.run may be called many times.
I added sync.Once field to idleListner for preventing it.

Fixes golang/go#51435
  • Loading branch information
uji committed Mar 5, 2022
1 parent e155b03 commit 51f310c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions internal/jsonrpc2_v2/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ func NewIdleListener(timeout time.Duration, wrap Listener) Listener {
}

type idleListener struct {
wrapped Listener
timeout time.Duration
newConns chan *idleCloser
closed chan struct{}
wasTimeout chan struct{}
closeOnce sync.Once
wrapped Listener
timeout time.Duration
newConns chan *idleCloser
closed chan struct{}
wasTimeout chan struct{}
closeOnce sync.Once
timeoutOnce sync.Once
}

type idleCloser struct {
Expand Down Expand Up @@ -273,7 +274,7 @@ func (l *idleListener) run() {
case <-timeout:
// we timed out, only happens when there are no active conns
// close the underlying listener, and allow the normal closing process to happen
close(l.wasTimeout)
l.timeoutOnce.Do(func() { close(l.wasTimeout) })
l.wrapped.Close()
case <-firstClosed:
// a conn closed, remove it from the active list
Expand Down

0 comments on commit 51f310c

Please sign in to comment.