The runtime schedules asynchronous requests and owns their cleanup. Its connection and snapshot API captures state explicitly. Commands, creation and session observation use the same runtime.
Choose an adapter:
libtmux.runtime.luvfor a standalone Lua process.run(body, limits)drives a quiescent top-level loop and returnsvalue, erronce owned work has finished. It rejects coroutine entry, Neovim, active borrowed handles and nested calls from a Lua-driven luv loop. A C host that drives libuv without a Luauv.runframe is outside this contract. Importing the adapter does not load luv or start a loop.libtmux.runtime.nvimin Neovim.start(body, on_done, limits)returns the runtime and root Request at once. It borrows the host loop and schedules callbacks outside fast events.on_done(value, err)runs after owned cleanup; unrelated host handles stay open.
connect, start_server and with_server use the first executable regular
file named tmux on the caller's PATH. Each request captures PATH and the
current directory at submission and reuses the selected absolute path. With
PATH unset, the lookup checks /usr/bin and /bin. An empty entry checks the
captured working directory.
Pass an absolute binary to select a particular build. Use
runtime:find_tmux():await() to get the path without launching tmux or
contacting a server. A search with no executable returns invalid_endpoint
before sending a command.
runtime:start_server(options):await() starts a tmux server this runtime owns
and resolves to an OwnedServer. connect only binds a server
that already runs. The tested programs are
start_server.lua and
with_server.lua.
| Option | Default | Meaning |
|---|---|---|
binary |
tmux on PATH |
Absolute tmux executable override |
config |
/dev/null |
Absolute configuration file; the default loads none |
root |
system temporary directory | Absolute directory that holds the private one |
environment |
none | NAME = value entries for the daemon and its panes |
timeout |
5000 | Milliseconds for the launch command and each stop attempt |
After launch, the runtime gives tmux a separate 750 ms to report the daemon's
identity. Increasing timeout leaves that check at 750 ms. If it expires,
start_server returns uncertain_generation with
err.cause.code == "deadline_exceeded". A failed launch command instead
returns start_failed with its cause.
- The server lives in a new
libtmux-XXXXXXdirectory underroot, with its sockettmux.sockinside. The directory is private to the caller. - The daemon inherits your environment without
TMUX,TMUX_PANEand everyLIBTMUX_SOCKET*variable.environmentis added, andTMUX_TMPDIRnames the private directory.environmentcannot set those names. - The daemon runs with
exit-empty off, so it stays up with no session. The call resolves once the server answers; create sessions withnew_session. - If the version check refuses the daemon, the runtime attempts to stop it
and remove its directory before returning
unsupported_version. If the daemon still answers, runtime shutdown retries cleanup. - The directory name and tmux's socket alias must fit
sun_path: 107 bytes on Linux, 103 on macOS and the BSDs. Arootmore than 45 bytes under that limit fails withinvalid_endpointbefore anything is created. Withoutroot, the system temporary directory is used, or/tmpwhen it is too long.
OwnedServer has server, the same handle connect returns, and
socket_path, which a second handle can connect to.
server:close() releases the handle and leaves the daemon running.
The connection example prints the name of the
work session on the socket in LIBTMUX_SOCKET_PATH. With that variable
unset or empty, it creates work on a private server and stops it afterward.
owned:stop():await() ends the server:
- It runs
kill-server, connects to the socket until it is refused or gone, and removes the directory. Each wait is bounded bytimeout. - A daemon that still answers fails with
stop_failed. The directory stays anderr.directorynames it. Callingstopagain tries once more. - A server the program left under the directory is stopped too, and
stopfails withleaked_server.err.socketslists each socket, relative to the directory. - After a successful stop, calling it again returns
true.
The runtime stops every owned server that is still running when it ends,
whether the body returned, failed or was cancelled. A stop that fails then
fails the run with cleanup_failed. Cleanup does not run when the process is
killed with SIGKILL: the daemon and its directory stay.
runtime:with_server(options, body) starts a server, calls
body(server, owned) and stops the server whether the body returns a value or
an error or raises one. It resolves to what the body returns. The body runs in
the calling task, so it can await.
The body receives its runtime. runtime:spawn(body) creates a child task and
returns its Request at once.
- Tasks return
value, err. A non-nil error or an exception fails the enclosing task scope. - Library operations isolate operational failures in the Request they return. An unhandled failure in a task you spawned still fails the root.
- Returning normally joins child tasks, requests and deferred callbacks.
- A task yields only through runtime operations. Lua CPU work is not preemptible.
Waiting:
request:await()returnsvalue, errfrom a managed coroutine in the same runtime. A wait from the main thread, a foreign coroutine or another runtime raisesinvalid_await_context. Several tasks can wait on one Request.request:on_complete(fn)registers a deferredfn(value, err). Registering after settlement stays deferred while the runtime is live. After the root retires or the runtime closes it raisesclosed, and exceeding the callback or retained-byte limits raisesqueue_fullat once. A callback failure fails its scope, or appears inruntime:errors()when the scope has already failed or retired.request:result()reads a settled result without waiting. An unsettled one returnsnil, errwith codepending.is_settled()is caller completion andis_retired()is transport cleanup. Cancellation can settle before retirement.
- The task that creates an operation owns its cancellation. Cancelling a task that waits on it only detaches the wait.
request:cancel(reason)cancels the operation.runtime:close(reason)rejects new work, cancels owned work and returns the root Request. Repeated closes are safe.- Transport errors keep
effect:not_sent,unknownorcompleted. Cancelling a tmux client cannot prove that accepted daemon work stopped. Cleanup failures stay visible even after a result settled.
| Limit | Default |
|---|---|
max_active |
16 |
max_pending |
128 |
max_logical |
128 |
max_bytes |
16 MiB |
max_tasks |
128 |
max_callbacks |
256 |
max_resources |
128 |
dispatch_budget |
64 |
Pass overrides as the adapter's limits table. runtime:stats() exposes the
counters.
- Byte accounting covers queued callback and waiter delivery until transport retirement and delivery finish. Values you keep belong to your own budget.
max_callbackscounts outstanding registrations throughrequest:on_complete. A callback onServer:batch()orwatch:close()uses one slot. The runtime owns the continuations that run those operations.- Timers use monotonic milliseconds and reject delays above 2,147,483,647 ms.
- One-shot waits owned by the runtime, such as an observation's next event, use
the separate
max_logicallimit until retirement and take no active or pending process slot. A normal root return joins them. Cancel them, close their producer or set a deadline when no event is expected. Cancelling a borrowed waiter only detaches that waiter. - Persistent native resources use private leases outside the process slots. A
lease can own one bounded byte reservation for its buffers, which counts
toward
max_bytesandstats().resource_bytes. Unreleased bytes reportcleanup_failed, stay charged and count inresources_failed. Leases and raw Request constructors are private. - A host failure triggers bounded cleanup. If it cannot finish,
cleanup_failedreports the remaining counters rather than successful retirement. - The runtime never closes a borrowed host loop or kills a tmux server as part of cancelling a request.
See the development checks for the luv and Neovim host probes, and compatibility targets.
LuaLS completes runtime, Request, Server and snapshot chains in both adapter
bodies, related pane and window fields included, and keeps the standalone run
return type. It cannot infer the body result's members in Neovim's separate
on_done callback: annotate that callback's result parameter when you want
completion there.