Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/emit-events-after-reconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Room events emitted right after a reconnect are no longer held back. The room went back to `Connected` and emitted `Reconnected` while the engine still reported a pending reconnect until its attempt settled, so events such as `RoomMetadataChanged` or `ActiveSpeakersChanged` sent in that window were buffered and never emitted.
69 changes: 69 additions & 0 deletions src/room/RTCEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,75 @@ describe('RTCEngine', () => {
});
});

describe('pendingReconnect (#2183)', () => {
// Room buffers events while a reconnect is pending, so the flag must drop as soon as the
// scheduled attempt starts and whenever the timer is cancelled.
interface PendingInternals {
_isClosed: boolean;
handleDisconnect: (connection: string, reason?: number) => void;
attemptReconnect: (reason?: number) => Promise<void>;
clearReconnectTimeout: () => void;
}

function primeEngine() {
const engine = new RTCEngine({
...roomOptionDefaults,
reconnectPolicy: { nextRetryDelayInMs: () => 100 },
});
const internals = engine as unknown as PendingInternals;
internals._isClosed = false;
const attempts: Array<Future<void, Error>> = [];
internals.attemptReconnect = vi.fn(() => {
const attempt = new Future<void, Error>();
attempts.push(attempt);
return attempt.promise;
});
return { engine, internals, attempts };
}

beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

it('is cleared once the scheduled attempt starts', () => {
const { engine, internals } = primeEngine();
internals.handleDisconnect('signal');
expect(engine.pendingReconnect).toBe(true);

vi.advanceTimersByTime(100);

expect(internals.attemptReconnect).toHaveBeenCalledTimes(1);
expect(engine.pendingReconnect).toBe(false);
});

it('is cleared when the scheduled attempt is cancelled', () => {
// e.g. handleBrowserOnLine cancels the timer and reconnects right away
const { engine, internals } = primeEngine();
internals.handleDisconnect('signal');

internals.clearReconnectTimeout();

expect(engine.pendingReconnect).toBe(false);
});

it('keeps a retry scheduled while the previous attempt settles', async () => {
const { engine, internals, attempts } = primeEngine();
internals.handleDisconnect('signal');
vi.advanceTimersByTime(100);

// the attempt fails and schedules a retry before its promise settles
internals.handleDisconnect('signal');
attempts[0].resolve?.();
await vi.advanceTimersByTimeAsync(0);

expect(engine.pendingReconnect).toBe(true);
});
});

describe('publisher answer mid mapping', () => {
function primeEngine() {
const engine = new RTCEngine(roomOptionDefaults);
Expand Down
15 changes: 4 additions & 11 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1309,17 +1309,10 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
// since the current engine may have inherited a regional url
this.emit(EngineEvent.TokenRefreshed, this.token);
}
const reconnectTimeout = CriticalTimers.setTimeout(
() =>
this.attemptReconnect(disconnectReason).finally(() => {
// a failed attempt schedules its retry before this runs; don't clear the retry's handle
if (this.reconnectTimeout === reconnectTimeout) {
this.reconnectTimeout = undefined;
}
}),
delay,
);
this.reconnectTimeout = reconnectTimeout;
this.reconnectTimeout = CriticalTimers.setTimeout(() => {
this.reconnectTimeout = undefined;
this.attemptReconnect(disconnectReason);
}, delay);
};

/**
Expand Down
Loading