mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-05 16:02:17 +00:00
[rewrite-vowifi-sms-followup-05] clean up timed out USSI dialogs
This commit is contained in:
1 parent
eef5afa863
commit
2db9f4e491
2 files changed
+101
-5
No files matched your search
+30
-5
@@ -7,8 +7,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const dialogFailureCleanupTimeout = 5 * time.Second
|
||||
|
||||
// Send starts a USSI dialog and waits for a real network result.
|
||||
func (s *Service) Send(command string) (*Result, error) {
|
||||
return s.SendContext(context.Background(), command)
|
||||
@@ -50,7 +53,7 @@ func (s *Service) SendContext(ctx context.Context, command string) (*Result, err
|
||||
if result, found, parseErr := parseResponseResult(session.id, response); found || parseErr != nil {
|
||||
return s.finishNetworkResult(session, result, parseErr)
|
||||
}
|
||||
return s.waitResult(ctx, session)
|
||||
return s.waitResult(ctx, cfg, session)
|
||||
}
|
||||
|
||||
// Continue sends an in-dialog INFO and waits for the network result.
|
||||
@@ -82,7 +85,8 @@ func (s *Service) ContinueContext(ctx context.Context, sessionID, input string)
|
||||
session.mu.Unlock()
|
||||
response, err := cfg.Transport.RoundTrip(ctx, request)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ussi: INFO transaction failed: %w", err)
|
||||
cause := fmt.Errorf("ussi: INFO transaction failed: %w", err)
|
||||
return nil, s.cleanupFailedDialog(cfg, session, cause)
|
||||
}
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
s.clearSession(session.id)
|
||||
@@ -91,7 +95,7 @@ func (s *Service) ContinueContext(ctx context.Context, sessionID, input string)
|
||||
if result, found, parseErr := parseResponseResult(session.id, response); found || parseErr != nil {
|
||||
return s.finishNetworkResult(session, result, parseErr)
|
||||
}
|
||||
return s.waitResult(ctx, session)
|
||||
return s.waitResult(ctx, cfg, session)
|
||||
}
|
||||
|
||||
// Cancel sends a real in-dialog BYE before clearing the session.
|
||||
@@ -287,10 +291,11 @@ func (s *Service) clearSession(sessionID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) waitResult(ctx context.Context, session *Session) (*Result, error) {
|
||||
func (s *Service) waitResult(ctx context.Context, cfg Config, session *Session) (*Result, error) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, fmt.Errorf("ussi: wait for network result: %w", ctx.Err())
|
||||
cause := fmt.Errorf("ussi: wait for network result: %w", ctx.Err())
|
||||
return nil, s.cleanupFailedDialog(cfg, session, cause)
|
||||
case event := <-session.results:
|
||||
if event.err != nil {
|
||||
return nil, event.err
|
||||
@@ -299,6 +304,26 @@ func (s *Service) waitResult(ctx context.Context, session *Session) (*Result, er
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) cleanupFailedDialog(cfg Config, session *Session, cause error) error {
|
||||
session.mu.Lock()
|
||||
session.cseq++
|
||||
request := buildDialogRequest(cfg, session, "BYE", nil)
|
||||
session.mu.Unlock()
|
||||
s.clearSession(session.id)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), dialogFailureCleanupTimeout)
|
||||
defer cancel()
|
||||
response, err := cfg.Transport.RoundTrip(ctx, request)
|
||||
if err != nil {
|
||||
return errors.Join(cause, fmt.Errorf("ussi: cleanup BYE transaction failed: %w", err))
|
||||
}
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
cleanupErr := fmt.Errorf("ussi: cleanup BYE rejected: %d %s", response.StatusCode, strings.TrimSpace(response.Reason))
|
||||
return errors.Join(cause, cleanupErr)
|
||||
}
|
||||
return cause
|
||||
}
|
||||
|
||||
func (s *Service) finishNetworkResult(session *Session, result Result, err error) (*Result, error) {
|
||||
if err != nil {
|
||||
s.clearSession(session.id)
|
||||
|
||||
@@ -135,6 +135,77 @@ func TestServiceWaitsForInboundINFO(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceWaitTimeoutCleansDialogAndAllowsNextSend(t *testing.T) {
|
||||
invites := 0
|
||||
transport := &scriptedTransport{}
|
||||
transport.roundTrip = func(_ context.Context, request string) (Response, error) {
|
||||
switch requestMethod(request) {
|
||||
case "INVITE":
|
||||
invites++
|
||||
if invites == 1 {
|
||||
return ussiResponse(nil), nil
|
||||
}
|
||||
return ussiResponse(requestBodyXML(t, "Recovered", true)), nil
|
||||
case "BYE":
|
||||
return Response{StatusCode: 200, Reason: "OK"}, nil
|
||||
default:
|
||||
return Response{}, errors.New("unexpected request")
|
||||
}
|
||||
}
|
||||
svc := configuredService(t, transport)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
||||
defer cancel()
|
||||
if _, err := svc.SendContext(ctx, "*100#"); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("SendContext error = %v", err)
|
||||
}
|
||||
if active := svc.ActiveSessionID(); active != "" {
|
||||
t.Fatalf("active session after timeout = %q", active)
|
||||
}
|
||||
result, err := svc.Send("*101#")
|
||||
if err != nil {
|
||||
t.Fatalf("second Send: %v", err)
|
||||
}
|
||||
if !result.Done || result.Message != "Recovered" {
|
||||
t.Fatalf("second result = %#v", result)
|
||||
}
|
||||
if methods := requestMethods(transport.Requests()); strings.Join(methods, ",") != "INVITE,ACK,BYE,INVITE,ACK" {
|
||||
t.Fatalf("SIP sequence = %v", methods)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceINFOTimeoutCleansEstablishedDialog(t *testing.T) {
|
||||
transport := &scriptedTransport{}
|
||||
transport.roundTrip = func(ctx context.Context, request string) (Response, error) {
|
||||
switch requestMethod(request) {
|
||||
case "INVITE":
|
||||
return ussiResponse(requestBodyXML(t, "1. Continue", false)), nil
|
||||
case "INFO":
|
||||
<-ctx.Done()
|
||||
return Response{}, ctx.Err()
|
||||
case "BYE":
|
||||
return Response{StatusCode: 200, Reason: "OK"}, nil
|
||||
default:
|
||||
return Response{}, errors.New("unexpected request")
|
||||
}
|
||||
}
|
||||
svc := configuredService(t, transport)
|
||||
result, err := svc.Send("*100#")
|
||||
if err != nil {
|
||||
t.Fatalf("Send: %v", err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
||||
defer cancel()
|
||||
if _, err := svc.ContinueContext(ctx, result.SessionID, "1"); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("ContinueContext error = %v", err)
|
||||
}
|
||||
if active := svc.ActiveSessionID(); active != "" {
|
||||
t.Fatalf("active session after INFO timeout = %q", active)
|
||||
}
|
||||
if methods := requestMethods(transport.Requests()); strings.Join(methods, ",") != "INVITE,ACK,INFO,BYE" {
|
||||
t.Fatalf("SIP sequence = %v", methods)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRejectsSIPFailure(t *testing.T) {
|
||||
transport := &scriptedTransport{roundTrip: func(context.Context, string) (Response, error) {
|
||||
return Response{StatusCode: 503, Reason: "Service Unavailable"}, nil
|
||||
|
||||
Reference in new issue
Block a user