Skip to content

Commit

Permalink
preserve socket error for listener
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed May 9, 2019
1 parent ea4f44e commit 5fee4f1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions readloop_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (l *Listener) monitor() {
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
}
} else {
l.socketError.Store(err)
l.Close()
return
}
Expand Down
1 change: 1 addition & 0 deletions readloop_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (l *Listener) monitor() {
}
}
} else {
l.socketError.Store(err)
l.Close()
return
}
Expand Down
26 changes: 12 additions & 14 deletions sess.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,12 +710,12 @@ type (
chSessionClosed chan net.Addr // session close queue
headerSize int // the additional header to a KCP frame

die chan struct{} // notify the listener has closed
dieOnce sync.Once
dieError error
die chan struct{} // notify the listener has closed
dieOnce sync.Once
socketError atomic.Value

rd atomic.Value // read deadline for Accept()

rd atomic.Value // read deadline for Accept()
wd atomic.Value
chTxQueue chan []ipv4.Message
}
)
Expand Down Expand Up @@ -819,7 +819,10 @@ func (l *Listener) AcceptKCP() (*UDPSession, error) {
case c := <-l.chAccepts:
return c, nil
case <-l.die:
return nil, l.dieError
if err := l.socketError.Load().(error); err != nil {
return nil, err
}
return nil, errors.New(errClosed)
}
}

Expand All @@ -837,18 +840,13 @@ func (l *Listener) SetReadDeadline(t time.Time) error {
}

// SetWriteDeadline implements the Conn SetWriteDeadline method.
func (l *Listener) SetWriteDeadline(t time.Time) error {
l.wd.Store(t)
return nil
}
func (l *Listener) SetWriteDeadline(t time.Time) error { return errors.New(errInvalidOperation) }

// Close stops listening on the UDP address. Already Accepted connections are not closed.
func (l *Listener) Close() (err error) {
l.dieOnce.Do(func() {
if err := l.conn.Close(); err == nil {
l.dieError = errors.New(errClosed)
} else {
l.dieError = err
if err := l.conn.Close(); err != nil {
l.socketError.Store(err)
}
close(l.die)
})
Expand Down
1 change: 1 addition & 0 deletions tx_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (l *Listener) txLoop() {
nbytes += n
xmitBuf.Put(txqueue[k].Buffers[0])
} else {
l.socketError.Store(err)
l.Close()
return
}
Expand Down
1 change: 1 addition & 0 deletions tx_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (l *Listener) txLoop() {
if n, err := conn.WriteBatch(vec, 0); err == nil {
vec = vec[n:]
} else {
l.socketError.Store(err)
l.Close()
return
}
Expand Down

0 comments on commit 5fee4f1

Please sign in to comment.