# LibTmux.Control.with_connection/2

- **Module:** LibTmux
- **Package:** libtmux
- **Language:** Elixir
- **Kind:** function
- **Source:** https://github.com/libtmux/libtmux-elixir/blob/66b9b1c3f3f3186428abdfda2c76f5cf87446710/lib/libtmux/control.ex#L106
- **Page:** https://libtmux.org/en/elixir/latest/reference/libtmux-control-with_connection-2/

```
with_connection(options, callback)
@spec with_connection([scope_option()], (t() -> value)) ::
        {:ok, value}
        | {:error,
           LibTmux.Error.t()
           | LibTmux.Control.ScopeFailure.t(value)}
      when value: var
```

Runs a callback with one connection and owns its native and supervisor cleanup.

Takes the explicit server, existing session and host qualification options
from `start_link/1`. The callback runs in the calling process. Its duration
is separate from startup and `cleanup_timeout` (default 950ms). A dedicated
connection owner also requests cleanup when that caller dies.

Returns `{:ok, value}` after proven connection retirement and Root exit.
Cleaned-up exceptions, throws and exits are re-raised with their original
stacktrace. If cleanup fails, returns `{:error, ScopeFailure}` containing the
callback's original value or exception and separate cleanup evidence.
Startup failures return `{:error, Error}`; invalid options raise.

The control handle is scoped to the callback. Explicit reconnect is allowed
inside it; cleanup permanently fences new epochs and retires the current one.
Escaped handles cannot reconnect after the scope. Startup retains `start_link/1`'s OTP initialization
limitation; a timeout can report unknown cleanup while its owner is still
unwinding startup. It does not promise a hard native process-exit deadline.

    alias LibTmux.Control

    Control.with_connection(options, fn control ->
      Control.execute(control, LibTmux.Control.Request.field("pid"))
    end)

## Type parameters

- `value` (bound `var`)

## Parameters

- `options` (`[scope_option()]`)
- `callback` (`(t() -> value)`)
