Skip to content

Commit a84eee8

Browse files
authored
Wrap setE2EEEnabled call in a mutex (#1882)
1 parent 6214558 commit a84eee8

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

.changeset/tidy-parrots-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"livekit-client": patch
3+
---
4+
5+
Wrap setE2EEEnabled call in a mutex

src/room/Room.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
187187

188188
private e2eeManager: BaseE2EEManager | undefined;
189189

190+
private e2eeStateMutex: Mutex = new Mutex();
191+
190192
private connectionReconcileInterval?: ReturnType<typeof setInterval>;
191193

192194
private regionUrlProvider?: RegionUrlProvider;
@@ -293,7 +295,6 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
293295
});
294296

295297
this.disconnectLock = new Mutex();
296-
297298
this.localParticipant = new LocalParticipant(
298299
'',
299300
'',
@@ -411,13 +412,21 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
411412
* @experimental
412413
*/
413414
async setE2EEEnabled(enabled: boolean) {
414-
if (this.e2eeManager) {
415-
await Promise.all([this.localParticipant.setE2EEEnabled(enabled)]);
416-
if (this.localParticipant.identity !== '') {
417-
this.e2eeManager.setParticipantCryptorEnabled(enabled, this.localParticipant.identity);
415+
const unlock = await this.e2eeStateMutex.lock();
416+
try {
417+
if (this.e2eeManager) {
418+
if (this.isE2EEEnabled !== enabled) {
419+
await this.localParticipant.setE2EEEnabled(enabled);
420+
421+
if (this.localParticipant.identity !== '') {
422+
this.e2eeManager.setParticipantCryptorEnabled(enabled, this.localParticipant.identity);
423+
}
424+
}
425+
} else {
426+
throw Error('e2ee not configured, please set e2ee settings within the room options');
418427
}
419-
} else {
420-
throw Error('e2ee not configured, please set e2ee settings within the room options');
428+
} finally {
429+
unlock();
421430
}
422431
}
423432

@@ -450,6 +459,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
450459
this.emit(RoomEvent.EncryptionError, error, participant);
451460
});
452461
this.e2eeManager?.setup(this);
462+
this.e2eeManager?.setupEngine(this.engine);
453463
}
454464
}
455465

src/room/participant/LocalParticipant.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Mutex } from '@livekit/mutex';
12
import {
23
AddTrackRequest,
34
AudioTrackFeature,
@@ -143,6 +144,8 @@ export default class LocalParticipant extends Participant {
143144

144145
private encryptionType: Encryption_Type = Encryption_Type.NONE;
145146

147+
private e2eeStateMutex = new Mutex();
148+
146149
private reconnectFuture?: Future<void, Error>;
147150

148151
private signalConnectedFuture?: Future<void, Error>;
@@ -499,8 +502,20 @@ export default class LocalParticipant extends Participant {
499502

500503
/** @internal */
501504
async setE2EEEnabled(enabled: boolean) {
502-
this.encryptionType = enabled ? Encryption_Type.GCM : Encryption_Type.NONE;
503-
await this.republishAllTracks(undefined, false);
505+
const unlock = await this.e2eeStateMutex.lock();
506+
try {
507+
this.encryptionType = enabled ? Encryption_Type.GCM : Encryption_Type.NONE;
508+
await Promise.all(this.pendingPublishPromises.values());
509+
if (
510+
this.trackPublications.size === 0 ||
511+
Array.from(this.trackPublications.values()).every((pub) => pub.isEncrypted === enabled)
512+
) {
513+
return;
514+
}
515+
await this.republishAllTracks(undefined, false);
516+
} finally {
517+
unlock();
518+
}
504519
}
505520

506521
/**

0 commit comments

Comments
 (0)