← OfflineAudioContext incremental rendering

v158 · web audio · method reference

OfflineAudioContext.close()

Stops an OfflineAudioContext permanently: rendering halts, any chunks still queued by startRendering() are cancelled, and state becomes "closed". It is the only way to end an indefinite-length (length: null) render, and it can also abandon a long finite render early.

Syntax

// Web Audio API 1.1, OfflineAudioContext (IDL verbatim)
Promise<undefined> close();

// Usage
await offlineCtx.close();

Chromium: [RuntimeEnabled=OfflineAudioContextIncrementalRendering, RaisesException, CallWith=ScriptState, ImplementedAs=closeContext] Promise<undefined> close(); — the same flag as startRendering(chunkSize), which is why 'close' in OfflineAudioContext.prototype is the recommended feature test. AudioContext has had its own close() for years; this is the first one on OfflineAudioContext.

Source: Web Audio API 1.1 §1.3.3 — close(); Chromium offline_audio_context.idl

Parameters

None. close() takes no arguments; any passed are ignored.

Source: close() IDL

Return value

A Promise that fulfills with undefined once the rendering thread has stopped. Observable effects of a successful close():

The spec algorithm specifies the promise, the state change and statechange; the cancellation of pending render and suspend promises is Chromium behavior asserted by the WPT suite.

Source: close() algorithm; closeContext, OnRenderStopped and RejectPendingRenderingResolvers in Chromium offline_audio_context.cc; WPT offlineaudiocontext-close.html

Exceptions

close() rejections
Rejects withWhen
InvalidStateErrorThe document is not fully active.
InvalidStateErrorThe context is already closed — a second close() (even while the first is still pending), or close() after a finite render closed itself. Chromium: “cannot close a closed offline context”.

Because a finite context auto-closes after its last chunk, guard with if (ctx.state !== 'closed') await ctx.close(); when the same cleanup path serves both kinds of render.

Source: close() algorithm steps 1–3; Chromium offline_audio_context.cc; “calling close twice rejects the second call” in WPT close

Requirements and context

Source: Web Audio API 1.1 §1.3 IDL; “close before rendering”, “close while rendering”, “close while suspended” tests in WPT close

Lifecycle and state transitions

"suspended" / "running" → "closed", and "closed" is terminal. On the control thread close() marks the context closed and queues a control message; on the rendering thread rendering stops, then a task resolves the promise, sets state and fires statechange. After that, startRendering(), suspend(), resume() and close() all reject with InvalidStateError. If the close runs because the document is unloading, the spec skips notifying the page. A finite render reaches the same terminal state on its own after its final chunk; an indefinite render only gets there through close().

Source: close() and “control message to close” steps; resume(); suspend()

Examples

Stop an open-ended render with close()

const ctx = new OfflineAudioContext({ numberOfChannels: 2, sampleRate: 48000 }); // length: null
// ...graph...
const stop = new AbortController();
cancelButton.addEventListener('click', () => stop.abort());

while (!stop.signal.aborted) {
  const chunk = await ctx.startRendering(48000); // one second
  await writer.write(toWav(chunk));
}
await ctx.close(); // ctx.state === "closed"

Cancel queued chunks with close()

const pending = [ctx.startRendering(4096), ctx.startRendering(4096)];
await ctx.close();
const results = await Promise.allSettled(pending);
// Any chunk not finished before close() is { status: "rejected", reason: AbortError }
Source: gendn-derived from the close() algorithm and WPT close; interactive version in the lifecycle-and-close showcase demo

Browser compatibility

OfflineAudioContext.close() support (BCD)
BrowserVersion added
Chrome158
Chrome Android, WebView, Edge, OperaMirror Chrome (158)
FirefoxNo
Safari / Safari iOSNo

BCD marks the entry experimental: true (single engine) and tags it web-features:offline-audio-context-incremental-rendering.

Source: BCD api/OfflineAudioContext.json — close (checked 2026-11-22); webstatus.dev

Security and privacy

close() only releases resources the page already owns (the offline rendering thread and its queued buffers); it reveals nothing new and needs no permission. It lets a page bound the lifetime of an open-ended render, which would otherwise keep a rendering thread allocated until the context is garbage-collected. The Web Audio privacy considerations are unaffected.

Source: close(); Web Audio API 1.1 §8

Specifications

Web Audio API 1.1 — OfflineAudioContext.close(), added by WebAudio/web-audio-api#2675. Tracking: ChromeStatus 5109900936085504.