# Ownership and cleanup

Source: https://libtmux.org/en/powershell/latest/topics/ownership/

> Keep daemon, client, session, and runspace lifetimes explicit.

A server handle does not own the tmux daemon. Most cmdlets borrow the native
objects supplied to them. Decide who creates and removes each resource before
putting operations in a pipeline or background job.

## Match cleanup to what you created

| Resource | Lifetime rule |
| --- | --- |
| Endpoint from [`New-TmuxServer`](<https://libtmux.org/en/powershell/latest/reference/libtmux-new-tmuxserver/>) | A description; constructing it starts no daemon. |
| An existing server or session | Remains under its existing owner's control. |
| A session your example creates | Remove that session in cleanup when its work ends. |
| Client from [`Connect-TmuxControl`](<https://libtmux.org/en/powershell/latest/reference/libtmux-connect-tmuxcontrol/>) | Close explicitly with [`Disconnect-TmuxControl`](<https://libtmux.org/en/powershell/latest/reference/libtmux-disconnect-tmuxcontrol/>). |
| Client created internally by [`Watch-TmuxEvent`](<https://libtmux.org/en/powershell/latest/reference/libtmux-watch-tmuxevent/>) | The watcher closes it when its reader ends. |
| A thread job you start | Stop and remove the job, including after a failed receive. |

Disconnecting a control client closes that client for every caller sharing it.
It does not mean the daemon should be killed. Cleanup awaits native disposal;
that operation has no cancellation token.

## Use an isolated endpoint for automation

The complete examples choose a unique socket name and a clean tmux configuration.
They remove only the session they create. This makes the resource boundary
visible and avoids attaching an example's lifetime to your interactive server.

Do not treat `-Confirm:$false` as an ownership check. It suppresses a confirmation
prompt; your code must still target the resources it is responsible for.
Keep useful `-WhatIf` behavior in operator-facing scripts.

## Preserve failures during cleanup

Use a `finally` scope to release resources after success, operation failure, or
an early exit. If cleanup fails too, retain both errors rather than replacing the
original failure. The [create-layout program](https://libtmux.org/en/powershell/latest/examples/create-layout/)
shows that handling in full.

Module removal ends active watchers in that runspace. It does not close clients
previously returned by [`Connect-TmuxControl`](<https://libtmux.org/en/powershell/latest/reference/libtmux-connect-tmuxcontrol/>). A handle transferred to another
caller therefore still needs an agreed owner.

Thread jobs run in the same process and preserve native objects. Process jobs
serialize output and cannot be used to transfer a live native handle unchanged.
Create and close a client's resources in the worker that uses them when running
independent work. See [runspaces and watchers](https://libtmux.org/en/powershell/latest/guides/watch/) for bounded
job recipes and [control clients](https://libtmux.org/en/powershell/latest/guides/commands/) for borrowed-client
examples.
