A long task stopped: checkpoints and safe resumption

A Python and MySQL 8.4 model: check checkpoints against outputs and dependencies, distinguish unknown from stale, and avoid repeating a verified step.

Marking a completed row helps locate the point to resume.

The process stopped after reporting that it was working on the index. The result is already on disk, but the journal still marks that step as unfinished. Starting everything again is easy. The harder questions are whether a retry would create another result and whether the next step may already proceed.

A checkpoint helps only when it can be reconciled with the current inputs, results, and dependencies. The number of the last step is not enough: different branches of a task may be ready to different degrees, and a success record may outlive the file it once confirmed.

Consider an independent teaching model written in Python with MySQL 8.4/InnoDB through PyMySQL. It stores the state of four steps, interrupts work at two defined points, and reads the same state in a new process through a separate TCP connection. Output bytes come from authored teaching fixtures. The program writes these synthetic artifacts; it does not perform refactoring, deployment, or arbitrary engineering commands.

Resume a dependency graph

The teaching task assembles an imaginary reading pack from two notes. Every result is short and known in advance, allowing the resume protocol to be checked without a complex document generator.

Step What must be confirmed beforehand Teaching result
prepare Nothing Prepared notes
index prepare An index of the notes
preview prepare A short preview
package index and preview The assembled pack

This is a small DAG: a directed graph without cycles. index and preview depend on the same preparation, but not on each other. package needs both. A line saying that preview was the last step does not establish whether the index was accepted; admitting the final step requires the state of each dependency.

The validator separately rejects a cycle, an unknown predecessor, and a duplicate step identifier. A structurally valid graph, however, does not prove that its domain dependencies are correct. The task author decides that the pack actually needs an index. Automatic validation only enforces the rule already supplied.

Intent, result, and acknowledgement are three records

For each step, the model stores pending, running, or completed. Alongside it are the attempt number, operation identifier, input fingerprint, and fingerprint of the accepted result. The artifact itself is stored as a LONGBLOB in a separate table within the same MySQL schema. The tables use InnoDB, foreign keys link artifacts and counters to steps, and textual identities use the utf8mb4_0900_bin collation.

The sequence deliberately uses three transactions. First, begin records the intent and moves the step to running. Next, emit writes the declared artifact bytes and increments the synthetic effect counter. Finally, acknowledge reads the evidence again and moves the step to completed.

Separate transactions create the uncertainty window needed for the experiment. If the artifact and checkpoint were always written together in one transaction, this particular gap between them would not exist here. For a real external API, one local transaction would likewise not combine the remote action with its local journal.

The code explicitly calls the native connection’s begin(), commit(), and rollback() methods, described in the PyMySQL documentation. The protocol rejects an existing caller-owned transaction without committing or rolling it back. An error inside its own transaction causes a rollback and propagates without automatic retry; this includes duplicate key 1062, lock timeout 1205, and deadlock 1213. The MySQL documentation distinguishes their effects on InnoDB; the model’s wrapper explicitly rolls back the entire transaction it owns.

First interruption: the intent exists, but the result does not

The first process completes prepare, begins index, and exits in a controlled way with code 42 before writing the index. The database retains index=running, with its effect count at zero. This is an ordinary controlled process exit after a committed transaction, not SIGKILL or a power cut.

The second process connects to the same MySQL schema through a separate native TCP connection and first acquires a new modelled right to resume. It does not execute index merely because its state is running. A separate reconcile operation inspects the result store.

In this narrow example, that store accounts for every synthetic task effect: the model records them only in MySQL and calls no external services. Its TCP connections to the database are still real network connections. A successful read confirming that the artifact is absent can therefore return the step to pending. The next attempt creates the index once. The attempt number becomes two, while the count of recorded effects remains one.

That conclusion depends on complete observation. An empty local cache does not prove that a payment failed to go through or a message was not sent. If the check does not cover the place where the actual effect occurs, a missing record is insufficient grounds for repeating the action.

Second interruption: the result exists, but the checkpoint lags

In another run, the first process writes the index but exits in a controlled way with code 42 before acknowledge. The journal again shows running; this time, the effect count is already one.

After reopening the database, the new process checks several conditions together: the artifact belongs to the same operation, has the required input fingerprint, its actual bytes match the declared result, and the stored digest matches the bytes read. Checking only the claimed_hash field would mean trusting a declaration.

When all conditions hold, reconcile accepts the existing result and records completion. It does not call emit for the index again. This run retains the first attempt and one effect.

