fix(ims): ask the SMSC for its queue once port-s recovers

A P-CSCF reset kills whatever MT delivery the network had in flight on
that flow. Shortening the reconnect grace cut the blackout to about six
seconds, but a short message lost inside it still waits for the SMSC's
own retransmission timer, measured at 13 to 30 minutes on Vodafone UK.

RP-SMMA is the receiver-initiated way to ask for that queue now, so send
one when port-s comes back after a drop, reusing the existing one-per-
minute limit. Carriers that never reset the push flow never trigger it.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yibaibaandCursor committed 2026-09-02 16:26:42 +08:00
1 parent 43df0091e5
commit daff953be9
4 files changed
+71 -8

No files matched your search

@@ -249,6 +249,13 @@ func (s *Service) acceptProtectedSIP(listener net.Listener) {
_ = conn.Close()
return
}
if s.portSPushLost.CompareAndSwap(true, false) {
s.networkDone.Add(1)
go func() {
defer s.networkDone.Done()
s.promptSMSCRedeliveryAfterPushRecovery()
}()
}
s.networkDone.Add(1)
go s.serveProtectedSIPConnection(conn)
}
@@ -280,6 +287,9 @@ func (s *Service) handleProtectedServerPushClosed() {
if remaining > 0 {
return
}
// Any MT delivery the network attempted on the dead flow is lost, so mark
// the flow for an RP-SMMA prompt once port-s is back.
s.portSPushLost.Store(true)
logging.Info("IMS protected server push closed; wait for port-s reconnect",
"device", s.DeviceID(), "grace", s.portSReconnectWait())
s.schedulePortSReconnectWatch()
+28 -8
View File
@@ -158,15 +158,27 @@ func (s *Service) sendRPReportWithRetry(report rpReportRequest) {
// only receiver-initiated way to ask for that queue now (TS 24.011 clears
// MCEF and alerts the service centre), so send one instead of waiting.
func (s *Service) promptSMSCRedeliveryAfterRejectedReport(reportErr error) {
if s == nil || s.stopped() || rpReportRejectStatus(reportErr) == 0 {
if rpReportRejectStatus(reportErr) == 0 {
return
}
now := time.Now()
last := s.smmaPromptLastAt.Load()
if last != 0 && now.Sub(time.Unix(0, last)) < smmaPromptMinInterval {
s.promptSMSCRedelivery("a rejected report")
}
// promptSMSCRedeliveryAfterPushRecovery runs when port-s comes back after the
// P-CSCF dropped it. Whatever the network was pushing over the dead flow is
// gone, and the SMSC only retries on its own timer — measured at 13 to 30
// minutes — so ask for the queue as soon as the flow is usable again.
func (s *Service) promptSMSCRedeliveryAfterPushRecovery() {
// Same gate as RFC 5626 flow recovery in portSReconnectWatchFired: the
// REGISTER binding has to be live before anything is worth sending.
if s == nil || s.RegState() != regRegistered {
return
}
if !s.smmaPromptLastAt.CompareAndSwap(last, now.UnixNano()) {
s.promptSMSCRedelivery("a push connection reset")
}
func (s *Service) promptSMSCRedelivery(reason string) {
if s == nil || s.stopped() || !s.reserveSMMAPrompt(time.Now()) {
return
}
s.smsSendMu.Lock()
@@ -174,11 +186,19 @@ func (s *Service) promptSMSCRedeliveryAfterRejectedReport(reportErr error) {
s.smsSendMu.Unlock()
if err != nil {
logging.RunDebug("IMS RP-SMMA redelivery prompt failed",
"device", s.DeviceID(), "error", err)
"device", s.DeviceID(), "reason", reason, "error", err)
return
}
logging.Info("IMS RP-SMMA sent to release the SMSC queue after a rejected report",
"device", s.DeviceID())
logging.Info("IMS RP-SMMA sent to release the SMSC queue",
"device", s.DeviceID(), "reason", reason)
}
func (s *Service) reserveSMMAPrompt(now time.Time) bool {
last := s.smmaPromptLastAt.Load()
if last != 0 && now.Sub(time.Unix(0, last)) < smmaPromptMinInterval {
return false
}
return s.smmaPromptLastAt.CompareAndSwap(last, now.UnixNano())
}
func (s *Service) sendRPReportWithRetryPolicy(
@@ -386,6 +386,38 @@ func TestRejectedRPAckPromptsSMMARedelivery(t *testing.T) {
}
}
func TestPushConnectionRecoveryPromptsSMMARedelivery(t *testing.T) {
service, _, _ := newInboundSMSTestService(t)
// Keep RFC 5626 flow recovery out of the way; this test only covers the prompt.
service.portSReconnectGrace = time.Hour
var bodies [][]byte
service.transport.SetSendFn(func(raw string) error {
_, body, _ := strings.Cut(raw, "\r\n\r\n")
bodies = append(bodies, []byte(body))
service.transport.DeliverResponse(registerResponseForRequest(raw, 202, nil))
return nil
})
service.handleProtectedServerPushClosed()
if !service.portSPushLost.Load() {
t.Fatal("a closed push flow did not arm the RP-SMMA prompt")
}
service.promptSMSCRedeliveryAfterPushRecovery()
if len(bodies) != 1 {
t.Fatalf("sent %d requests, want one RP-SMMA prompt", len(bodies))
}
if kind := smscodec.ClassifyRPDU(bodies[0]).Kind; kind != smscodec.RPDUKindSMMA {
t.Fatalf("prompt RPDU = %q, want RP-SMMA", kind)
}
// Vodafone resets port-s several times an hour; one prompt per window.
service.promptSMSCRedeliveryAfterPushRecovery()
if len(bodies) != 1 {
t.Fatalf("sent %d requests, want the prompt to stay rate limited", len(bodies))
}
}
func TestRPAckUsesPAINotFrom(t *testing.T) {
service, _, _ := newInboundSMSTestService(t)
var requests []string
@@ -183,6 +183,7 @@ type Service struct {
protectedConnMu sync.Mutex
protectedConns map[net.Conn]struct{}
portSReconnectGrace time.Duration
portSPushLost atomic.Bool
portSWatchMu sync.Mutex
portSWatchTimer *time.Timer
sipWriteMu sync.Mutex