Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition when forwarding livechat by splitting subscription removal #33381

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/dry-taxis-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Adjusts the findOneAndDelete method to insert into the trash collection before deletion to prevent a race condition that causes livechat users to get stuck in the agent's sidebar panel after being forwarded.
ricardogarim marked this conversation as resolved.
Show resolved Hide resolved
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
Loading