Skip to main content

States

A PID tracks its state through a set of flags. The main states:
StateMeaning
RunningActor is active and processing messages
StoppingShutdown or passivation in progress
SuspendedSuspended by the supervisor after a failure
PassivatingIdle timeout; about to be stopped
Use pid.IsRunning(), pid.IsSuspended(), pid.IsStopping() to query state when needed.

Lifecycle hooks

  • PreStart β€” Called once when the actor is spawned. Use for initialization.
  • Receive β€” Called for each message. This is where your logic lives.
  • PostStop β€” Called once when the actor is about to stop. Use for cleanup.

Spawn and stop

  • Spawn β€” Creates the actor, registers it, calls PreStart, and starts the mailbox dispatch loop.
  • Stop β€” Sends a stop signal. The actor finishes current work, runs PostStop, and is removed from the tree.
When a parent stops, all children stop first (depth-first). The order among descendants is not guaranteed.

Lifecycle hooks (Actor interface)

The Actor interface defines three lifecycle hooks. See Actor Model for the full interface.
HookWhenUse for
PreStartBefore first messageInitialize DB, caches, recover state. Return error to abort startup.
ReceiveFor each messageProcess messages. State is private; no locks needed.
PostStopBefore terminationRelease resources, flush buffers, notify other systems.
If PreStart returns an error, the actor is not started and the supervisor handles the failure.

System messages

The framework injects system messages into the actor’s mailbox to drive lifecycle and coordination. These are plain Go structs in the actor package. Handle them in Receive when you need to react.

Lifecycle control

MessageRole
PostStartDelivered as the first message after the actor is spawned. Use it to capture ctx.Self(), set up references, or perform logic that must run after the actor is fully started.
PoisonPillGracefully stops the actor. It is enqueued like any other messageβ€”the actor processes all messages already in the mailbox first, then shuts down. Use for clean, ordered shutdown.

Death watch

MessageRole
TerminatedSent to every actor that is watching another actor when that watched actor stops. Use it to clean up references, restart dependents, or trigger failover. Subscribe via ctx.Watch(targetPID); cancel via ctx.UnWatch(targetPID). See Death Watch.

Event stream (observability)

These events are published to the event stream for subscribers. You typically do not handle them in Receive unless you have a specific need:
MessageWhen
ActorStartedActor has started
ActorStoppedActor has stopped
ActorChildCreatedChild actor was spawned
ActorPassivatedActor was passivated (idle timeout)
ActorRestartedActor was restarted by supervisor
ActorSuspendedActor was suspended after a failure
ActorReinstatedActor was reinstated by supervisor
NodeJoined / NodeLeftCluster membership change

Handling system messages

Add a case for the system messages you care about. For messages you don’t handle, call ctx.Unhandled() so the framework can apply default behavior (e.g., logging, event publishing).