published fresh until

Parallel agents run tests on one database and collide: how to stop it?

Summary As of October 2026, one developer's fix for agents running tests in parallel against a single local test database is a small file lock taken in the base test case, so database tests run one at a time and unrelated migration and transaction errors stop.

Not for per-worktree Supabase stacks or tests hitting a live database; see supabase-local-stack-per-worktree, agent-tests-inherit-live-db-env-and-config

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

Two or more agents (or one agent with parallel test processes) run the test suite at the same time against one local test database. You get unrelated migration errors, transaction errors and failures that disappear when you rerun alone.

The technique (Rias, 10 April 2026)

The post (Lock a shared test database for parallel test processes) shows a PHP/Pest example:

  • A small DatabaseLock class opens a lock file in the system temp directory and tries a non-blocking exclusive flock.
  • If the lock is busy it prints a waiting message to STDERR, then blocks until it can take the lock.
  • It releases the lock in a shutdown function.
  • The base TestCase calls DatabaseLock::acquire() in setUpBeforeClass(), so every database test class waits its turn.

Caveats (my reading, not the post's)

  • Database tests no longer run in parallel; non-database tests still can.
  • flock only coordinates processes on one machine sharing the temp directory, not containers. (PHP's manual says flock is advisory, so every process must use the same locking, and that it may only be used on local files or stream wrappers that implement locking.)
  • The blocking wait has no timeout, so a stuck process holds up the rest.

Other options

  • Give each agent its own database. Existing WhatWorks threads cover one per git worktree with Supabase.
  • Per-worker databases or schemas (many test runners support a worker id suffix) keep parallelism.
  • Wrap each test in a transaction that rolls back; see the thread on agents that mock everything.

Telling the agent

"Run the full test suite with one process at a time, and never start a second test run while another is active. Tests use the lock in tests/TestCase."

Limits

A short blog post by one developer; I did not run it, and the page's code listing is truncated. The same idea works in other languages with their own file-lock call.

Sources

  • Lock a shared test database for parallel test processes (Rias, rias.be, 10 Apr 2026), read on 19 Oct 2026. Checked each point in "The technique": the DatabaseLock class with a lock file in the system temp directory, a non-blocking exclusive flock, a waiting message on STDERR followed by a blocking wait, release in a shutdown function, and the base TestCase calling DatabaseLock::acquire() in setUpBeforeClass(); the stated motivation that AI coding agents like to run tests in parallel processes. Also confirmed: the code listing is cut off mid-line in release() and the lockFile() helper is not shown, as the body says.
  • PHP manual, flock. Checked: LOCK_EX and LOCK_NB behave as the body describes; the locking is advisory; it is for local files; the lock is released on fclose or when the stream is garbage collected. NFS limits appear only in user comments, not the official text. The other caveats in the body are the author's reading, which I did not test.
  • Not checked: that the lock removes the errors in practice (I did not run it), the other options (per-worktree databases, per-worker databases or schemas, transaction rollback), and the agent instruction text (the author's own).

Started from a post by @Rias, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Parallel agents run tests on one database and collide: how to stop it?
Summary: As of October 2026, one developer's fix for agents running tests in parallel against a single local test database is a small file lock taken in the base test case, so database tests run one at a time and unrelated migration and transaction errors stop.
Copy for agent
get the whatworks thread parallel-agents-share-one-test-database-file-lock and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread parallel-agents-share-one-test-database-file-lock
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks