⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(ws): discard obsolete revocation retries after access restoration
Tin69333e5d7742
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.tsindex bbfad362f..bf6a0ea9a 100644--- a/apps/api/src/auth.ts+++ b/apps/api/src/auth.ts@@ -534,7 +534,7 @@ export const auth = betterAuth({await revokeWorkspaceConnections(member.userId,member.organizationId,- { force: true },+ { role: user.role ?? null },);}void syncWorkspaceSeats(member.organizationId).catch((error) => {diff --git a/apps/api/src/ws/index.ts b/apps/api/src/ws/index.tsindex 7e7a87fc5..b59105c8e 100644--- a/apps/api/src/ws/index.ts+++ b/apps/api/src/ws/index.ts@@ -238,14 +238,17 @@ function revokeLocalWorkspaceConnections(userId: string, workspaceId: string) {export async function revokeWorkspaceConnections(userId: string,workspaceId: string,- options: { force?: boolean } = {},+ options: { force?: boolean; role?: string | null } = {},) {if (!options.force) {try {- const [user] = await db- .select({ userId: userTable.id, role: userTable.role })- .from(userTable)- .where(eq(userTable.id, userId));+ const [user] =+ "role" in options+ ? [{ role: options.role }]+ : await db+ .select({ userId: userTable.id, role: userTable.role })+ .from(userTable)+ .where(eq(userTable.id, userId));if (hasInstanceAdminRole(user?.role)) return;} catch (error) {console.error("Failed to read role after membership removal:", error);@@ -255,11 +258,33 @@ export async function revokeWorkspaceConnections(type: "WORKSPACE_ACCESS_REVOKED",workspaceId,});- await revocationDelivery?.send({- userId,- message: { type: "WORKSPACE_ACCESS_REVOKED", workspaceId },- origin: INSTANCE_ID,- });+ await revocationDelivery?.send(+ {+ userId,+ message: { type: "WORKSPACE_ACCESS_REVOKED", workspaceId },+ origin: INSTANCE_ID,+ },+ options.force+ ? undefined+ : async () => {+ const [[user], members] = await Promise.all([+ db+ .select({ role: userTable.role })+ .from(userTable)+ .where(eq(userTable.id, userId)),+ db+ .select({ userId: workspaceUserTable.userId })+ .from(workspaceUserTable)+ .where(+ and(+ eq(workspaceUserTable.userId, userId),+ eq(workspaceUserTable.workspaceId, workspaceId),+ ),+ ),+ ]);+ return !hasInstanceAdminRole(user?.role) && members.length === 0;+ },+ );}const workspaceLookups = new Map<string, Promise<string | null>>();diff --git a/apps/api/src/ws/revocation-delivery.ts b/apps/api/src/ws/revocation-delivery.tsindex 849009a62..cbd260022 100644--- a/apps/api/src/ws/revocation-delivery.ts+++ b/apps/api/src/ws/revocation-delivery.ts@@ -1,15 +1,22 @@import type { BroadcastAdapter, UserBroadcast } from "./broadcast-adapter";export function createRevocationDelivery(adapter: BroadcastAdapter) {- const pending = new Map<string, UserBroadcast>();+ type Pending = {+ message: UserBroadcast;+ shouldRetry?: () => Promise<boolean>;+ };+ const pending = new Map<string, Pending>();let timer: ReturnType<typeof setTimeout> | undefined;let stopped = false;let delay = 1_000;- const attempt = async (key: string, message: UserBroadcast) => {+ const attempt = async (key: string, entry: Pending, retry = false) => {try {- await adapter.publishToUser(message);- if (pending.get(key) === message) pending.delete(key);+ if (!retry || !entry.shouldRetry || (await entry.shouldRetry())) {+ if (stopped || pending.get(key) !== entry) return;+ await adapter.publishToUser(entry.message);+ }+ if (pending.get(key) === entry) pending.delete(key);} catch {// Membership is already removed. Keep the signal until Redis recovers.}@@ -19,7 +26,7 @@ export function createRevocationDelivery(adapter: BroadcastAdapter) {timer = setTimeout(async () => {timer = undefined;await Promise.all(- [...pending].map(([key, message]) => attempt(key, message)),+ [...pending].map(([key, entry]) => attempt(key, entry, true)),);delay = pending.size ? Math.min(delay * 2, 30_000) : 1_000;schedule();@@ -27,11 +34,12 @@ export function createRevocationDelivery(adapter: BroadcastAdapter) {timer.unref();};return {- async send(message: UserBroadcast) {+ async send(message: UserBroadcast, shouldRetry?: () => Promise<boolean>) {if (stopped) return;const key = JSON.stringify([message.userId, message.message.workspaceId]);- pending.set(key, message);- await attempt(key, message);+ const entry = { message, shouldRetry };+ pending.set(key, entry);+ await attempt(key, entry);schedule();},stop() {diff --git a/apps/web/src/lib/evict-workspace-cache.test.ts b/apps/web/src/lib/evict-workspace-cache.test.tsindex f7990a6ba..2cbdf83e3 100644--- a/apps/web/src/lib/evict-workspace-cache.test.ts+++ b/apps/web/src/lib/evict-workspace-cache.test.ts@@ -42,6 +42,9 @@ it("cancels unknown project settings reads when membership is revoked", async ()const client = new QueryClient();for (const prefix of ["github-integration",+ "github-repositories",+ "gitea-repositories",+ "gitlab-projects","columns","workflow-rules","custom-fields",diff --git a/apps/web/src/lib/evict-workspace-cache.ts b/apps/web/src/lib/evict-workspace-cache.tsindex 68995c35b..5a74938cf 100644--- a/apps/web/src/lib/evict-workspace-cache.ts+++ b/apps/web/src/lib/evict-workspace-cache.ts@@ -62,6 +62,10 @@ export function evictWorkspaceCache(client: QueryClient, workspaceId: string) {scopes.get(value) !== workspaceId,);const privatePrefixes = new Set([+ "github-repositories",+ "gitea-repositories",+ "gitlab-projects",+ "active-organization","github-integration","gitlab-integration","gitea-integration",diff --git a/tests/api/ws/project-move.test.ts b/tests/api/ws/project-move.test.tsindex 4d2a56c6e..17abdee90 100644--- a/tests/api/ws/project-move.test.ts+++ b/tests/api/ws/project-move.test.ts@@ -328,3 +328,27 @@ it("retries revocation delivery after Redis recovers without a project broadcastawait vi.advanceTimersByTimeAsync(60_000);expect(m.publish).toHaveBeenCalledTimes(2);});++it("drops a queued membership revocation when access is restored before Redis recovery", async () => {+ vi.useFakeTimers();+ m.redis = true;+ await initializeWebSocketAdapter();+ m.publish.mockRejectedValueOnce(new Error("Redis unavailable"));+ await revokeWorkspaceConnections("user", "old", { role: "user" });+ const reconnected = connect();+ m.members.mockResolvedValue([{ userId: "user" }]);+ await vi.advanceTimersByTimeAsync(60_000);+ expect(m.publish).toHaveBeenCalledTimes(1);+ expect(reconnected.close).not.toHaveBeenCalled();+});++it("retries a membership revocation while access remains absent", async () => {+ vi.useFakeTimers();+ m.redis = true;+ await initializeWebSocketAdapter();+ m.members.mockResolvedValue([]);+ m.publish.mockRejectedValueOnce(new Error("Redis unavailable"));+ await revokeWorkspaceConnections("user", "old", { role: "user" });+ await vi.advanceTimersByTimeAsync(1_000);+ expect(m.publish).toHaveBeenCalledTimes(2);+});