# Server.withControlMode(attachingTo:_:)

- **Module:** Server
- **Package:** libtmux-swift
- **Language:** Swift
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-swift/blob/f02a4668570e1cc5198c941413750e021f42c214/Sources/LibTmux/ControlMode.swift#L167
- **Page:** https://libtmux.org/reference/swift/server-withcontrolmode(attachingto-_-)/

```
Server.withControlMode(attachingTo:_:)(attachingTo: String, body: @escaping (ControlSession) async throws -> Result) -> Result
```

Opens a control-mode connection for the duration of `body`, handing over the connection itself.

``connected(attachingTo:_:)`` is the one to reach for: it gives the same
connection *and* a server that speaks over it. This is the layer beneath,
for talking the control protocol directly.

The process is scoped even though Swift can retain the actor handed to
`body`. Returning or storing it does not extend the process lifetime;
later calls fail. When `body` returns, the session is closed and the
child is reaped before this call does.

Connection loss cancels `body`; teardown still waits for code that
ignores cancellation.

- Parameters:
  - session: the session to attach to. Control mode reports `%output`
    only for a session it is attached to, so a connection with no
    target sees command replies and little else.
  - body: the work to run against the connection.
- Throws: ``TmuxError/connectionClosed`` if the connection ends before
  `body`, including when `body` detaches its own client.

## Raises

- `Result`
