mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-06 00:01:20 +00:00
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 <cursoragent@cursor.com>
This commit is contained in:
1 parent
0dd791f805
commit
288b7424cd
5 files changed
+177
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user