fix: hold signal messages until the ReconnectResponse goes out (#4827)
* fix: hold signal messages until the ReconnectResponse goes out Clients take the ReconnectResponse as the first message on a resumed or migrated in signal connection, anything ahead of it is dropped. Hold messages back until it has been written. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: keep queued participant updates on a path that always flushes Queue only while the participant is not ready, that queue is always drained by the join or reconnect response. Log a dropped SDP, it leaves the negotiation waiting until the state machine recovers it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: flush queued updates whenever the connection opens Queue participant updates while the handshake is pending again, and give the signaller a hook that fires when the connection opens, on an explicit open and on the handshake window expiring, so the queue always drains. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: drive the handshake window off a timer and read the gate atomically The window ran only when something asked whether the handshake was pending, so a connection with nothing else to send held its queue. Reading the gate under the lock the flush takes closes the race where an update queued just after a flush. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix: ignore a handshake timeout from a wait that has ended Stop cannot cancel a timeout that is already running, so tag each wait and let a timeout act only on its own. Count opens atomically in the test, the timer fires on its own goroutine. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
60e08cb19f
commit
0aae7a4894
@@ -31,8 +31,9 @@ import (
|
||||
var _ ParticipantSignaller = (*signallerAsync)(nil)
|
||||
|
||||
type SignallerAsyncParams struct {
|
||||
Logger logger.Logger
|
||||
Participant types.LocalParticipant
|
||||
Logger logger.Logger
|
||||
Participant types.LocalParticipant
|
||||
OnHandshakeOpened func()
|
||||
}
|
||||
|
||||
type signallerAsync struct {
|
||||
@@ -43,8 +44,11 @@ type signallerAsync struct {
|
||||
|
||||
func NewSignallerAsync(params SignallerAsyncParams) ParticipantSignaller {
|
||||
return &signallerAsync{
|
||||
params: params,
|
||||
signallerAsyncBase: newSignallerAsyncBase(signallerAsyncBaseParams{Logger: params.Logger}),
|
||||
params: params,
|
||||
signallerAsyncBase: newSignallerAsyncBase(signallerAsyncBaseParams{
|
||||
Logger: params.Logger,
|
||||
OnHandshakeOpened: params.OnHandshakeOpened,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,17 +73,35 @@ func (s *signallerAsync) WriteMessage(msg proto.Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !s.params.Participant.IsReady() {
|
||||
if typed, ok := msg.(*livekit.SignalResponse); !ok {
|
||||
s.params.Logger.Warnw(
|
||||
"unknown message type", nil,
|
||||
"messageType", fmt.Sprintf("%T", msg),
|
||||
)
|
||||
} else {
|
||||
if typed.GetJoin() == nil {
|
||||
return nil
|
||||
// a signal connection opens with a join response, or with a reconnect response when
|
||||
// it is resumed or migrated in. The client reads that first message as the handshake,
|
||||
// so nothing may go out ahead of it.
|
||||
isHandshake := false
|
||||
isSdp := false
|
||||
if typed, ok := msg.(*livekit.SignalResponse); !ok {
|
||||
s.params.Logger.Warnw(
|
||||
"unknown message type", nil,
|
||||
"messageType", fmt.Sprintf("%T", msg),
|
||||
)
|
||||
} else {
|
||||
isHandshake = typed.GetJoin() != nil || typed.GetReconnect() != nil
|
||||
isSdp = typed.GetOffer() != nil || typed.GetAnswer() != nil
|
||||
}
|
||||
|
||||
if !isHandshake && (!s.params.Participant.IsReady() || s.HandshakePending()) {
|
||||
logFunc := s.params.Logger.Debugw
|
||||
if isSdp {
|
||||
// a dropped SDP leaves the negotiation waiting for the peer until the
|
||||
// negotiation state machine recovers it, so make it visible
|
||||
logFunc = func(msg string, keysAndValues ...any) {
|
||||
s.params.Logger.Infow(msg, keysAndValues...)
|
||||
}
|
||||
}
|
||||
logFunc(
|
||||
"dropping message, connection has not opened yet",
|
||||
"messageType", getMessageType(msg),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
sink := s.GetResponseSink()
|
||||
@@ -108,6 +130,10 @@ func (s *signallerAsync) WriteMessage(msg proto.Message) error {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if isHandshake {
|
||||
// the connection is open, hold nothing back any more
|
||||
s.OpenHandshake()
|
||||
}
|
||||
s.params.Logger.Debugw("sent signal response", "response", logger.Proto(msg))
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user