<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "Tor",
  "identifier" : "/documentation/Tor/TorClient/stop()",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Tor"
    ],
    "preciseIdentifier" : "s:3Tor0A6ClientC4stopyyYaF"
  },
  "title" : "stop()"
}
-->

# stop()

Stop the Tor instance and release all associated resources.

```
func stop() async
```

## Discussion

Best-effort, non-throwing: if [`state`](/documentation/Tor/TorClient/state) is already [`TorState.idle`](/documentation/Tor/TorState/idle),
[`TorState.stopped`](/documentation/Tor/TorState/stopped), or [`TorState.failed(_:)`](/documentation/Tor/TorState/failed(_:)) the call is a
no-op. Otherwise, the shutdown sequence:

1. Advances ``doc://Tor/documentation/Tor/TorClient/state`` to ``doc://Tor/documentation/Tor/TorState/stopping`` and broadcasts a
   corresponding ``doc://Tor/documentation/Tor/TorEvent/stateChanged(_:)``.
1. Sends `SIGNAL SHUTDOWN` over the control socket
   (control-spec.txt §3.7) via ``doc://Tor/documentation/Tor/TorControlClient/shutdown()``.
1. Polls the Tor thread for up to **10 seconds** at 100 ms
   intervals, then `Thread.cancel()`s if the thread is still
   executing.
1. Releases the control client, closes the FD, and clears
   ``doc://Tor/documentation/Tor/TorClient/socksEndpoint``.
1. If `configuration.ownsDataDirectory` is `true`, removes the
   data directory best-effort (errors are swallowed).
1. Advances ``doc://Tor/documentation/Tor/TorClient/state`` to ``doc://Tor/documentation/Tor/TorState/stopped`` and finishes every
   outstanding ``doc://Tor/documentation/Tor/TorClient/events`` continuation.

> Important: The 10 s grace window is deliberately shorter than
> Tor’s default `ShutdownWaitLength` (30 s) to bound the wait on
> misbehaving Tor builds; `Thread.cancel()` after that window is
> a best-effort unwind, not a guaranteed teardown.

> Note: After resolution the `TorClient` is reusable — a
> subsequent ``doc://Tor/documentation/Tor/TorClient/start()`` re-enters ``doc://Tor/documentation/Tor/TorState/starting`` with a
> fresh data directory when the configuration specifies an
> ephemeral path.