Skip to content

Commit bb11bf4

Browse files
authored
regression: timed status sometimes never reverts after expiring (#41047)
1 parent 6fa5378 commit bb11bf4

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

ee/packages/presence/src/Presence.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export class Presence extends ServiceClass implements IPresence {
4848

4949
private expirationTimeout?: NodeJS.Timeout;
5050

51+
private expirationScheduleToken?: symbol;
52+
5153
constructor() {
5254
super();
5355

@@ -131,10 +133,19 @@ export class Presence extends ServiceClass implements IPresence {
131133
}
132134

133135
private async setupNextExpiration(): Promise<void> {
136+
const token = Symbol();
137+
this.expirationScheduleToken = token;
138+
139+
const next = await Users.findNextStatusExpiration();
140+
141+
// A newer reschedule replaced our token while we awaited the lookup; let it arm the timer.
142+
if (this.expirationScheduleToken !== token) {
143+
return;
144+
}
145+
134146
clearTimeout(this.expirationTimeout);
135147
this.expirationTimeout = undefined;
136148

137-
const next = await Users.findNextStatusExpiration();
138149
if (!next?.statusExpiresAt) {
139150
return;
140151
}
@@ -174,7 +185,9 @@ export class Presence extends ServiceClass implements IPresence {
174185

175186
override async stopped(): Promise<void> {
176187
this.reaper.stop();
188+
this.expirationScheduleToken = undefined;
177189
clearTimeout(this.expirationTimeout);
190+
this.expirationTimeout = undefined;
178191
clearTimeout(this.lostConTimeout);
179192
}
180193

packages/models/src/models/Users.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ export class UsersRaw extends BaseRaw<IUser, DefaultFields<IUser>> implements IU
11331133

11341134
findNextStatusExpiration() {
11351135
return this.findOne<Pick<IUser, '_id' | 'statusExpiresAt'>>(
1136-
{ statusExpiresAt: { $gte: new Date() } },
1136+
{ statusExpiresAt: { $exists: true } },
11371137
{ projection: { _id: 1, statusExpiresAt: 1 }, sort: { statusExpiresAt: 1 } },
11381138
);
11391139
}

0 commit comments

Comments
 (0)