-
Notifications
You must be signed in to change notification settings - Fork 91
Fix tier2/live-backfiller crash, race, leaks and cursor-resolver cancellation #820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+358
−16
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
caa63c5
Return error on nil Modules in ProcessRange
sduchesneau a6945f5
Fix map race in GetExecutionPlan cleanup goroutine
sduchesneau 12355b5
Fix reversed errors.Is args on stream error
sduchesneau 5e6a59c
Fix connection and goroutine leaks in live back filler
sduchesneau cbfe2a8
Propagate cancellation in cursor resolver
sduchesneau 9ab03e3
Merge branch 'develop' into fix/tier-service-bugs
sduchesneau ec477c2
Merge branch 'develop' into fix/tier-service-bugs
sduchesneau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package pipeline | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/streamingfast/bstream" | ||
| "github.com/streamingfast/bstream/hub" | ||
| "github.com/streamingfast/dstore" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestCursorResolver_CancellationDoesNotEmitUndo covers the residual race of | ||
| // issue #399: when the context is cancelled, the source shutdown goroutine may | ||
| // terminate the source before the ctx.Done() select case is picked. If the | ||
| // src.Terminated() case wins, the resolver used to fall through to the error | ||
| // fallback and emit an undo-to-LIB signal (junction = cursor.LIB, err = nil) | ||
| // instead of propagating the cancellation. | ||
| // | ||
| // The select choice between the two ready channels is randomized by the | ||
| // runtime, so we run several iterations: every single one must surface the | ||
| // context error, never an undo signal. | ||
| func TestCursorResolver_CancellationDoesNotEmitUndo(t *testing.T) { | ||
| mergedBlocksStore := dstore.NewMockStore(nil) | ||
| forkedBlocksStore := dstore.NewMockStore(nil) | ||
|
|
||
| resolver := NewCursorResolver((*hub.ForkableHub)(nil), mergedBlocksStore, forkedBlocksStore) | ||
|
|
||
| cursor := &bstream.Cursor{ | ||
| Step: bstream.StepNew, | ||
| Block: bstream.NewBlockRef("bb", 105), | ||
| HeadBlock: bstream.NewBlockRef("bb", 105), | ||
| LIB: bstream.NewBlockRef("aa", 100), | ||
| } | ||
|
|
||
| for i := 0; i < 30; i++ { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() // cancelled before resolution starts | ||
|
|
||
| junction, head, err := resolver(ctx, cursor) | ||
| require.Error(t, err, "iteration %d: cancellation was swallowed, got junction=%v head=%v (undo-to-LIB signal)", i, junction, head) | ||
| require.True(t, errors.Is(err, context.Canceled), "iteration %d: expected context.Canceled, got: %v", i, err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
| "time" | ||
|
|
||
| pbssinternal "github.com/streamingfast/substreams/pb/sf/substreams/intern/v2" | ||
| "github.com/streamingfast/substreams/client" | ||
| "github.com/streamingfast/substreams/reqctx" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/metadata" | ||
| ) | ||
|
|
||
| type fakeProcessRangeClientStream struct { | ||
| recvErr error | ||
| } | ||
|
|
||
| func (s *fakeProcessRangeClientStream) Recv() (*pbssinternal.ProcessRangeResponse, error) { | ||
| return nil, s.recvErr | ||
| } | ||
| func (s *fakeProcessRangeClientStream) Header() (metadata.MD, error) { return nil, nil } | ||
| func (s *fakeProcessRangeClientStream) Trailer() metadata.MD { return nil } | ||
| func (s *fakeProcessRangeClientStream) CloseSend() error { return nil } | ||
| func (s *fakeProcessRangeClientStream) Context() context.Context { return context.Background() } | ||
| func (s *fakeProcessRangeClientStream) SendMsg(any) error { return nil } | ||
| func (s *fakeProcessRangeClientStream) RecvMsg(any) error { return nil } | ||
|
|
||
| type fakeSubstreamsClient struct { | ||
| processRangeErr error | ||
| recvErr error | ||
| } | ||
|
|
||
| func (c *fakeSubstreamsClient) ProcessRange(ctx context.Context, in *pbssinternal.ProcessRangeRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[pbssinternal.ProcessRangeResponse], error) { | ||
| if c.processRangeErr != nil { | ||
| return nil, c.processRangeErr | ||
| } | ||
| return &fakeProcessRangeClientStream{recvErr: c.recvErr}, nil | ||
| } | ||
|
|
||
| func newCountingClientFactory(fakeClient *fakeSubstreamsClient, closeCount *int) client.InternalClientFactory { | ||
| return func() (pbssinternal.SubstreamsClient, func() error, []grpc.CallOption, client.Headers, error) { | ||
| closeFunc := func() error { | ||
| *closeCount++ | ||
| return nil | ||
| } | ||
| return fakeClient, closeFunc, nil, nil, nil | ||
| } | ||
| } | ||
|
|
||
| // TestRequestBackProcessing_ConnectionClosed verifies that the grpc client | ||
| // connection created by the clientFactory is closed exactly once on every | ||
| // code path of requestBackProcessing: previously, error returns leaked the | ||
| // connection and the success path closed it twice. | ||
| func TestRequestBackProcessing_ConnectionClosed(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| fakeClient *fakeSubstreamsClient | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "error getting stream", | ||
| fakeClient: &fakeSubstreamsClient{processRangeErr: errors.New("connection refused")}, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "error receiving from stream", | ||
| fakeClient: &fakeSubstreamsClient{recvErr: errors.New("stream broken")}, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "success", | ||
| fakeClient: &fakeSubstreamsClient{recvErr: io.EOF}, | ||
| expectError: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| closeCount := 0 | ||
| factory := newCountingClientFactory(c.fakeClient, &closeCount) | ||
|
|
||
| request := &pbssinternal.ProcessRangeRequest{SegmentSize: 10} | ||
| err := requestBackProcessing(context.Background(), zap.NewNop(), request, factory) | ||
| if c.expectError { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| assert.Equal(t, 1, closeCount, "clientFactory closeFunc must be called exactly once") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestRequestBackProcessing_NoGoroutineLeakOnCancel verifies that | ||
| // RequestBackProcessing does not block forever on its result channel when the | ||
| // context is cancelled and nobody is reading the channel anymore (which is | ||
| // what happens when LiveBackFiller.Start returns on ctx.Done()). | ||
| func TestRequestBackProcessing_NoGoroutineLeakOnCancel(t *testing.T) { | ||
| ctx := reqctx.WithRequest(context.Background(), &reqctx.RequestDetails{}) | ||
| ctx = reqctx.WithTier2RequestParameters(ctx, reqctx.Tier2RequestParameters{StateBundleSize: 10}) | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| cancel() | ||
|
|
||
| closeCount := 0 | ||
| factory := newCountingClientFactory(&fakeSubstreamsClient{processRangeErr: errors.New("unreachable")}, &closeCount) | ||
|
|
||
| jobResult := make(chan error) // unbuffered and never read, like after Start() has returned | ||
| done := make(chan struct{}) | ||
| go func() { | ||
| RequestBackProcessing(ctx, zap.NewNop(), 0, 0, factory, jobResult) | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("RequestBackProcessing leaked: still blocked sending its result after context cancellation") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hope it's not me :)