Terminated message when another actor stops. Use it to clean up references, fail over, or react to actor lifecycle.
API
| Method | Purpose |
|---|---|
pid.Watch(cid) | Register to receive Terminated when cid stops. |
pid.UnWatch(cid) | Cancel the watch for cid. |
ctx.Watch(cid) | Same as ctx.Self().Watch(cid). |
ctx.UnWatch(cid) | Same as ctx.Self().UnWatch(cid). |
Watch and UnWatch are on *PID and ReceiveContext. Use ctx.Watch(pid) from inside Receive to watch another actor.
Terminated message
When a watched actor stops, the watcher receives:Receive:
When to use
- Dependencies β Watch a child or peer; when it stops, recreate or fail over.
- Resource cleanup β Release references to the stopped actor.
- Supervision β Custom logic when a watched actor dies.
Automatic unwatch
When an actor stops, the system automatically callsUnWatch on all actors it was watching. Watchers do not need to call UnWatch when they receive Terminatedβthe watch is already cancelled. Call UnWatch only when you want to stop watching before the target terminates.