State diagrams: timeouts
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 | @startuml |
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 ofC— 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 indecision --> 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 baretimeout. K --> K : read datais 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 | stateDiagram-v2 |
Points to note:
state "Connecting" as C { ... }is a composite state, written exactly like PlantUML’s.- Because there is no
entry/exitsyntax, arming the timer has to be an explicit state:[*] --> TimerArmedlands on “timer armed” the moment the composite state is entered, andTimerArmed --> Waiting : arm 30s timerlabels 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. Anotesaying “the timer is cancelled on leaving C” is the common practice. state "Judgment" as decisiongives 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]andK --> K : read dataare 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:
classDefdoes 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 useskinparam state { ... }.- 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) throwsCannot 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.