Skip to content

Commit cd2fc20

Browse files
authored
fix: prevent duplicate livechat rooms on concurrent requests (RocketChat#39087)
1 parent 8d8853e commit cd2fc20

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rocket.chat/models': patch
3+
'@rocket.chat/meteor': patch
4+
---
5+
6+
Fixes race condition causing duplicate open livechat rooms per visitor token.

apps/meteor/app/livechat/server/lib/Helper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ export const prepareLivechatRoom = async (
145145
priorityWeight: LivechatPriorityWeight.NOT_SPECIFIED,
146146
estimatedWaitingTimeQueue: DEFAULT_SLA_CONFIG.ESTIMATED_WAITING_TIME_QUEUE,
147147
...extraRoomInfo,
148+
// marker field for unique index - only new rooms have this field (see #39087)
149+
// allows index creation to succeed even if old duplicates exist
150+
_enforceSingleRoom: true,
148151
} as InsertionModel<IOmnichannelRoom>;
149152
};
150153

packages/models/src/models/LivechatRooms.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
7777
{ key: { servedBy: 1, ts: 1 }, partialFilterExpression: { servedBy: { $exists: true }, t: 'l' } },
7878
{ key: { 'v.activity': 1, 'ts': 1 }, partialFilterExpression: { 'v.activity': { $exists: true }, 't': 'l' } },
7979
{ key: { contactId: 1 }, partialFilterExpression: { contactId: { $exists: true }, t: 'l' } },
80+
{
81+
key: { 'v.token': 1 },
82+
unique: true,
83+
partialFilterExpression: {
84+
t: 'l',
85+
open: true,
86+
_enforceSingleRoom: true,
87+
},
88+
},
8089
];
8190
}
8291

0 commit comments

Comments
 (0)