From 288b7424cd5d7f616e1f2ff71bc651ce9c17bbae Mon Sep 17 00:00:00 2001 From: yibaiba <946100224@qq.com> Date: Thu, 3 Sep 2026 08:41:50 +0800 Subject: [PATCH] fix(ims): say when the SMSC is holding a short message A rejected report leaves its message at the head of the SMSC queue and every later one waits behind it. On 2026-09-03 that lasted 53 minutes and held five messages, and nothing in the logs said so: the redelivered copies were absorbed by dedup, the rejections looked like isolated warnings, and the state was only noticed as missing SMS. A redelivery is the observable sign the queue has not advanced, so remember the messages whose report was rejected and report it when one of them comes back, with the rejection count and how long the block has lasted. The record is dropped when a report finally lands, which is also the moment the queue is released. Redeliveries cannot be matched on the existing dedup fingerprint because it mixes in the RP-MR and the SMSC allocates a fresh one per attempt, which is why the same message came back looking new three times. Key on what survives instead: sender, recipient, content and SCTS. Co-authored-by: Cursor --- .../internal/vowifi/imscore/sms_ack.go | 6 ++ .../internal/vowifi/imscore/sms_inbound.go | 2 + .../vowifi/imscore/sms_queue_block.go | 100 ++++++++++++++++++ .../vowifi/imscore/sms_queue_block_test.go | 65 ++++++++++++ .../internal/vowifi/imscore/sms_runtime.go | 4 + 5 files changed, 177 insertions(+) create mode 100644 third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block.go create mode 100644 third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block_test.go diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go index 69639123..2477dbeb 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go @@ -18,6 +18,7 @@ type rpReportRequest struct { Body []byte RPMR byte Fingerprint string + Identity string RemoteURI string ContentType string ServiceCenter string @@ -139,7 +140,12 @@ func (s *Service) sendRPReportWithRetry(report rpReportRequest) { attempts, err := s.sendRPReportWithRetryPolicy( report, rpReportInitialDelay, rpReportRetryDelay, ) + if err == nil { + s.clearRejectedMTReport(report.Identity) + return + } if err != nil { + s.rememberRejectedMTReport(report.Identity) deviceID := "" if s != nil && s.cfg != nil { deviceID = s.cfg.DeviceID diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_inbound.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_inbound.go index 52cfd861..b4de069c 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_inbound.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_inbound.go @@ -235,6 +235,7 @@ func (s *Service) finalizeInboundSMSData( if shouldDispatch { s.publishInboundSMSWithFragment(message, message.fragmentSessionID, false) } + s.reportMTQueueBlocked(mtSMSIdentity(message)) fingerprint := buildMTSMSFingerprint(message, raw) return inboundSIPResult{ response: response, @@ -381,6 +382,7 @@ func (s *Service) rpReportForInbound(raw string, message inboundSMS, rpdu []byte report := rpReportRequest{ Inbound: raw, Body: rpdu, RPMR: message.rpMR, ServiceCenter: message.serviceCenter, + Identity: mtSMSIdentity(message), } if message.msisdnLess && strings.TrimSpace(message.deliveryReportTo) != "" { if contentType, body, err := buildMSISDNLessSMSPayload(shortMessageInfo{To: message.deliveryReportTo}, rpdu); err == nil { diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block.go new file mode 100644 index 00000000..a9e3dbad --- /dev/null +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block.go @@ -0,0 +1,100 @@ +package imscore + +import ( + "crypto/sha256" + "encoding/hex" + "strings" + "time" + + "github.com/iniwex5/vowifi-go/internal/vowifi/logging" +) + +// A short message whose report the gateway rejected stays at the head of the +// SMSC queue and holds back every later one until some redelivery is finally +// acknowledged. Measured on 2026-09-03: three rejections over 53 minutes, with +// five messages waiting behind it for up to 48 minutes each. Nothing in the +// logs said so at the time, so the state was only noticed as missing SMS. +const mtQueueBlockTTL = 2 * time.Hour + +// mtSMSIdentity keys a short message by what survives redelivery. The MT +// dedup fingerprint cannot be reused: it mixes in the RP-MR, and the SMSC +// allocates a fresh one per delivery attempt, so the same message looks +// different every time it comes back. +func mtSMSIdentity(message inboundSMS) string { + content := sha256.Sum256([]byte(strings.TrimSpace(message.content))) + scts := "" + if !message.timestamp.IsZero() { + scts = message.timestamp.Truncate(time.Second).UTC().Format(time.RFC3339) + } + identity := sha256.Sum256([]byte(strings.Join([]string{ + normalizeFragmentIdentity(message.sender), + normalizeFragmentIdentity(message.targetURI), + hex.EncodeToString(content[:]), scts, + }, "|"))) + return hex.EncodeToString(identity[:]) +} + +type unacknowledgedMT struct { + firstRejectedAt time.Time + rejections int +} + +// rememberRejectedMTReport records that the SMSC still believes this message is +// outstanding, so the next redelivery can be reported as a blocked queue. +func (s *Service) rememberRejectedMTReport(identity string) { + if s == nil || strings.TrimSpace(identity) == "" { + return + } + now := time.Now() + s.unackedMTMu.Lock() + defer s.unackedMTMu.Unlock() + if s.unackedMT == nil { + s.unackedMT = make(map[string]unacknowledgedMT, 8) + } + for key, entry := range s.unackedMT { + if now.Sub(entry.firstRejectedAt) > mtQueueBlockTTL { + delete(s.unackedMT, key) + } + } + entry := s.unackedMT[identity] + if entry.firstRejectedAt.IsZero() { + entry.firstRejectedAt = now + } + entry.rejections++ + s.unackedMT[identity] = entry +} + +// clearRejectedMTReport drops the record once a report for the message lands, +// which is also the moment the SMSC releases whatever queued behind it. +func (s *Service) clearRejectedMTReport(identity string) { + if s == nil || strings.TrimSpace(identity) == "" { + return + } + s.unackedMTMu.Lock() + defer s.unackedMTMu.Unlock() + if entry, held := s.unackedMT[identity]; held { + delete(s.unackedMT, identity) + logging.Info("IMS report accepted for a message the SMSC was holding", + "device", s.DeviceID(), "rejections", entry.rejections, + "held_for", time.Since(entry.firstRejectedAt).Round(time.Second)) + } +} + +// reportMTQueueBlocked runs when a message we could not acknowledge comes back. +// Its redelivery is the observable sign that the SMSC queue has not advanced. +func (s *Service) reportMTQueueBlocked(identity string) { + if s == nil || strings.TrimSpace(identity) == "" { + return + } + s.unackedMTMu.Lock() + entry, held := s.unackedMT[identity] + s.unackedMTMu.Unlock() + if !held { + return + } + logging.WarnRate("sms-mt-queue-blocked-"+s.DeviceID(), 30*time.Second, + "IMS SMSC is redelivering a short message whose report it rejected; "+ + "later messages wait behind it", + "device", s.DeviceID(), "rejections", entry.rejections, + "blocked_for", time.Since(entry.firstRejectedAt).Round(time.Second)) +} diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block_test.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block_test.go new file mode 100644 index 00000000..a996c6f3 --- /dev/null +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_queue_block_test.go @@ -0,0 +1,65 @@ +package imscore + +import ( + "testing" + "time" +) + +// The SMSC allocates a fresh RP-MR per delivery attempt, so a redelivery is +// only recognisable by what survives it. +func TestMTIdentitySurvivesARedelivery(t *testing.T) { + scts := time.Date(2026, 9, 3, 7, 35, 26, 0, time.UTC) + first := inboundSMS{ + sender: "Vodafone", targetURI: "tel:+447700900123", + content: "OTP is 'MVMNU'", timestamp: scts, rpMR: 153, + } + redelivery := first + redelivery.rpMR = 208 + + if mtSMSIdentity(first) != mtSMSIdentity(redelivery) { + t.Fatal("a fresh RP-MR changed the identity, so redeliveries cannot be matched") + } + if buildMTSMSFingerprint(first, "") == buildMTSMSFingerprint(redelivery, "") { + t.Fatal("expected the dedup fingerprint to differ; this test guards why identity exists") + } + + other := first + other.content = "OTP is '0F9M4'" + if mtSMSIdentity(first) == mtSMSIdentity(other) { + t.Fatal("different messages shared an identity") + } +} + +func TestRedeliveryOfARejectedReportIsReportedThenCleared(t *testing.T) { + service, _, _ := newInboundSMSTestService(t) + identity := "message-held-at-the-queue-head" + + // Nothing recorded yet: a first delivery must stay quiet. + service.reportMTQueueBlocked(identity) + if len(service.unackedMT) != 0 { + t.Fatalf("unackedMT = %d, want the map untouched", len(service.unackedMT)) + } + + service.rememberRejectedMTReport(identity) + service.rememberRejectedMTReport(identity) + entry, held := service.unackedMT[identity] + if !held { + t.Fatal("a rejected report left no record of the held message") + } + if entry.rejections != 2 { + t.Fatalf("rejections = %d, want 2", entry.rejections) + } + if entry.firstRejectedAt.IsZero() { + t.Fatal("no timestamp to measure how long the queue has been blocked") + } + first := entry.firstRejectedAt + service.rememberRejectedMTReport(identity) + if got := service.unackedMT[identity].firstRejectedAt; !got.Equal(first) { + t.Fatal("a later rejection moved the start of the block") + } + + service.clearRejectedMTReport(identity) + if _, still := service.unackedMT[identity]; still { + t.Fatal("an accepted report left the message marked as held") + } +} diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go index 382022d9..baf89dc0 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go @@ -100,6 +100,10 @@ type messagingRuntime struct { mtSMSSeenMu sync.Mutex mtSMSSeen map[string]time.Time + // Messages the SMSC still holds because it rejected our report. + unackedMTMu sync.Mutex + unackedMT map[string]unacknowledgedMT + // RP-SMMA has no MO message to correlate, so its Call-IDs live here // instead of smsPending; the network still reports on it (24.341 5.3.2.5). smmaSentMu sync.Mutex