# tmux_server:with_server/2

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

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

Open a borrowed endpoint, run a callback in the calling process, then close it. Performs the same I/O as `open/1` and `close/1`; the borrowed daemon survives. Startup failure passes through without invoking the callback.

With successful cleanup, returns {ok, Value} for any callback return value,
including a returned error tuple. Callback error, throw and exit exceptions
are re-raised with their original reason and stack. If cleanup fails, returns
{error, Failure} with `t:scope_failure/0`, preserving both outcomes and the
endpoint for explicit recovery. It attempts cleanup once; a timeout does not
prove retirement. Existing supervised cleanup ownership remains in force.

The callback must retire separately created resources such as `m:tmux_control`
attachments. This scope closes its endpoint, not every resource the callback
may allocate. Use `with_owned/2` when the scope should own the daemon too.

## Parameters

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

## Guides

Pages that discuss `with_server/2`.

### Guides

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

### Topics

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