Skip to content

Commit

Permalink
fix handling of ctx cancel in advertise server
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Aug 26, 2024
1 parent f8c80ef commit 2518419
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions firehose/app/firehose/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,11 @@ func (a *App) Run() error {
}
}

go func() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
if err := a.modules.InfoServer.Init(ctx, forkableHub, mergedBlocksStore, oneBlocksStore, a.logger); err != nil {
a.Shutdown(fmt.Errorf("cannot initialize info server: %w", err))
}
}()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
if err := a.modules.InfoServer.Init(ctx, forkableHub, mergedBlocksStore, oneBlocksStore, a.logger); err != nil {
a.Shutdown(fmt.Errorf("cannot initialize info server: %w", err))
}

a.logger.Info("launching gRPC firehoseServer", zap.Bool("live_support", withLive))
a.isReady.CAS(false, true)
Expand Down
8 changes: 4 additions & 4 deletions firehose/info/endpoint_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ func (s *InfoServer) getBlockFromOneBlockStore(ctx context.Context, blockNum uin
// it can be called only once
func (s *InfoServer) init(ctx context.Context, fhub *hub.ForkableHub, mergedBlocksStore dstore.Store, oneBlockStore dstore.Store, logger *zap.Logger) error {
ctx, cancel := context.WithCancel(ctx)
if s.validate {
defer cancel()
}
// cancel is later and depends on s.validate

ch := make(chan *pbbstream.Block)

Expand Down Expand Up @@ -199,6 +197,7 @@ func (s *InfoServer) init(ctx context.Context, fhub *hub.ForkableHub, mergedBloc
if !s.validate {
// in this case we don't wait for an answer, but we still try to fill the response
go func() {
defer cancel()
select {
case blk := <-ch:
if err := s.responseFiller(blk, s.response, s.validate); err != nil {
Expand All @@ -210,11 +209,12 @@ func (s *InfoServer) init(ctx context.Context, fhub *hub.ForkableHub, mergedBloc
logger.Warn("info response", zap.Error(err))
}
close(s.ready)
cancel()
}()

cancel()
return nil
}
defer cancel()

select {
case blk := <-ch:
Expand Down

0 comments on commit 2518419

Please sign in to comment.