Skip to content

Commit

Permalink
Merge pull request ethereum#43 from rus-alex/fix/api-graceful-stop
Browse files Browse the repository at this point in the history
Fix of RPC graceful stop
  • Loading branch information
uprendis authored Jan 30, 2023
2 parents 2b54480 + 0a1113e commit 426ade5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ func (h *handler) cancelServerSubscriptions(err error) {
// startCallProc runs fn in a new goroutine and starts tracking it in the h.calls wait group.
func (h *handler) startCallProc(fn func(*callProc)) {
h.callWG.Add(1)
h.reg.callWG.Add(1)
go func() {
ctx, cancel := context.WithCancel(h.rootCtx)
defer h.callWG.Done()
defer h.reg.callWG.Done()

ctx, cancel := context.WithCancel(h.rootCtx)
defer cancel()
fn(&callProc{ctx: ctx})
}()
Expand Down
14 changes: 8 additions & 6 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
// requests to finish, then closes all codecs which will cancel pending requests and
// subscriptions.
func (s *Server) Stop() {
if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
log.Debug("RPC server shutting down")
s.codecs.Each(func(c interface{}) bool {
c.(ServerCodec).close()
return true
})
if !atomic.CompareAndSwapInt32(&s.run, 1, 0) {
return
}
log.Debug("RPC server shutting down")
s.codecs.Each(func(c interface{}) bool {
c.(ServerCodec).close()
return true
})
s.services.callWG.Wait()
}

// RPCService gives meta information about the server.
Expand Down
1 change: 1 addition & 0 deletions rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (

type serviceRegistry struct {
mu sync.Mutex
callWG sync.WaitGroup
services map[string]service
}

Expand Down

0 comments on commit 426ade5

Please sign in to comment.