usbip: close pre-promotion conns on shutdown
Tie accepted conns to s.ctx via closeConnOnContextDone and wait for dispatch goroutines in Close so handshake reads/writes can no longer leak goroutines or fds past service shutdown.
This commit is contained in:
@@ -36,6 +36,8 @@ type ServerService struct {
|
||||
sessions map[DataSession]struct{}
|
||||
sessionsClosed bool
|
||||
sessionsWG sync.WaitGroup
|
||||
|
||||
pendingConnsWG sync.WaitGroup
|
||||
}
|
||||
|
||||
func NewServerService(ctx context.Context, logger log.ContextLogger, tag string, options option.USBIPServerServiceOptions) (adapter.Service, error) {
|
||||
@@ -126,6 +128,7 @@ func (s *ServerService) Close() error {
|
||||
for _, session := range sessions {
|
||||
_ = session.Close()
|
||||
}
|
||||
s.pendingConnsWG.Wait()
|
||||
s.sessionsWG.Wait()
|
||||
|
||||
s.reconcileAccess.Lock()
|
||||
|
||||
@@ -34,11 +34,15 @@ func (s *ServerService) acceptLoop(ln net.Listener) {
|
||||
s.logger.Error("accept: ", err)
|
||||
return
|
||||
}
|
||||
s.pendingConnsWG.Add(1)
|
||||
go s.dispatchConn(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerService) dispatchConn(conn net.Conn) {
|
||||
defer s.pendingConnsWG.Done()
|
||||
cancelClose := closeConnOnContextDone(s.ctx, conn)
|
||||
defer cancelClose()
|
||||
var prefix [controlPrefaceSize]byte
|
||||
_, err := io.ReadFull(conn, prefix[:])
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user