# server.Server.with_session

- **Module:** server.Server
- **Package:** libtmux
- **Language:** Rust
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-rs/blob/8a648c0894dfffc9303583f753b6c8ae01f2fe76/crates/libtmux/src/server.rs#L1223
- **Page:** https://libtmux.org/reference/rs/server-server-with_session/

```
server.Server.with_session(self, options: impl Into<NewSessionOptions>, operation: impl AsyncFnOnce(&Session) -> Result<T, E>) -> Result<T, E>
```

Create a session, run an operation with it, then kill it.

Once this future is polled, the scope owns creation and cleanup.
Cancellation or unwinding can let an in-flight creation finish, but a
session whose creation yields a handle is killed while the Tokio
runtime remains active. Ordinary handle `Drop` remains non-destructive.

Setup and teardown failures convert into the operation's own error
type, so a caller writes one `?` rather than unwrapping twice. When
both the operation and cleanup fail, the cleanup error is returned as
[`Error::AfterEffect`], because tmux had already accepted the scope's
creation; the operation error is discarded. When the operation fails
and cleanup succeeds, its generic error is returned unchanged: the
scope cannot certify replay safety for arbitrary callback work.
A canceled caller cannot receive a cleanup error, so tracing is its
only report.

# Errors

Returns the operation's error, or a converted [`Error`] when the
session could not be created or could not be killed after creation.
