Skip to content

How can my application prevent an expired worker lease from overwriting a newer worker's result?

Give every claimed unit of local work a changing lease token, and require that token on state-changing writes. An expired worker may resume after another worker takes ownership; its old token must no longer authorize updates. This protects your application's workers, not AISwapLab's internal scheduling.

MAKE API WORK RECOVERABLEAISwapLab team · Editorial methodUpdated 2 min read
Open API documentationExplore 67 Image SkillsPrivate keys · Explicit credit approval
Article workflow: Claim atomically; Fence every consequential write; Handle remote uncertainty separately.
Workflow diagram: Claim atomically → Fence every consequential write → Handle remote uncertainty separately. Read the article for evidence and limits.

Expiry does not stop a process

A lease timeout says another process may take over. It does not physically terminate the first process. A long pause or lost network connection can therefore leave two workers believing they should persist observations, publish notifications or update delivery state.

What to do next

  1. 01

    Claim atomically

    Use a conditional database update to claim eligible work and increment a lease generation or issue a new random token. Return the token with the claim so the worker knows which ownership epoch its later writes represent.

  2. 02

    Fence every consequential write

    Require both task identity and current lease token when storing results or changing dispatch state. Check affected-row counts; a zero-row update means ownership was lost and the worker must stop local side effects.

  3. 03

    Handle remote uncertainty separately

    Fencing prevents stale local writes, but cannot recall a remote HTTP request already sent. Keep the original remote request identity and use its established recovery path rather than treating a new lease as permission for new spending.

A concrete example

Illustrative example: worker A holds token 4, pauses, and worker B takes token 5. A later tries to mark delivery complete with token 4. The database rejects that write, leaving B's current ownership and evidence intact.

Before you call it ready

  • Old tokens cannot update current work.
  • Every claim changes the token.
  • Lease loss stops downstream local actions.

A locally executed, reproducible check

A stale token fails an actual conditional database write.

We executed this exact synthetic fixture on November 16, 2026 with Python 3.12.10 and SQLite 3.49.1. It ran locally with no network requests, account data or model generation. The recorded output below is an observation from that environment, not a platform benchmark. Copy the code into a separate Python file to repeat it in a suitable local environment; compare the mechanism and check your runtime version.

python
import sqlite3
c=sqlite3.connect(':memory:')
c.execute('CREATE TABLE task(id TEXT PRIMARY KEY, token INTEGER, state TEXT)')
c.execute("INSERT INTO task VALUES('t1',4,'running')")
c.execute("UPDATE task SET token=5 WHERE id='t1' AND token=4")
stale=c.execute("UPDATE task SET state='complete' WHERE id='t1' AND token=4").rowcount
current=c.execute("UPDATE task SET state='complete' WHERE id='t1' AND token=5").rowcount
print('stale writer rows:', stale)
print('current writer rows:', current)
print('stored:', c.execute('SELECT token,state FROM task').fetchone())

Recorded output and interpretation

This checks the token on every illustrated write. It does not test lease acquisition, wall-clock expiry, object-store writes, or the AISwapLab service. An unfenced external side effect remains outside the experiment.

A matching local output establishes only the illustrated library or database behavior under these inputs. It does not certify a production integration. Preserve the approved action, authorization boundaries and current AISwapLab request contract when applying the idea.

text
stale writer rows: 0
current writer rows: 1
stored: (5, 'complete')

When to stop or choose another method

Stop relying on timestamp expiry alone when a stale process can still write; add a checked ownership token before enabling takeover.

Sources

One task. A recoverable workflow.

Check the contract, approve the quote and retain the job.

Open API documentation