Skip to content

Fix a leaked transaction and a hang when transactions begin on several threads - #85

Open
AntonOresten wants to merge 1 commit into
JuliaDatabases:mainfrom
AntonOresten:concurrent-transactions
Open

AntonOresten wants to merge 1 commit into
JuliaDatabases:mainfrom
AntonOresten:concurrent-transactions

Conversation

@AntonOresten

Copy link
Copy Markdown

Transaction(env; …) registers each new transaction in env.txns with an unsynchronized push!, after mdb_txn_begin and before the finalizer is attached. When two threads begin transactions in one environment at the same time, that push! can throw ConcurrencyViolationError("Vector can not be resized concurrently"). The transaction has already begun in LMDB by then, it has no finalizer yet, and the do-block method's abort is never reached because the throw is in the constructor it calls. Nothing ever ends that transaction:

  • a read-only transaction loses its reader slot;
  • a write transaction keeps LMDB's writer mutex for the life of the process. The next write transaction blocks in mdb_txn_begin forever, and since that is a plain ccall, the first garbage collection another thread asks for waits on it as well: the process sits at 0% CPU.

Reproducer

# julia --threads=2 mwe.jl
using LMDB
env = LMDB.Environment(mktempdir(); mapsize = 1 << 26, maxreaders = 510, flags = LMDB.MDB_NOSYNC | LMDB.MDB_NOTLS)
function begin_each(kind, flags)
    for _ in 1:400_000
        try
            LMDB.Transaction(_ -> nothing, env; flags)
        catch err
            println(stderr, kind, ": ", sprint(showerror, err))
        end
    end
end
readers = [Threads.@spawn begin_each("read-only", LMDB.MDB_RDONLY) for _ in 2:Threads.nthreads()]
writer = Threads.@spawn begin_each("write", 0)
foreach(wait, readers)
wait(writer)    # never returns once a write transaction's `push!` has thrown
println("done")

With 3.0.1 on Julia 1.13.3 (Linux, aarch64) this prints

write: ConcurrencyViolationError("Vector can not be resized concurrently")

within a second and then hangs, on 2, 4 and 12 threads. With this change it prints done.

Fix

  • Environment gets a lock for txns, held for the push! in the Transaction constructor and while close(env) takes the list.
  • The finalizer is attached before the transaction is registered, so a transaction whose registration fails for any reason is still ended.

The lock is a SpinLock because close is also the environment's finalizer, and a finalizer must not wait on a lock that switches tasks.

Test

test/threads.jl begins read-only and write transactions from four threads in a child process, so that it has threads however the suite is started (CI runs it on one). Without the fix it failed in 10 of 10 runs on Julia 1.13; with it, it takes about half a second. The suite passes locally on Julia 1.10 and 1.13.

Where this was met

Through CompilerCaching's LMDB object cache: several tasks loading cached GPU kernels at once, each in a read-only transaction, while one refreshes access times in a write transaction. The process froze at 0% CPU.

Not addressed here

env.txns gains a WeakRef per transaction and is emptied only by close(env), so a long-lived environment's list grows with every transaction it has begun. This change leaves that as it is.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GqhLuqgqY1TAufNyxboLQj

`Transaction` pushed onto `env.txns` unsynchronized, so two threads
beginning transactions in one environment could make the `push!` throw
`ConcurrencyViolationError`. The transaction had already begun and its
finalizer was not yet attached, so nothing ended it: a read-only
transaction lost its reader slot, and a write transaction kept LMDB's
writer mutex, blocking every later write transaction in `mdb_txn_begin`.

Guard the list with a lock, and attach the finalizer before registering
the transaction.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqhLuqgqY1TAufNyxboLQj

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant