diff --git a/pkg/service/ioservice_ingress.go b/pkg/service/ioservice_ingress.go index c1fe59555..39e882447 100644 --- a/pkg/service/ioservice_ingress.go +++ b/pkg/service/ioservice_ingress.go @@ -73,6 +73,13 @@ func (s *IOInfoService) UpdateIngressState(ctx context.Context, req *rpc.UpdateI if err != nil { return nil, err } + if info.State == nil { + // LoadIngress reads the ingress info and its state non-atomically: a concurrent + // DeleteIngress can remove the state key between the two reads, in which case + // LoadIngress returns the info with a nil State and no error. Treat it as an + // empty state instead of panicking on the status comparison below. + info.State = &livekit.IngressState{} + } if err = s.is.UpdateIngressState(ctx, req.IngressId, req.State); err != nil { logger.Errorw("could not update ingress", err) diff --git a/pkg/service/ioservice_ingress_test.go b/pkg/service/ioservice_ingress_test.go new file mode 100644 index 000000000..acd778d68 --- /dev/null +++ b/pkg/service/ioservice_ingress_test.go @@ -0,0 +1,46 @@ +// Copyright 2026 LiveKit, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package service_test + +import ( + "context" + "testing" + + "github.com/livekit/protocol/livekit" + "github.com/livekit/protocol/rpc" + "github.com/stretchr/testify/require" + + "github.com/livekit/livekit-server/pkg/service" + "github.com/livekit/livekit-server/pkg/service/servicefakes" + "github.com/livekit/livekit-server/pkg/telemetry/telemetryfakes" +) + +// A concurrent DeleteIngress can remove the ingress state key between the two +// non-atomic reads LoadIngress performs, making it return an IngressInfo with a +// nil State and no error. UpdateIngressState used to panic dereferencing it, +// bringing down the whole node. +func TestUpdateIngressStateWithNilState(t *testing.T) { + is := &servicefakes.FakeIngressStore{} + is.LoadIngressReturns(&livekit.IngressInfo{IngressId: "IN_test"}, nil) + + io, err := service.NewIOInfoService(nil, nil, is, nil, &telemetryfakes.FakeTelemetryService{}) + require.NoError(t, err) + + _, err = io.UpdateIngressState(context.Background(), &rpc.UpdateIngressStateRequest{ + IngressId: "IN_test", + State: &livekit.IngressState{Status: livekit.IngressState_ENDPOINT_INACTIVE}, + }) + require.NoError(t, err) +}