# tmux_server:with_owned/2

- **Package:** libtmux_erlang
- **Language:** Erlang
- **Kind:** function
- **Source:** https://github.com/libtmux/libtmux-erlang/blob/d1b6308440cf9eb8f6647e7ae434bce60e849368/apps/libtmux_erlang/src/tmux_server.erl#L204
- **Page:** https://libtmux.org/en/erlang/latest/reference/tmux_server-with_owned-2/

```
with_owned(Options, Function)
-spec with_owned(owned_options(), fun((t()) -> Value)) ->
                    {ok, Value} |
                    {error, scope_failure() | map()}.
```

Start an owned daemon, run a callback in the calling process, then stop it. Performs `start_owned/1` and token-based `stop_owned/1` around the callback. With successful cleanup, returns {ok, Value} or re-raises the original callback exception. A returned error tuple is an ordinary callback value. Startup failure passes through without invoking the callback.

Cleanup failure returns `t:scope_failure/0`, including both server and ownership
handles for inspection and an explicit stop retry. It preserves the callback
outcome and cleanup evidence separately; it never claims timeout is retirement.
See `with_server/2` for callback and separately allocated resource obligations.

## Parameters

- `Options` (`owned_options()`)
- `Function` (`fun((t()) -> Value)`)

## Guides

Pages that discuss `with_owned/2`.

### Guides

- [Native API documentation](/en/erlang/latest/guides/api-documentation/)
- [Getting started](/en/erlang/latest/guides/getting-started/)

### Topics

- [Ownership and identity](/en/erlang/latest/topics/ownership/)
