Improve server error handling

This commit is contained in:
世界
2023-03-02 00:18:35 +08:00
parent 20e1caa531
commit 6e22c004f6
4 changed files with 35 additions and 5 deletions

View File

@@ -13,6 +13,8 @@ import (
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"go.uber.org/atomic"
)
var _ adapter.Inbound = (*myInboundAdapter)(nil)
@@ -42,6 +44,8 @@ type myInboundAdapter struct {
udpAddr M.Socksaddr
packetOutboundClosed chan struct{}
packetOutbound chan *myInboundPacket
inShutdown atomic.Bool
}
func (a *myInboundAdapter) Type() string {
@@ -97,6 +101,7 @@ func (a *myInboundAdapter) Start() error {
}
func (a *myInboundAdapter) Close() error {
a.inShutdown.Store(true)
var err error
if a.clearSystemProxy != nil {
err = a.clearSystemProxy()

View File

@@ -39,10 +39,17 @@ func (a *myInboundAdapter) loopTCPIn() {
for {
conn, err := tcpListener.Accept()
if err != nil {
if E.IsClosed(err) {
//goland:noinspection GoDeprecation
//nolint:staticcheck
if netError, isNetError := err.(net.Error); isNetError && netError.Temporary() {
a.logger.Error(err)
continue
}
if a.inShutdown.Load() && E.IsClosed(err) {
return
}
a.logger.Error("accept: ", err)
a.tcpListener.Close()
a.logger.Error("serve error: ", err)
continue
}
go a.injectTCP(conn, adapter.InboundContext{})