Skip to content

Is RwTxn Send? #339

@elbaro

Description

@elbaro

I found that multi-threaded tokio runtime moves the task between threads, and this leads to let tx = env.write_txn() and tx.commit() to run in different threads. And the next call to env.write_txn() hangs.

let tx = env.write_txn()?; // thread A

while let Some(row) = stream.next().await {
    insert_row(&mut tx, row)?;
}

tx.commit()?;  // thread B

let tx = env.write_txn()?;  // hangs
[indexing] IndexBuilder tx is created in thread=ThreadId(9)
████████████████████████████████████████ 4317834/4317834
[indexing] IndexBuilder tx is committed in thread=ThreadId(17)
[task] lmdb - getting WriteTx in thread=ThreadId(17)  # hangs

Switching to a single-threaded tokio runtime did not have a deadlock.

I found this in jnwatson/py-lmdb#89 (comment) :

A write Transaction can only be used from the thread it was created on.

Is it okay to commit a write transaction in a different thread?
If not, what is a recommended approach to interleave async workload with transactions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions