# Ownership and cleanup

Source: https://libtmux.org/en/rs/latest/topics/context-managers/

> Scope-based cleanup for tmux objects, and when your program must kill them explicitly.

A tmux session, window, or pane normally remains until you kill it. Scope-based
cleanup can kill it when your code leaves a block, including after an exception.
See [Workspaces](https://libtmux.org/en/rs/latest/concepts/workspaces/) for a temporary layout example.

<a id="rust-no-async-drop-so-cleanup-is-explicit-or-best-effort"></a>

## Explicit asynchronous cleanup

Rust's [`Drop`](<https://doc.rust-lang.org/std/ops/trait.Drop.html>) is synchronous and cannot await an async tmux kill. Use
explicit shutdown when you need to observe cleanup failures:

- **`kill(self)` consumes the handle.** Session, window, and pane kill methods
  take `self` by value, preventing subsequent use of that handle.
- **[`libtmux::test::TestServer`](<https://libtmux.org/en/rs/latest/reference/test-testserver/>) provides a test guard.** Call
  [`TestServer.shutdown`](<https://libtmux.org/en/rs/latest/reference/test-testserver-shutdown/>) to handle cleanup errors. Its [`Drop`](<https://doc.rust-lang.org/std/ops/trait.Drop.html>) implementation
  makes a synchronous cleanup attempt.

```rust
use libtmux::test::TestServer;

let guard = TestServer::new().await?;
let server = guard.server();

let session = server.new_session("work").await?;
session.new_window("editor").await?;

// Await shutdown to handle cleanup errors.
guard.shutdown().await?;
```

<a id="what-this-means-in-practice"></a>

## Testing cleanup

Use explicit cleanup for objects whose handles have no disposal hook. For an
entire disposable test server, prefer your port's test fixture or server guard;
see [Testing with libtmux](https://libtmux.org/en/rs/latest/guides/testing-with-libtmux/).
