#38 AI agents and MCP 5.2 min
Checkpoint and Resume: Crash Without Sending Twice
My job crashed, I restarted it, and it sent the same email twice.
Run the four cases
Copy the code folder first. Run python compare.py. It uses only the standard library. Compare each line with the video: steps run 10 and 1 email, then 7 and 1, then 7 and 2, then 7 and 1. Then explain in one sentence why the third case sent two emails.
A hint
The crash is raised after the step ran and before its result is saved.
Crash at the last step
In compare.py change the first case so that it crashes in "log" instead of "draft", still with no checkpoint. Predict the steps run and emails sent, then run it. The printed label for that case will still say "crash in draft"; change the label too if you want it to match. I got steps run 12 and emails sent 2.
A hint
Without a checkpoint the restart begins again at the first step, so the send step runs again.
Safe send without a checkpoint
In compare.py add a fifth case to CASES with the label 'safe send only, crash in send', the crash step "send", keep False and safe True. Predict the steps run and emails sent, then run it. I got steps run 11 and emails sent 1.
A hint
Without a checkpoint all earlier steps run again, but the safe send finds its key in the outbox.
Move the crash earlier
Add another case to CASES with a checkpoint, no safe send, and a crash in "fetch". Predict the steps run and emails sent, then run it. I got steps run 7 and emails sent 1. Say why the number of steps is 7 and not 6.
A hint
The step that crashed ran once before the crash and runs again after the restart.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
The crash is a raised error, not a killed process. One crash point per case and six made-up steps. The email is a line in a file. The key check reads a file, so it does not protect two workers running at once. No database, locks or retries.
Up next in AI agents and MCP
More in AI agents and MCP
- #01 Local AI Agent in Python: No API Key (Ollama)
- #12 Multi-Agent Patterns in Plain Python: Pipeline, Router, Loop
- #22 OpenAI Agents SDK vs LangGraph vs Plain Python
- #23 Agent Memory: What to Keep and What to Forget
- #24 Approval Gates for Risky Agent Actions
- #25 Tool Errors: Retries, Fallbacks and Made-Up Answers
Related videos
- #239 Async AI Agents: Design for Timeouts With Job Polling Enterprise architecture and AI agents
- #232 Give Every AI Agent Its Own Identity Enterprise architecture and AI agents
- #236 Agent Orchestration Patterns: Supervisor, Pipeline, Swarm Enterprise architecture and AI agents
- #237 Where Should AI Agent State and Memory Live? A Crash Test Enterprise architecture and AI agents
