Skip to content

Commit 41cee6f

Browse files
authored
Fix race in creating calls (#2662)
* Fix race in creating calls We ran an async function between checking for an existing call and adding the new one to the map, so it would have been possible to start creating another call while we were placing the first call. This changes the code to add the call to the map as soon as we've created it. Also adds more logging. * Switch to logger.debug * Fix unit tests
1 parent 3e1e99f commit 41cee6f

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

spec/unit/webrtc/groupCall.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ describe('Group Call', function() {
533533
let newCall: MatrixCall;
534534
while (
535535
(newCall = groupCall1.getCallByUserId(client2.userId)) === undefined ||
536+
newCall.peerConn === undefined ||
536537
newCall.callId == oldCall.callId
537538
) {
538539
await flushPromises();
@@ -643,6 +644,7 @@ describe('Group Call', function() {
643644
groupCall.localCallFeed.setAudioVideoMuted = jest.fn();
644645
const setAVMutedArray = groupCall.calls.map(call => {
645646
call.localUsermediaFeed.setAudioVideoMuted = jest.fn();
647+
call.localUsermediaFeed.isVideoMuted = jest.fn().mockReturnValue(true);
646648
return call.localUsermediaFeed.setAudioVideoMuted;
647649
});
648650
const tracksArray = groupCall.calls.reduce((acc, call) => {

src/webrtc/groupCall.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -856,12 +856,22 @@ export class GroupCall extends TypedEventEmitter<
856856
},
857857
);
858858

859+
if (existingCall) {
860+
logger.debug(`Replacing call ${existingCall.callId} to ${member.userId} with ${newCall.callId}`);
861+
this.replaceCall(existingCall, newCall, CallErrorCode.NewSession);
862+
} else {
863+
logger.debug(`Adding call ${newCall.callId} to ${member.userId}`);
864+
this.addCall(newCall);
865+
}
866+
859867
newCall.isPtt = this.isPtt;
860868

861869
const requestScreenshareFeed = opponentDevice.feeds.some(
862870
(feed) => feed.purpose === SDPStreamMetadataPurpose.Screenshare);
863871

864-
logger.log(`Placing call to ${member.userId}.`);
872+
logger.debug(
873+
`Placing call to ${member.userId}/${opponentDevice.device_id} session ID ${opponentDevice.session_id}.`,
874+
);
865875

866876
try {
867877
await newCall.placeCallWithCallFeeds(
@@ -881,18 +891,13 @@ export class GroupCall extends TypedEventEmitter<
881891
),
882892
);
883893
}
894+
this.removeCall(newCall, CallErrorCode.SignallingFailed);
884895
return;
885896
}
886897

887898
if (this.dataChannelsEnabled) {
888899
newCall.createDataChannel("datachannel", this.dataChannelOptions);
889900
}
890-
891-
if (existingCall) {
892-
this.replaceCall(existingCall, newCall, CallErrorCode.NewSession);
893-
} else {
894-
this.addCall(newCall);
895-
}
896901
};
897902

898903
public getDeviceForMember(userId: string): IGroupCallRoomMemberDevice {

0 commit comments

Comments
 (0)