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:
世界 2026-05-17 18:47:16 +08:00
parent b9cf48a827
commit 74140d3c67
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 7 additions and 0 deletions

View file

@ -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()

View file

@ -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 {