From f4f0bd171dc4ffb7c5d0b8f26f2b21ca5512068c Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 22 Nov 2026 21:42:04 +0100 Subject: [PATCH] Add more fallback logic to attempt to clear listeners? --- src/gateway/listener/listener.ts | 14 +++++++++++++- src/gateway/util/WebSocket.ts | 4 ++++ src/util/util/ipc/Event.ts | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/gateway/listener/listener.ts b/src/gateway/listener/listener.ts index 9efe0fb40..f5d6afb09 100644 --- a/src/gateway/listener/listener.ts +++ b/src/gateway/listener/listener.ts @@ -19,6 +19,7 @@ import { Channel as AMQChannel } from "amqplib"; import { bgRedBright } from "picocolors"; import { Ban, Member, Message, Recipient, Relationship } from "@spacebar/database"; +import { TimeSpan } from "@spacebar/extensions"; import { EVENTEnum, EventOpts, getPermission, listenEvent, ListenEventOpts, NewUrlUserSignatureData, Permissions, RabbitMQ } from "@spacebar/util"; import { WebSocket } from "@spacebar/gateway"; import { PublicMember, RelationshipType } from "@spacebar/schemas"; @@ -31,7 +32,9 @@ import { CLOSECODES, OPCODES, Send } from "../util"; // Sharding: calculate if the current shard id matches the formula: shard_id = (guild_id >> 22) % num_shards // https://discord.com/developers/docs/topics/gateway#sharding -export function handlePresenceUpdate(this: WebSocket, { event, acknowledge, data }: EventOpts) { +export function handlePresenceUpdate(this: WebSocket, { event, acknowledge, data, cancel }: EventOpts) { + if (this.closedAt && TimeSpan.fromDates(+this.closedAt, +new Date()).totalSeconds > 300) cancel?.(); + acknowledge?.(); if (event === EVENTEnum.PresenceUpdate) { return Send(this, { @@ -157,10 +160,14 @@ export async function setupListener(this: WebSocket) { RabbitMQ.on("disconnected", handleDisconnect); this.rawSocket.once("close", async () => { + console.log("[Gateway/listener] Handling connection close..."); + this.closedAt = new Date(); + // Unsubscribe from RabbitMQ events RabbitMQ.off("reconnected", handleReconnect); RabbitMQ.off("disconnected", handleDisconnect); + console.log(`[Gateway/listener/${this.user_id}] Disconnected RabbitMQ handlers, if any...`); // wait for event consumer cancellation await Promise.all( Object.values(this.events).map((x) => { @@ -168,7 +175,10 @@ export async function setupListener(this: WebSocket) { else return Promise.resolve(); }), ); + console.log(`[Gateway/listener/${this.user_id}] Attempted to disable all event listeners...`); + await Promise.all(Object.values(this.member_events).map((x) => x())); + console.log(`[Gateway/listener/${this.user_id}] Attempted to disable all member event listeners...`); if (opts.channel) { try { @@ -190,6 +200,8 @@ async function consume(this: WebSocket, opts: EventOpts) { const consumer = consume.bind(this); const listenOpts = opts as ListenEventOpts; opts.acknowledge?.(); + + if (this.closedAt && TimeSpan.fromDates(+this.closedAt, +new Date()).totalSeconds > 300) opts.cancel?.(); // console.log("event", event); // deduplicate gateway messages diff --git a/src/gateway/util/WebSocket.ts b/src/gateway/util/WebSocket.ts index 3a3cf1899..cd8a72a83 100644 --- a/src/gateway/util/WebSocket.ts +++ b/src/gateway/util/WebSocket.ts @@ -28,9 +28,13 @@ export class WebSocket { rawSocket: WS; constructor(socket: WS) { this.rawSocket = socket; + this.openedAt = new Date(); } recentTransactions: string[] = []; + openedAt: Date; + closedAt?: Date; + version: number; user_id: string; session_id: string; diff --git a/src/util/util/ipc/Event.ts b/src/util/util/ipc/Event.ts index af7c832df..9546d83fa 100644 --- a/src/util/util/ipc/Event.ts +++ b/src/util/util/ipc/Event.ts @@ -59,7 +59,7 @@ export async function initEvent() { writer = new RabbitMqSingleWriter(Config.get().rabbitmq.host!); await writer.init(); } - } else if (process.env.EVENT_TRANSMISSION === "unix" && process.env.EVENT_SOCKET_PATH) { + } else if (process.env.EVENT_TRANSMISSION === "unix") { if (!process.env.EVENT_SOCKET_PATH) { throw new Error("[Events] EVENT_SOCKET_PATH is not configured."); }