Skip to content

Commit

Permalink
Fix nil pointer panic and wait for headerSub to finish
Browse files Browse the repository at this point in the history
  • Loading branch information
IronGauntlets committed Nov 18, 2024
1 parent e75669b commit 71a5c07
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions rpc/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpc
import (
"context"
"encoding/json"
"sync"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/core"
Expand Down Expand Up @@ -37,7 +38,8 @@ func (h *Handler) SubscribeEvents(ctx context.Context, fromAddr *felt.Felt, keys
if blockID == nil {
requestedHeader = headHeader
} else {
requestedHeader, rpcErr := h.blockHeaderByID(blockID)
var rpcErr *jsonrpc.Error
requestedHeader, rpcErr = h.blockHeaderByID(blockID)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down Expand Up @@ -67,17 +69,26 @@ func (h *Handler) SubscribeEvents(ctx context.Context, fromAddr *felt.Felt, keys

// The specification doesn't enforce ordering of events therefore events from new blocks can be sent before
// old blocks.
sub.wg.Go(func() {
// Todo: see if sub's wg can be used?
wg := sync.WaitGroup{}
wg.Add(1)

go func() {
defer wg.Done()

for {
select {
case <-subscriptionCtx.Done():
return
case header := <-headerSub.Recv():
h.processEvents(subscriptionCtx, w, id, header.Number, headHeader.Number, fromAddr, keys)
h.processEvents(subscriptionCtx, w, id, header.Number, header.Number, fromAddr, keys)
}
}
})
}()

h.processEvents(subscriptionCtx, w, id, requestedHeader.Number, headHeader.Number, fromAddr, keys)

wg.Wait()
})

return &SubscriptionID{ID: id}, nil
Expand Down

0 comments on commit 71a5c07

Please sign in to comment.