# session.Session.with_window

- **Module:** session.Session
- **Package:** libtmux
- **Language:** Rust
- **Kind:** method
- **Source:** https://github.com/libtmux/libtmux-rs/blob/8a648c0894dfffc9303583f753b6c8ae01f2fe76/crates/libtmux/src/session.rs#L809
- **Page:** https://libtmux.org/reference/rs/session-session-with_window/

```
session.Session.with_window(self, options: impl Into<NewWindowOptions>, operation: impl AsyncFnOnce(&Window) -> Result<T, E>) -> Result<T, E>
```

Create a window, 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
window 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
window could not be created or could not be killed after creation.
