diff --git a/CHANGELOG.md b/CHANGELOG.md index 38a2fc2e36a5..ec519dcf38a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve - Changed the signature of `ProcessPayload`. - Only Build the Protobuf state once during serialization. - Capella blocks are execution. +- Fixed panic when http request to subscribe to event stream fails. ### Deprecated diff --git a/api/client/event/event_stream.go b/api/client/event/event_stream.go index e85269038ddf..57187b8d5571 100644 --- a/api/client/event/event_stream.go +++ b/api/client/event/event_stream.go @@ -93,6 +93,7 @@ func (h *EventStream) Subscribe(eventsChannel chan<- *Event) { EventType: EventConnectionError, Data: []byte(errors.Wrap(err, client.ErrConnectionIssue.Error()).Error()), } + return } defer func() { diff --git a/api/client/event/event_stream_test.go b/api/client/event/event_stream_test.go index 66aa5c05f706..835e59f941b0 100644 --- a/api/client/event/event_stream_test.go +++ b/api/client/event/event_stream_test.go @@ -40,7 +40,7 @@ func TestNewEventStream(t *testing.T) { func TestEventStream(t *testing.T) { mux := http.NewServeMux() - mux.HandleFunc("/eth/v1/events", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/eth/v1/events", func(w http.ResponseWriter, _ *http.Request) { flusher, ok := w.(http.Flusher) require.Equal(t, true, ok) for i := 1; i <= 3; i++ { @@ -79,3 +79,23 @@ func TestEventStream(t *testing.T) { } } } + +func TestEventStreamRequestError(t *testing.T) { + topics := []string{"head"} + eventsChannel := make(chan *Event, 1) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + // use valid url that will result in failed request with nil body + stream, err := NewEventStream(ctx, http.DefaultClient, "http://badhost:1234", topics) + require.NoError(t, err) + + // error will happen when request is made, should be received over events channel + go stream.Subscribe(eventsChannel) + + event := <-eventsChannel + if event.EventType != EventConnectionError { + t.Errorf("Expected event type %q, got %q", EventConnectionError, event.EventType) + } + +}