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
DatabaseLockclass opens a lock file in the system temp directory and tries a non-blocking exclusiveflock. - 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
TestCasecallsDatabaseLock::acquire()insetUpBeforeClass(), 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.
flockonly 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