# Close a KubeMQ Rust Client (/sdks/rust/how-to/connection/close)



## Overview [#overview]

Closing a client isn't an afterthought — it tells the broker and your own process that this connection is done, so both sides release what they were holding for it. A KubeMQ client is more than a socket: it's a gRPC channel plus whatever child tasks (subscriptions) it has spawned to service it. Skip the close and those linger — subscriptions keep running, the channel stays open — and in short-lived binaries or test suites you leak connections until the process exits.

Awaiting `client.close()` cancels all child tasks, waits out the drain timeout, then closes the gRPC channel. Once it returns, the client is in a terminal closed state — every call after that fails fast with `KubemqError::ClientClosed`.

**Gotchas:** the drain window is bounded, not unlimited, so a slow consumer can still lose the tail of a burst if you close mid-stream; `close()` is idempotent, so calling it twice is safe, but a closed client is still dead forever — no reconnect on the same instance, build a new one; and forgetting to await `close()`, or letting the client simply drop, skips the graceful drain, leaving in-flight subscriptions to end abruptly.

## Prerequisites [#prerequisites]

* KubeMQ server running on `localhost:50000`
* Rust SDK installed (`cargo add kubemq`)

## Code [#code]

```rust title="main.rs"
use kubemq::prelude::*;

#[tokio::main]
async fn main() -> kubemq::Result<()> {
    let client = KubemqClient::builder()
        .host("localhost")
        .port(50000)
        .build()
        .await?;

    let info = client.ping().await?;
    println!("Connected. Server: {}", info.version);

    client.close().await?;
    println!("Client closed");

    Ok(())
}
```

## How It Works [#how-it-works]

* `close()` cancels all child tasks (subscriptions), waits for the drain timeout, then closes the gRPC channel.
* The method is idempotent — safe to call multiple times.
* After `close()`, all subsequent operations return `KubemqError::ClientClosed`.
* Run the program while the server from the prerequisites is available.

## Related [#related]

* [Getting Started](/deploy)
* [Rust SDK Reference](/sdks/rust/reference)
* [Connect](/sdks/rust/tutorials/connect)
* [Graceful Shutdown](/sdks/rust/how-to/error-handling/graceful-shutdown)
