fix: clear stale VoWiFi retry state

This commit is contained in:
yibaiba committed 2026-09-05 22:08:34 +08:00
1 parent 72820aeb19
commit 0c298436ca
3 files changed
+87 -41

No files matched your search

@@ -400,23 +400,34 @@ func TestRuntimeStartEmitsTerminalErrorAfterReconnectLoopStops(t *testing.T) {
}
func TestRuntimeStartKeepsScheduledRegistrarRetryInCurrentLoop(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
recorder := &eventRecorder{onEvent: func(event RuntimeEvent[*SessionResult]) {
if event.Kind == "retry" {
cancel()
}
}}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
recorder := &eventRecorder{}
req := baseRuntimeRequest(recorder)
req.Reconnect = true
req.SessionStarter = func(context.Context, SessionConfig) (*SessionResult, error) {
return nil, scheduledRetryTestError{retryAt: time.Now().Add(30 * time.Minute)}
var firstStore *imscore.RegistrarPenaltyStore
attempts := 0
req.SessionStarter = func(_ context.Context, config SessionConfig) (*SessionResult, error) {
attempts++
if attempts == 1 {
firstStore = config.RegistrarPenalties
return nil, scheduledRetryTestError{retryAt: time.Now().Add(10 * time.Millisecond)}
}
if config.RegistrarPenalties != firstStore {
t.Fatal("runtime reconnect replaced the registrar penalty store")
}
cancel()
return nil, context.Canceled
}
_, err := (Runtime{}).Start(ctx, req)
if !errors.Is(err, context.Canceled) {
t.Fatalf("Runtime.Start() error = %v", err)
}
want := []string{"prepared", "connecting", "error", "retry"}
if attempts != 2 || firstStore == nil {
t.Fatalf("runtime attempts = %d, registrar store = %p", attempts, firstStore)
}
want := []string{"prepared", "connecting", "error", "retry", "prepared", "connecting", "error"}
if got := recorder.kinds(); !reflect.DeepEqual(got, want) {
t.Fatalf("runtime events = %v, want %v", got, want)
}
@@ -484,18 +495,14 @@ func TestWaitRuntimeInterruptionUsesRecoveredContextKind(t *testing.T) {
}
type eventRecorder struct {
mu sync.Mutex
events []RuntimeEvent[*SessionResult]
onEvent func(RuntimeEvent[*SessionResult])
mu sync.Mutex
events []RuntimeEvent[*SessionResult]
}
func (recorder *eventRecorder) OnRuntimeEvent(_ context.Context, event RuntimeEvent[*SessionResult]) {
recorder.mu.Lock()
recorder.events = append(recorder.events, event)
recorder.mu.Unlock()
if recorder.onEvent != nil {
recorder.onEvent(event)
}
}
func (recorder *eventRecorder) kinds() []string {
+40 -12
View File
@@ -280,39 +280,67 @@ func TestRuntimeEventNamesAndStateAreRecovered(t *testing.T) {
instance.setState(State{
SessionState: "established", TunnelReady: true, DataPlaneUp: true,
IMSReady: true, SMSReady: true, SMSHealthReady: true,
IMSState: "registered", RegStatus: 1, RegStatusText: "registered",
SMSReadyReason: "IMS SMS receiver ready",
})
instance.setSession(&runtimecore.SessionResult{})
instance.setService(&stubService{})
observer := &instanceObserver{inst: instance, deviceID: "dev-1"}
observer.OnRuntimeEvent(context.Background(), runtimecore.RuntimeEvent[*runtimecore.SessionResult]{
Kind: "retry", Attempt: 2, RetryDelay: int64(time.Second), RedirectEPDG: "epdg-2.example",
})
state := instance.State()
if state.LastEvent != "retrying" || state.Phase != "retrying" || state.SessionState != "retrying" ||
state.LastRedirectEPDG != "epdg-2.example" || state.TunnelReady || state.IMSReady || state.SMSReady {
t.Fatalf("retry state = %+v", state)
}
observer.OnRuntimeEvent(context.Background(), runtimecore.RuntimeEvent[*runtimecore.SessionResult]{
Kind: "error", Message: "authentication rejected",
})
state = instance.State()
state := instance.State()
if state.LastEvent != "error" || state.Phase != "retrying" || state.SessionState != "retrying" ||
state.LastError != "authentication rejected" {
state.LastError != "authentication rejected" || state.IMSState != "retrying" || state.RegStatus != 0 ||
state.RegStatusText != "retrying" || state.SMSReadyReason != "" {
t.Fatalf("retryable error state = %+v", state)
}
assertRuntimeHandlesCleared(t, instance)
observer.OnRuntimeEvent(context.Background(), runtimecore.RuntimeEvent[*runtimecore.SessionResult]{
Kind: "ims_registered",
Kind: "retry", Attempt: 2, RetryDelay: int64(time.Second), RedirectEPDG: "epdg-2.example",
})
state = instance.State()
if state.LastEvent != "retrying" || state.Phase != "retrying" ||
state.LastRedirectEPDG != "epdg-2.example" || state.LastError != "authentication rejected" {
t.Fatalf("retry state = %+v", state)
}
newSession := &runtimecore.SessionResult{}
observer.OnRuntimeEvent(context.Background(), runtimecore.RuntimeEvent[*runtimecore.SessionResult]{
Kind: "established", Handle: newSession, Snapshot: runtimecore.Snapshot{Established: true},
})
state = instance.State()
if state.Phase != "ipsec_up" || state.IMSState != "registering" ||
state.RegStatus != 0 || state.RegStatusText != "registering" {
t.Fatalf("IPsec-ready IMS state = %+v", state)
}
observer.OnRuntimeEvent(context.Background(), runtimecore.RuntimeEvent[*runtimecore.SessionResult]{
Kind: "ims_registered", Handle: newSession,
})
state = instance.State()
if state.Phase != "ims_ready" || state.LastError != "" || state.LastErrorClass != "" || state.Error != "" {
t.Fatalf("recovered state still has last_error = %+v", state)
}
instance.setService(&stubService{})
observer.OnRuntimeEvent(context.Background(), runtimecore.RuntimeEvent[*runtimecore.SessionResult]{
Kind: "terminal_error", Message: "redirect loop",
})
state = instance.State()
if state.LastEvent != "terminal_error" || state.Phase != "error" || state.SessionState != "error" ||
state.LastError != "redirect loop" {
state.LastError != "redirect loop" || state.IMSState != "failed" || state.RegStatus != 0 ||
state.RegStatusText != "failed" {
t.Fatalf("terminal state = %+v", state)
}
assertRuntimeHandlesCleared(t, instance)
}
func assertRuntimeHandlesCleared(t *testing.T, instance *Instance) {
t.Helper()
instance.mu.RLock()
session := instance.session
instance.mu.RUnlock()
if session != nil || instance.Service() != nil {
t.Fatalf("runtime handles were not cleared: session=%p service=%T", session, instance.Service())
}
}
func restoredRuntimeRequest() StartRequest {
+25 -14
View File
@@ -62,6 +62,7 @@ func (observer *instanceObserver) applyEvent(
state.AccessReady = true
case "ipsec_up":
observer.installSession(event)
markIMSUnavailable(state, "registering")
state.Phase = readyPhase(*state)
state.SessionState = "established"
state.TunnelReady = event.Snapshot.Established || event.Handle != nil
@@ -85,41 +86,38 @@ func (observer *instanceObserver) applyEvent(
clearRuntimeError(state)
case "interrupted":
state.Phase = "interrupted"
state.SessionState = "interrupted"
state.TunnelReady = false
state.IMSReady = false
state.SMSReady = false
state.SMSHealthReady = false
state.DataPlaneUp = false
markIMSUnavailable(state, "restarting")
state.LastReason = strings.TrimSpace(event.Reason)
state.LastRedirectEPDG = strings.TrimSpace(event.RedirectEPDG)
case "retrying":
observer.clearRuntimeHandles()
applyRetryingState(state, event.Reason)
case "error":
observer.clearRuntimeHandles()
applyRetryingState(state, event.Reason)
state.LastErrorClass = "runtime"
state.LastError = firstNonEmptyString(event.Message, event.Reason)
state.Error = state.LastError
case "terminal_error":
observer.clearRuntimeHandles()
state.Phase = "error"
state.SessionState = "error"
state.TunnelReady = false
state.DataPlaneUp = false
state.IMSReady = false
state.SMSReady = false
state.SMSHealthReady = false
markIMSUnavailable(state, "failed")
state.LastErrorClass = "runtime"
state.LastError = firstNonEmptyString(event.Message, event.Reason)
state.Error = state.LastError
case "stopped":
observer.inst.setService(nil)
observer.inst.setSession(nil)
observer.clearRuntimeHandles()
state.Phase = "stopped"
state.SessionState = "stopped"
state.TunnelReady = false
state.IMSReady = false
state.SMSReady = false
state.SMSHealthReady = false
state.DataPlaneUp = false
markIMSUnavailable(state, "stopped")
}
}
@@ -128,14 +126,22 @@ func applyRetryingState(state *State, reason string) {
state.SessionState = "retrying"
state.TunnelReady = false
state.DataPlaneUp = false
state.IMSReady = false
state.SMSReady = false
state.SMSHealthReady = false
markIMSUnavailable(state, "retrying")
if reason = strings.TrimSpace(reason); reason != "" {
state.LastReason = reason
}
}
func markIMSUnavailable(state *State, status string) {
state.IMSState = status
state.IMSReady = false
state.SMSReady = false
state.SMSHealthReady = false
state.SMSReadyReason = ""
state.RegStatus = 0
state.RegStatusText = status
}
func clearRuntimeError(state *State) {
if state == nil {
return
@@ -162,6 +168,11 @@ func (observer *instanceObserver) installSession(
observer.installService(event)
}
func (observer *instanceObserver) clearRuntimeHandles() {
observer.inst.setService(nil)
observer.inst.setSession(nil)
}
func (observer *instanceObserver) installService(
event runtimecore.RuntimeEvent[*runtimecore.SessionResult],
) {