State diagrams: timeouts

State diagrams: timeouts

puml.online

Introduction

The easiest thing to leave out of a state machine diagram is the timeout. No matter how polished the flow looks, if a waiting state has no fallback exit, production gets requests that hang forever. But drawing an arrow labelled timeout is only the surface of the job — what actually makes the diagram correct is the timer lifecycle: who starts it, and who cancels it when the response arrives in time.

Neither engine treats “timeout” as a first-class concept. timeout 30s is just an ordinary event label; the engine will not start a timer for you. The real difference is whether you can write the timer management into the state itself. PlantUML lets you attach entry / do / exit actions to a composite state, so the timer starts and stops with the state. Mermaid’s stateDiagram-v2 has no action syntax at all, so the timer has to be modelled as an explicit state.

This article builds the same two-tier “connect, then read” timeout scenario in both engines and flags the gotchas you will actually hit.

PlantUML version

PlantUML’s composite states support UML entry / do / exit actions, which is exactly where a timer belongs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@startuml
hide empty description

state "Connecting" as C {
C : entry / startTimer(30s)
C : do / awaitResponse()
C : exit / cancelTimer()
}
state "Connected" as K
state "Failed" as F
state decision <<choice>>

[*] --> C
C --> decision : handshake
decision --> K : [ok]
decision --> F : [connect timeout 30s]

K --> K : read data
K --> F : [read timeout 10s]

F --> [*]

note right of F
exit / cancelTimer() cancels the timer on leaving C,
so a late response cannot revive the request.
end note
@enduml

The key points:

  • state "Connecting" as C { ... } declares a composite state. English state names render fine.
  • Inside the composite state, C : entry / startTimer(30s) is the entry action, run once when the state is entered; C : do / awaitResponse() is the do activity, run continuously while the state is active; C : exit / cancelTimer() is the exit action.
  • The exit action hangs off the state, not off any single transition, so cancelTimer() runs on every way out of C — handshake success or timeout alike. This is where timeout models most often go wrong: if you put the cancel on the success path (C --> decision : handshake), nothing cancels the timer on the timeout path, and a late response can still “revive” a request that already failed.
  • state decision <<choice>> is the choice pseudostate and renders as a UML diamond; the square brackets in decision --> F : [connect timeout 30s] are the guard.
  • The two timeout tiers use two distinct event labels: [connect timeout 30s] is the handshake-stage timeout, [read timeout 10s] is the read timeout after the connection is up. Saying which tier it is is far more useful than a bare timeout.
  • K --> K : read data is a self-transition, meaning the connected state keeps reading data.

Mermaid version

Mermaid’s stateDiagram-v2 has no action syntax, so the timer becomes an explicit state:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
stateDiagram-v2
state "Connecting" as C {
[*] --> TimerArmed
TimerArmed --> Waiting : arm 30s timer
}
state "Connected" as K
state "Failed" as F
state "Judgment" as decision

[*] --> C
C --> decision : handshake
decision --> K : [ok]
decision --> F : [connect timeout 30s]

K --> K : read data
K --> F : [read timeout 10s]

F --> [*]

note right of F
Mermaid has no entry/exit actions,
so the timer is modelled as an explicit state.
end note

Points to note:

  • state "Connecting" as C { ... } is a composite state, written exactly like PlantUML’s.
  • Because there is no entry / exit syntax, arming the timer has to be an explicit state: [*] --> TimerArmed lands on “timer armed” the moment the composite state is entered, and TimerArmed --> Waiting : arm 30s timer labels what that step does.
  • The cost is that there is nowhere to put the timer cancellation. PlantUML’s exit / cancelTimer() has no direct equivalent here; either leave it out and explain it in a note, or add another state to express it. A note saying “the timer is cancelled on leaving C” is the common practice.
  • state "Judgment" as decision gives the choice node a readable label. Mermaid accepts the <<choice>> stereotype but still draws a plain rectangle and does not show the stereotype text, so an aliased normal state is more practical.
  • Guards, timeout labels and self-transitions are identical to PlantUML: decision --> F : [connect timeout 30s] and K --> K : read data are universal syntax.

Side-by-side

Aspect PlantUML Mermaid
Timeout event syntax C --> F : timeout 30s, an ordinary event label Identical
Timer lifecycle entry / do / exit actions on a composite state No action syntax — the timer must be an explicit state
Cancelling the timer exit / cancelTimer() on the state, covering every exit No equivalent mechanism, usually a note
Two tiers and guards [connect timeout 30s], [read timeout 10s] Identical
Choice pseudostate state decision <<choice>>, renders as a diamond Accepts <<choice>> but renders as a plain rectangle, and the stereotype text is not shown
State colouring skinparam state { ... } works; classDef in a state diagram returns HTTP 400 classDef + class works as expected

Two gotchas that will bite you:

  1. classDef does not work in a PlantUML state diagram. Add it and PlantUML parses the file as a sequence diagram and reports a syntax error (Syntax Error? (Assumed diagram type: sequence)). To recolour states you must use skinparam state { ... }.
  2. Mermaid nested composite states have a scoping trap. A plain one-level nesting is fine, but a self-transition on a child state written in the parent’s scope (Inner --> Inner : timeout 10s) throws Cannot read properties of undefined (reading 'clusterNode'). Writing that self-transition inside the child state’s own block (state Inner { Inner --> Inner : ... }) renders fine.

Wrap-up

The heart of timeout modelling is not the arrow labelled timeout — it is who owns the timer. PlantUML puts the start and stop logic inside the state definition via entry / exit actions, so one declaration covers every exit path, and that is the easier engine for drawing timeout flows. Mermaid’s syntax is cleaner, but the price is that the timer lifecycle has to be spelled out with extra states and notes.

Whichever engine you pick, three rules are worth baking into your template: arm the timer on state entry, not in the caller, or every state jump leaks it; cancel the timer on state exit, not on the success path, or the failure path leaks it; and label the tier — connect timeout 30s and read timeout 10s are diagnosed in completely different ways, and that is worth far more than a bare timeout.

  • Title: State diagrams: timeouts
  • Author: puml.online
  • Created at : 2026-12-05 01:00:00
  • Updated at : 2026-12-05 02:08:58
  • Link: https://puml.online/blog/state-diagram-timeouts-en/
  • License: This work is licensed under CC BY-NC-SA 4.0.