← 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.
Parameters
None. close() takes no arguments; any passed are ignored.
Return value
A Promise that fulfills with undefined once the rendering thread has stopped. Observable effects of a successful close():
statebecomes"closed"and astatechangeevent fires.- Every
startRendering()promise still pending — the chunk in progress and any queued behind it — rejects withAbortError(Chromium message “Audio context is going away”). - Every pending
suspend()promise rejects withInvalidStateError. - No
completeevent fires. - Audio nodes are not freed by
close()itself: the spec notes it “will not automatically release all AudioContext-created objects”. Drop your references so they can be garbage-collected.
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.
closeContext, OnRenderStopped and RejectPendingRenderingResolvers in Chromium offline_audio_context.cc; WPT offlineaudiocontext-close.html
Exceptions
| Rejects with | When |
|---|---|
InvalidStateError | The document is not fully active. |
InvalidStateError | The 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.
Requirements and context
- Receiver: an
OfflineAudioContext,[Exposed=Window]only. - No permission, user activation or secure context.
- Allowed in any non-closed state: before rendering has started (
"suspended"), while"running"(including between chunks), and while"suspended"bysuspend(). - Chrome 158+, flag
OfflineAudioContextIncrementalRendering(stablein runtime_enabled_features.json5).
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().
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
| Browser | Version added |
|---|---|
| Chrome | 158 |
| Chrome Android, WebView, Edge, Opera | Mirror Chrome (158) |
| Firefox | No |
| Safari / Safari iOS | No |
BCD marks the entry experimental: true (single engine) and tags it web-features:offline-audio-context-incremental-rendering.
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.
Specifications
Web Audio API 1.1 — OfflineAudioContext.close(), added by WebAudio/web-audio-api#2675. Tracking: ChromeStatus 5109900936085504.