mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 04:54:05 -05:00
Guard KZG send with context cancellation (#16144)
Avoid sending KZG verification reqs when the caller context is already canceled to prevent blocking on the channel
This commit is contained in:
@@ -163,11 +163,15 @@ func (s *Service) validateWithKzgBatchVerifier(ctx context.Context, dataColumns
|
|||||||
|
|
||||||
resChan := make(chan error, 1)
|
resChan := make(chan error, 1)
|
||||||
verificationSet := &kzgVerifier{dataColumns: dataColumns, resChan: resChan}
|
verificationSet := &kzgVerifier{dataColumns: dataColumns, resChan: resChan}
|
||||||
s.kzgChan <- verificationSet
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case s.kzgChan <- verificationSet:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return pubsub.ValidationIgnore, ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return pubsub.ValidationIgnore, ctx.Err() // parent context canceled, give up
|
return pubsub.ValidationIgnore, ctx.Err() // parent context canceled, give up
|
||||||
|
|||||||
@@ -304,6 +304,36 @@ func TestValidateWithKzgBatchVerifier_DeadlockOnTimeout(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateWithKzgBatchVerifier_ContextCanceledBeforeSend(t *testing.T) {
|
||||||
|
cancelledCtx, cancel := context.WithCancel(t.Context())
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
service := &Service{
|
||||||
|
ctx: context.Background(),
|
||||||
|
kzgChan: make(chan *kzgVerifier),
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
result, err := service.validateWithKzgBatchVerifier(cancelledCtx, nil)
|
||||||
|
require.Equal(t, pubsub.ValidationIgnore, result)
|
||||||
|
require.ErrorIs(t, err, context.Canceled)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(500 * time.Millisecond):
|
||||||
|
t.Fatal("validateWithKzgBatchVerifier did not return after context cancellation")
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-service.kzgChan:
|
||||||
|
t.Fatal("verificationSet was sent to kzgChan despite canceled context")
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createValidTestDataColumns(t *testing.T, count int) []blocks.RODataColumn {
|
func createValidTestDataColumns(t *testing.T, count int) []blocks.RODataColumn {
|
||||||
_, roSidecars, _ := util.GenerateTestFuluBlockWithSidecars(t, count)
|
_, roSidecars, _ := util.GenerateTestFuluBlockWithSidecars(t, count)
|
||||||
if len(roSidecars) >= count {
|
if len(roSidecars) >= count {
|
||||||
|
|||||||
3
changelog/avoid_kzg_send_after_context_cancel.md
Normal file
3
changelog/avoid_kzg_send_after_context_cancel.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Prevent blocked sends to the KZG batch verifier when the caller context is already canceled, avoiding useless queueing and potential hangs.
|
||||||
Reference in New Issue
Block a user