Skip to main content
A client holds the indexes it loaded, their refresh pollers and the embedding model. A session holds its documents and vectors. Garbage collection frees them eventually; close() frees them now and sends the final usage report. Close every client and session when you are done with it.

Close when you are done

In JavaScript, MossClient and SessionIndex support await using, which closes them when the enclosing block ends. In Python, both are context managers, so async with does the same.
await using needs Node.js 24 or later. On older versions, call close() in a finally block:
In synchronous Python code, with works too. It blocks the calling thread while the final report is sent, so prefer async with inside async code.
close(), with and async with need moss >= 1.15.0 (Python). Every @moss-js/moss release (JavaScript) has close() and await using, and the 2-second bound on the final report needs >= 1.16.0. In Swift the bound needs >= 0.8.0, and in C >= 0.24.0.

What close does

  1. Sends the final usage report. It waits until the report arrives or 2 seconds pass, so close() can take up to 2 seconds.
  2. Frees local data. It stops auto-refresh polling, unloads the indexes, and frees the documents and vectors, including memory-mapped vector files.
  3. Lets go of the embedding model. One copy of each model is shared by every client and session in the process that uses it. It is freed when the last of them closes, and loaded again on next use. Process memory may not shrink right away, because the allocator keeps freed pages for reuse.
Closing is safe to repeat. A closed object can’t be reopened: create a new client instead. After close():
  • JavaScript: client methods reject with MossClient is closed. A closed session still reads name and modelId; everything else throws.
  • Python: client methods raise RuntimeError("MossClient is closed"). A closed session still reads name; everything else raises InvalidArgumentError.
In JavaScript and Python, closing a client also closes the sessions it opened. A client or session that is garbage collected without close() still tries to send its final report, but a process that exits first can lose the last few seconds of usage. In Python, each client and session also keeps its own background threads until it is garbage collected. close() frees the memory but not those threads.

Other SDKs

Close idle clients

An app that searches in bursts, such as a search box or a desktop assistant, can close its client after a quiet period and create a new one on the next search. Memory stays free between bursts. The first search after a close pays for loading the index and the model again. Give the client a cache directory so the reload reads the index from disk when the cloud copy hasn’t changed; see Storage & Persistence.

Sessions

Create, query and push a session.

Telemetry

What the final usage report contains.