Skip to content

open_or_create_database: a process that is overtaken on a new path no longer fails with "Cannot create directory" - #493

Merged
tae898 merged 2 commits into
mainfrom
fix/492-open-or-create-race
Nov 25, 2026
Merged

tae898 merged 2 commits into
mainfrom
fix/492-open-or-create-race

Conversation

@tae898

@tae898 tae898 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes open_or_create_database and DatabaseFactory.open_or_create when two processes start on the same new path, in arcadedb and arcadedb-jvm (both use src/arcadedb/_open_or_create.py).

Cause

Two things together made one process fail in the run on main:

  1. The engine's LocalDatabase.create() makes the directory with if (!databaseDirectory.exists() && !databaseDirectory.mkdirs()) and throws "Cannot create directory" inside it. File.mkdirs() returns false for a directory that already exists, so a process that another one overtakes between the two calls gets that error about a directory that is there.
  2. After a failed create the helper waited 0.25 s for exists() and then raised the create error again. exists() looks for the schema file, which the other process writes near the end of its create. In that run this took longer than the wait.

This is not correct behaviour: the documented contract allows only the "locked by another process" error here.

The fix

The helper calls create() a second time. After the first failure the directory exists, so the engine skips mkdirs() and its database lock decides: this process creates the database, or gets "locked by another process", or gets "already exists" and opens it. A longer wait would only have moved the limit. A failure with another cause (a path that is a file, no permission) is still raised, after one more attempt. The outcomes that the two-process test accepts are unchanged.

The docstrings and the "Databases and queries" page now also say that the lock error can come while the other process is still creating the database.

The check followed by mkdirs() stays in the engine. This fix does not depend on a change there.

Related issues

Closes #492

Tests

Four new tests in tests/test_open_or_create.py script the sequence with a factory object, so they do not depend on the timing of two processes. The first commit holds only the tests.

On the test commit (097b289), the Python 3.14 leg of the JVM tests ended with 4 failed and 4258 passed: the failures are the four new tests, on the old helper. The checks of the head commit run them with the fix.

The failure showed on the Windows leg, which only the full tier runs. I did not start a full run: the race is intermittent (the two later runs on main at e031d35 passed without a fix), so one green Windows run would show little, while the new tests run the failing sequence on every platform. I ran only static checks on my machine (compile, pyflakes, pre-commit, Bandit).

Release note draft

open_or_create_database: when two processes start on the same new path, the second one no longer fails with "Cannot create directory". It opens the database or raises the "locked by another process" error, in arcadedb and arcadedb-jvm.

Checklist

  • A regression test fails on the old code and passes on the new
  • The test suite that the change can affect passes
  • uvx pre-commit run --files <changed files> passes

…ried again

The failure on main: open_or_create_database on a new path raised the engine's
"Cannot create directory" while another process created the database. These
tests script that sequence with a factory object, so they do not depend on the
timing of two processes. They fail on the current helper, which calls create()
once and raises its error when no database appears within the wait.
@tae898 tae898 added this to the 26.12.1 milestone Nov 25, 2026
@tae898 tae898 added bug Something isn't working python Pull requests that update python code concurrency Threads, transactions and conflicts native The arcadedb package (native library build) jvm The arcadedb-jvm package (JPype build) labels Nov 25, 2026
@tae898 tae898 self-assigned this Nov 25, 2026
Cause: LocalDatabase.create() in the engine makes the database directory with
exists() followed by mkdirs(). A process that another one overtakes between the
two gets "Cannot create directory" for a directory that now exists. The helper
then waited 0.25 s for schema.json, which the other process writes only at the
end of its create, and re-raised the directory error when that took longer.

The second create does not depend on timing: the directory is there, so the
engine's database lock decides (create, "locked by another process", or
"already exists"). Other failures fail the same way twice.

Both packages use the helper. Docstrings and the docs page now say that the
lock error can also come while the other process is still creating.
@tae898
tae898 merged commit 5dc9d2f into main Nov 25, 2026
24 checks passed
@tae898
tae898 deleted the fix/492-open-or-create-race branch November 25, 2026 04:22
@tae898 tae898 mentioned this pull request Nov 30, 2026
1 of 10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working concurrency Threads, transactions and conflicts jvm The arcadedb-jvm package (JPype build) native The arcadedb package (native library build) python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Full CI is red on main: Test JVM (arcadedb-jvm)

1 participant