From 978ac5a478cbece961b09ae66ed266095620526b Mon Sep 17 00:00:00 2001 From: Elias <69803556+elias1218@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:30:11 -0500 Subject: [PATCH] fix(server): revoked sessions close live dictation streams --- apps/server/src/speech/stream.test.ts | 84 ++++++++++++++++++++++++++- apps/server/src/speech/stream.ts | 9 ++- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/apps/server/src/speech/stream.test.ts b/apps/server/src/speech/stream.test.ts index 71d2fdca9131..d3e2eb8dedc7 100644 --- a/apps/server/src/speech/stream.test.ts +++ b/apps/server/src/speech/stream.test.ts @@ -1,13 +1,21 @@ import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; import { it } from "@effect/vitest"; import { expect } from "vite-plus/test"; +import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Schema from "effect/Schema"; import { HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http"; -import { SpeechStreamEvent, DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS } from "@t3tools/contracts"; -import type { SpeechService, SpeechStream } from "./SpeechService.ts"; -import { runSpeechSocket } from "./stream.ts"; +import { + AuthOrchestrationOperateScope, + AuthSessionId, + SpeechStreamEvent, + DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, +} from "@t3tools/contracts"; +import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; +import * as SessionStore from "../auth/SessionStore.ts"; +import { SpeechService, type SpeechStream } from "./SpeechService.ts"; +import { runSpeechSocket, speechStreamRouteLayer } from "./stream.ts"; import type * as NetAddress from "effect/net/NetAddress"; const decodeEvent = Schema.decodeUnknownSync(Schema.fromJsonString(SpeechStreamEvent)); @@ -167,3 +175,73 @@ it.live("processes pipelined audio before the finish command", () => { }, ).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest)); }); + +it.live("stops in-flight work, closes the socket, and releases the stream on invalidation", () => { + const feeding = Promise.withResolvers(); + const released = Promise.withResolvers(); + let feedInterrupted = false; + const sessionId = AuthSessionId.make("speech-test"); + const stream: SpeechStream = { + feed: () => + Effect.sync(() => feeding.resolve()).pipe( + Effect.andThen(Effect.never), + Effect.onInterrupt(() => Effect.sync(() => (feedInterrupted = true))), + ), + finish: Effect.never, + }; + return Effect.gen(function* () { + const invalidated = yield* Deferred.make(); + yield* Layer.build( + HttpRouter.serve(speechStreamRouteLayer, { + disableLogger: true, + disableListenLog: true, + }).pipe( + Layer.provide( + Layer.mock(EnvironmentAuth.EnvironmentAuth, { + authenticateWebSocketUpgrade: () => + Effect.succeed({ + sessionId, + subject: "speech-test", + method: "bearer-access-token", + scopes: [AuthOrchestrationOperateScope], + proofKeyThumbprint: "thumbprint", + }), + }), + ), + Layer.provide( + Layer.mock(SessionStore.SessionStore, { + cookieName: "test", + legacyCookieName: undefined, + awaitInvalidation: (id, proofKeyThumbprint) => + id === sessionId && proofKeyThumbprint === "thumbprint" + ? Deferred.await(invalidated) + : Effect.never, + }), + ), + Layer.provide( + Layer.mock(SpeechService, { + startStream: () => + Effect.acquireRelease(Effect.succeed(stream), () => + Effect.sync(() => released.resolve(feedInterrupted)), + ), + }), + ), + ), + ); + const server = yield* HttpServer.HttpServer; + const origin = HttpServer.formatAddress(server.address).replace(/^http/, "ws"); + const connection = client(`${origin}/ws/voice`); + yield* Effect.addFinalizer(() => Effect.sync(() => connection.socket.close())); + const closed = new Promise((resolve) => + connection.socket.addEventListener("close", () => resolve(), { once: true }), + ); + expect(yield* Effect.promise(() => connection.next())).toEqual({ type: "ready" }); + connection.socket.send(new Float32Array([0.25])); + yield* Effect.promise(() => feeding.promise); + yield* Deferred.succeed(invalidated, undefined); + const [, interruptedBeforeRelease] = yield* Effect.promise(() => + Promise.all([closed, released.promise]), + ); + expect(interruptedBeforeRelease).toBe(true); + }).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest)); +}); diff --git a/apps/server/src/speech/stream.ts b/apps/server/src/speech/stream.ts index 60d5acb2334e..c08575e9e0f8 100644 --- a/apps/server/src/speech/stream.ts +++ b/apps/server/src/speech/stream.ts @@ -14,6 +14,7 @@ import * as Scope from "effect/Scope"; import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http"; import * as Socket from "effect/socket/Socket"; import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts"; +import * as SessionStore from "../auth/SessionStore.ts"; import * as SpeechService from "./SpeechService.ts"; const decodeCommand = Schema.decodeUnknownEffect(Schema.fromJsonString(SpeechStreamCommand)); @@ -80,6 +81,8 @@ export const runSpeechSocket = Effect.fn("speech.runSocket")(function* ( }); } }).pipe( + // Stops the worker before the stream's slow cleanup, so it cannot send in the meantime. + Effect.scoped, Effect.timeoutOrElse({ duration: "10 minutes", // The socket read loop is already interrupted here; its writer can no longer send. @@ -93,6 +96,7 @@ export const speechStreamRouteLayer = Layer.unwrap( Effect.gen(function* () { const auth = yield* EnvironmentAuth.EnvironmentAuth; const speech = yield* SpeechService.SpeechService; + const sessions = yield* SessionStore.SessionStore; return HttpRouter.add( "GET", SPEECH_STREAM_PATH, @@ -105,7 +109,10 @@ export const speechStreamRouteLayer = Layer.unwrap( if (!session.success.scopes.includes(AuthOrchestrationOperateScope)) return HttpServerResponse.empty({ status: 403 }); const socket = yield* request.upgrade; - yield* runSpeechSocket(socket, speech).pipe(Effect.scoped); + yield* Effect.raceFirst( + runSpeechSocket(socket, speech).pipe(Effect.scoped), + sessions.awaitInvalidation(session.success.sessionId, session.success.proofKeyThumbprint), + ).pipe(Effect.ignore({ log: true })); return HttpServerResponse.empty(); }), );