Skip to content

Commit

Permalink
fix: race condition when forwarding livechat by splitting subscriptio…
Browse files Browse the repository at this point in the history
…n removal (#33381)
  • Loading branch information
ricardogarim committed Sep 27, 2024
1 parent 5965a1d commit 92e366e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-taxis-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fixes a race condition that causes livechat conversations to get stuck in the agent's sidebar panel after being forwarded.
22 changes: 11 additions & 11 deletions apps/meteor/server/models/raw/BaseRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,33 +318,33 @@ export abstract class BaseRaw<

async findOneAndDelete(filter: Filter<T>, options?: FindOneAndDeleteOptions): Promise<ModifyResult<T>> {
if (!this.trash) {
if (options) {
return this.col.findOneAndDelete(filter, options);
}
return this.col.findOneAndDelete(filter);
return this.col.findOneAndDelete(filter, options || {});
}

const result = await this.col.findOneAndDelete(filter);

const { value: doc } = result;
const doc = await this.col.findOne(filter);
if (!doc) {
return result;
return { ok: 1, value: null };
}

const { _id, ...record } = doc;

const trash: TDeleted = {
...record,
_deletedAt: new Date(),
__collection__: this.name,
} as unknown as TDeleted;

// since the operation is not atomic, we need to make sure that the record is not already deleted/inserted
await this.trash?.updateOne({ _id } as Filter<TDeleted>, { $set: trash } as UpdateFilter<TDeleted>, {
upsert: true,
});

return result;
try {
await this.col.deleteOne({ _id } as Filter<T>);
} catch (e) {
await this.trash?.deleteOne({ _id } as Filter<TDeleted>);
throw e;
}

return { ok: 1, value: doc };
}

async deleteMany(filter: Filter<T>, options?: DeleteOptions & { onTrash?: (record: ResultFields<T, C>) => void }): Promise<DeleteResult> {
Expand Down

0 comments on commit 92e366e

Please sign in to comment.