Skip to content

Commit

Permalink
fix(utils): wait for goroutine locks in Stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
sainnhe committed Jan 11, 2025
1 parent 2c8cbe7 commit bc0bf3c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
5 changes: 1 addition & 4 deletions pkg/remote/trans/netpollmux/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/cloudwego/netpoll"

goroutinelock "github.com/cloudwego/kitex/internal/utils/goroutine_lock"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/gofunc"
"github.com/cloudwego/kitex/pkg/kerrors"
Expand Down Expand Up @@ -366,9 +365,7 @@ func (t *svrTransHandler) GracefulShutdown(ctx context.Context) error {
}
return true
})
// 4. waiting for goroutine locks to be released
goroutinelock.GoroutineWg.Wait()
// 5. waiting all crrst packets received by client
// 4. waiting all crrst packets received by client
deadline := time.Now().Add(defaultExitWaitGracefulShutdownTime)
ticker := time.NewTicker(defaultExitWaitGracefulShutdownTime / 10)
defer ticker.Stop()
Expand Down
5 changes: 0 additions & 5 deletions pkg/remote/trans/nphttp2/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

"github.com/cloudwego/netpoll"

goroutinelock "github.com/cloudwego/kitex/internal/utils/goroutine_lock"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/gofunc"
"github.com/cloudwego/kitex/pkg/kerrors"
Expand Down Expand Up @@ -408,7 +407,6 @@ func (t *svrTransHandler) GracefulShutdown(ctx context.Context) error {
}
t.mu.Unlock()
if activeNums == 0 {
goroutinelock.GoroutineWg.Wait()
return nil
}
case <-graceTimer.C:
Expand All @@ -418,9 +416,6 @@ func (t *svrTransHandler) GracefulShutdown(ctx context.Context) error {
svrTrans := elem.Value.(*SvrTrans)
svrTrans.tr.Close()
}
// Goroutine Locks must wait after all connections are closed, otherwise new connections might be created
// while waiting for goroutine locks.
goroutinelock.GoroutineWg.Wait()
t.mu.Unlock()
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/cloudwego/localsession/backup"

internal_server "github.com/cloudwego/kitex/internal/server"
goroutinelock "github.com/cloudwego/kitex/internal/utils/goroutine_lock"
"github.com/cloudwego/kitex/pkg/acl"
"github.com/cloudwego/kitex/pkg/diagnosis"
"github.com/cloudwego/kitex/pkg/discovery"
Expand Down Expand Up @@ -319,6 +320,9 @@ func (s *server) Stop() (err error) {
}
s.svr = nil
}
// Goroutine Locks must wait after all connections are closed, otherwise new connections might be created while
// waiting for goroutine locks.
goroutinelock.GoroutineWg.Wait()
})
return
}
Expand Down

0 comments on commit bc0bf3c

Please sign in to comment.