diff --git a/CHANGELOG.md b/CHANGELOG.md index 059466b..8e4ad5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [4.43.5] + +- Fix concurrent `StreamingTranscriber.close()` calls. A call made while an earlier one was waiting for the `Termination` frame replaced the earlier call's wait and sent a second `Terminate` frame, so the earlier call never settled (with `terminationTimeout` `0`, or when the later call passed `waitForSessionTermination: false`) or rejected with `TypeError: Cannot read properties of undefined (reading 'close')`. A call made while another is running now returns the same promise, and the earlier call's `waitForSessionTermination` and `terminationTimeout` apply + ## [4.43.4] - Add `assemblyai-stt-3-7-realtime` (note taking) and `assemblyai-stt-3-7-realtime-agent` (voice agents) to the streaming `speechModel` options (`StreamingSpeechModel`) diff --git a/package.json b/package.json index 392e231..fb5b85e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "assemblyai", - "version": "4.43.4", + "version": "4.43.5", "description": "The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription and text-to-speech.", "engines": { "node": ">=18" diff --git a/src/services/streaming/service.ts b/src/services/streaming/service.ts index 2c3bc19..bf1d6e1 100644 --- a/src/services/streaming/service.ts +++ b/src/services/streaming/service.ts @@ -255,6 +255,8 @@ export class StreamingTranscriber { private socket?: PolyfillWebSocket; private listeners: StreamingListeners = {}; private sessionTerminatedResolve?: (event?: TerminationEvent) => void; + /** The `close()` in progress, shared by calls made while it runs. */ + private closing?: Promise; /** True from `Begin` until the `close` listener has been called. */ private closePending = false; @@ -1255,10 +1257,30 @@ Learn more at https://github.com/AssemblyAI/assemblyai-node-sdk/blob/main/docs/c * for it or it did not arrive before the timeout or the socket closed. * The `close` listener is called before this resolves, unless the session * never opened or the socket had already closed and reported it. + * + * A call made while an earlier `close()` is still running does not start a + * second one: it returns the earlier call's promise, so it settles with the + * same result, and the earlier call's `waitForSessionTermination` and + * `terminationTimeout` apply. */ - async close( + close( waitForSessionTermination = true, terminationTimeout = DEFAULT_TERMINATION_TIMEOUT_MS, + ): Promise { + if (!this.closing) { + this.closing = this.closeSession( + waitForSessionTermination, + terminationTimeout, + ).finally(() => { + this.closing = undefined; + }); + } + return this.closing; + } + + private async closeSession( + waitForSessionTermination: boolean, + terminationTimeout: number, ): Promise { let termination: TerminationEvent | undefined; if (this.flushTimer) { diff --git a/tests/unit/streaming-concurrent-close.test.ts b/tests/unit/streaming-concurrent-close.test.ts new file mode 100644 index 0000000..ef5c749 --- /dev/null +++ b/tests/unit/streaming-concurrent-close.test.ts @@ -0,0 +1,143 @@ +jest.mock("ws", () => require("./mocks/ws")); + +import WS from "jest-websocket-mock"; +import { StreamingTranscriber } from "../../src"; + +const websocketBaseUrl = "wss://localhost:1234/v3/ws"; +const termination = { + type: "Termination", + audio_duration_seconds: 12, + session_duration_seconds: 14, +}; +const terminate = JSON.stringify({ type: "Terminate" }); + +let server: WS; +let rt: StreamingTranscriber; +let onClose: jest.Mock; +let onTermination: jest.Mock; + +/** Settles with the outcome, or "pending" when `p` has not settled by `ms`. */ +function outcome(p: Promise, ms = 300): Promise { + return Promise.race([ + p.then( + (value) => ({ resolved: value }), + (error: Error) => ({ rejected: error.message }), + ), + new Promise((resolve) => setTimeout(() => resolve("pending"), ms)), + ]); +} + +function terminateFramesReceived(): number { + return server.messages.filter((m) => m === terminate).length; +} + +function expectTornDown() { + const state = rt as unknown as { + socket?: unknown; + listeners: Record; + sessionTerminatedResolve?: unknown; + }; + expect(state.socket).toBeUndefined(); + expect(state.listeners).toEqual({}); + expect(state.sessionTerminatedResolve).toBeUndefined(); +} + +describe("concurrent StreamingTranscriber.close() calls", () => { + beforeEach(async () => { + server = new WS(websocketBaseUrl); + rt = new StreamingTranscriber({ + websocketBaseUrl, + apiKey: "123", + sampleRate: 16_000, + }); + onClose = jest.fn(); + onTermination = jest.fn(); + rt.on("close", onClose); + rt.on("termination", onTermination); + const connectPromise = rt.connect(); + await server.connected; + server.send(JSON.stringify({ type: "Begin", id: "123", expires_at: 1 })); + await connectPromise; + }); + + afterEach(() => { + WS.clean(); + }); + + it("resolves every waiting call with the one Termination frame", async () => { + const first = rt.close(true, 0); + const second = rt.close(true, 0); + await expect(server).toReceiveMessage(terminate); + server.send(JSON.stringify(termination)); + + await expect(outcome(first)).resolves.toEqual({ resolved: termination }); + await expect(outcome(second)).resolves.toEqual({ resolved: termination }); + expect(terminateFramesReceived()).toBe(1); + expect(onTermination).toHaveBeenCalledTimes(1); + expect(onClose).toHaveBeenCalledTimes(1); + expect(onClose).toHaveBeenCalledWith(1000, "Session ended"); + expectTornDown(); + }); + + it("does not reject the earlier call when a later call finishes first", async () => { + const first = rt.close(true, 150); + const second = rt.close(true, 500); + await expect(server).toReceiveMessage(terminate); + setTimeout(() => server.send(JSON.stringify(termination)), 50); + + await expect(outcome(first)).resolves.toEqual({ resolved: termination }); + await expect(outcome(second)).resolves.toEqual({ resolved: termination }); + expect(onClose).toHaveBeenCalledTimes(1); + expectTornDown(); + }); + + it("resolves every call with undefined when the server stays silent", async () => { + const first = rt.close(true, 100); + const second = rt.close(true, 100); + + await expect(outcome(first)).resolves.toEqual({ resolved: undefined }); + await expect(outcome(second)).resolves.toEqual({ resolved: undefined }); + expect(terminateFramesReceived()).toBe(1); + expect(onClose).toHaveBeenCalledTimes(1); + expect(onClose).toHaveBeenCalledWith(1000, "Closed by the client"); + expectTornDown(); + }); + + it("applies the first call's policy to a later close(false)", async () => { + const first = rt.close(true, 0); + const second = rt.close(false); + await expect(server).toReceiveMessage(terminate); + server.send(JSON.stringify(termination)); + + await expect(outcome(first)).resolves.toEqual({ resolved: termination }); + await expect(outcome(second)).resolves.toEqual({ resolved: termination }); + expect(terminateFramesReceived()).toBe(1); + expect(onClose).toHaveBeenCalledTimes(1); + expectTornDown(); + }); + + it("resolves two close(false) calls without waiting", async () => { + const first = rt.close(false); + const second = rt.close(false); + + await expect(outcome(first, 50)).resolves.toEqual({ resolved: undefined }); + await expect(outcome(second, 50)).resolves.toEqual({ + resolved: undefined, + }); + expect(onClose).toHaveBeenCalledTimes(1); + expectTornDown(); + }); + + it("starts a fresh close() once the earlier one has finished", async () => { + const first = rt.close(true, 0); + await expect(server).toReceiveMessage(terminate); + server.send(JSON.stringify(termination)); + await expect(first).resolves.toEqual(termination); + + // Nothing left to close: a new call resolves at once, without the frame. + await expect(outcome(rt.close(true, 0), 50)).resolves.toEqual({ + resolved: undefined, + }); + expect(onClose).toHaveBeenCalledTimes(1); + }); +});