Interruption point Artifact during recovery Action after reconciliation
After intent, before output Absence is authoritatively confirmed in the complete local store Permit a new attempt
After output, before checkpoint Exact expected bytes and ownership by the operation Accept the existing result
Observation is unavailable Evidence is insufficient Keep the outcome unknown
Bytes or ownership contradict expectations A conflict exists Stop resuming with a stale outcome

Here, unknown and stale are reconciliation decisions. They are not disguised as pending. An unconfirmed running step cannot be started again, and its dependent steps remain blocked.

Recheck an old checkpoint against the present state

Before a new action, the model checks the fingerprint of the entire task description. Changing an input or an expected artifact creates a different task for this protocol. The old journal cannot be silently attached to it.

Likewise, completed does not remove the need to verify the result. If the artifact has been deleted or changed, a record of its earlier completion does not admit a dependent step. Before package, the model checks not only its immediate dependencies, index and preview, but also their shared foundation, prepare. Otherwise, an earlier corrupted result could hide behind later green statuses.

A fingerprint has a precise role: binding a check to specific bytes. It does not prove authorship, good architecture, or the correctness of an arbitrary algorithm. In this exercise, the expected texts are supplied in advance; a real task may have a different acceptance criterion, such as a behavioural test, a verified provider response, or an explicit operator decision.

The right to resume can also expire

The task table contains an owner, a generation number, and a lease expiry point. The experiment uses logical time: a lease remains valid while now < expires. At equality, the previous owner is already no longer allowed to write.

The new process receives a higher generation. Every begin, emit, acknowledge, and reconcile first locks the singleton task row with SELECT … FOR UPDATE and checks the owner, generation, and expiry again in the same transaction that changes state. This current locking read serializes protocol mutations under REPEATABLE READ too: the model reads no snapshot before acquiring the lock. The old process cannot append a checkpoint by referring to permission it once received.

This is an ownership model with controlled time. It does not measure clock skew, network partitions, or competition among distributed workers. Permission in this journal would not protect an external resource that does not check it either. Such a resource needs its own write-admission protocol.

Reproduce both runs

You need Python 3.12+, PyMySQL[rsa]==1.2.4, and the example’s shared guarded MySQL fixture configured. Run the following commands from the repository root; the dependency is installed into the active Python environment. The wrapper runs the experiment on disposable MySQL 8.4, while the shared tools/mysql_fixture.py manages resources through MysqlLab. Before business transactions, Ledger.initialize() explicitly creates the schema through a separate migrator connection. The runtime account has DML access only, without CREATE, ALTER, or DROP. Initialization is one-time: it is neither an atomic schema upgrade nor an automatic data transfer from an old SQLite file.

python3 -m venv .venv-mysql
.venv-mysql/bin/pip install -r tools/requirements-mysql.txt
PYTHONPATH=tools bash fixtures/mysql_labs/run.sh .venv-mysql/bin/python -B tools/checkpoint-resume-lab/run_lab.py /absolute/new-report.json

Use a new report path to preserve the previous evidence.

The run creates temporary MySQL schemas owned by the fixture. For each interruption point, it starts the first process, a separate resuming process, and one more process that rechecks the completed task. All three use separate native connections sharing a schema that remains available until the child processes finish. The report records exit codes, process identifiers, state transitions, and effect counters.

All 39 model checks passed on MySQL 8.4. For both interruption points, the three process exit codes were 42/0/0, and the final state contained one synthetic effect for each of the four steps. A subsequent replay did not increase those counters. Checks also covered a stale owner, changed input, a conflicting digest, unavailable observation, unverified dependencies, identifier and integer bounds, denied runtime DDL, and rollback of the owned transaction after native errors 1062 and 1205. When two separate connections simultaneously attempted lease takeover at the expiry boundary, only one received the new generation.

These results establish the bounded model’s behaviour after a controlled client exit and reads of committed state through new connections. They do not test SIGKILL, server restart, power loss, filesystem durability, or distributed leases. Transaction boundaries also matter during setup: the MySQL documentation on implicit commits explains why DDL runs separately from business transactions. A real engineering executor needs its own tests at those boundaries.

A useful checkpoint preserves enough evidence to answer three questions again after a pause: is this the same task, is the required result confirmed, and does this executor have permission to take the next action? Those answers determine how work resumes.

The shared MySQL fixture and its files are introduced in the transaction chapter. Run commands from the single article_demo root; do not create a separate Symfony application for each article.

  1. Platform Engineering Who should retry a failed operation: one retry owner
  2. Platform Engineering Timeout after an external request: when the outcome is unknown
  3. Platform Engineering Two workers took one job: lease, expiry, and fencing
  4. Platform Engineering From an engineering task to a bounded execution plan