MDB environment opened multiple times #1

Closed
opened 2025-12-29 20:55:50 +00:00 by scossu · 3 comments
Owner

While running the Pocket Archive test suite, the MDB environment is opened multiple times, possibly by the same process.

This is discouraged in the LMDB documentation:

LMDB uses POSIX locks on files, and these locks have issues if one process opens a file multiple times. Because of this, do not mdb_env_open() a file multiple times from a single process. Instead, share the LMDB environment that has opened the file across all threads. Otherwise, if a single process opens the same environment multiple times, closing it once will remove all the locks held on it, and the other instances will be vulnerable to corruption from other processes.

The PKAR test suite is currently having double-free crashes and deadlocks on garbage collection, where the same environment is being closed multiple times.

l_store_new() should maintain a global map of MDB store IDs and handles so as to still allow opening multiple environments on different files, but returning the same handle of an open environment if this is opened more than once.

While running the Pocket Archive test suite, the MDB environment is opened multiple times, possibly by the same process. This is discouraged in the [LMDB documentation](http://www.lmdb.tech/doc/starting.html): > LMDB uses POSIX locks on files, and these locks have issues if one process opens a file multiple times. Because of this, do not mdb_env_open() a file multiple times from a single process. Instead, share the LMDB environment that has opened the file across all threads. Otherwise, if a single process opens the same environment multiple times, closing it once will remove all the locks held on it, and the other instances will be vulnerable to corruption from other processes. The PKAR test suite is currently having double-free crashes and deadlocks on garbage collection, where the same environment is being closed multiple times. `l_store_new()` should maintain a global map of MDB store IDs and handles so as to still allow opening multiple environments on different files, but returning the same handle of an open environment if this is opened more than once.
Author
Owner

Fixed by be80a23f71.

Fixed by be80a23f71.
Author
Owner

Not yet resolved. This change stores the memorized store handles in the registry, which is tied to the Lua state. When run on multiple files, busted seems to open multiple Lua states, which open overlapping MDB environments that are all garbage-collected at the end, still causing segfault.

The memorized handles should be stored in the global environment.

Not yet resolved. This change stores the memorized store handles in the registry, which is tied to the Lua state. When run on multiple files, busted seems to open multiple Lua states, which open overlapping MDB environments that are all garbage-collected at the end, still causing segfault. The memorized handles should be stored in the global environment.
scossu reopened this issue 2025-12-31 16:34:17 +00:00
Author
Owner

57f4a9b2c8 effectively stores the handles in the globals table and now I can see busted only garbage collecting one store at the end. Segfault persists in PKAR but at this point I'll have to look elsewhere for the cause.

57f4a9b2c8 effectively stores the handles in the globals table and now I can see busted only garbage collecting one store at the end. Segfault persists in PKAR but at this point I'll have to look elsewhere for the cause.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
scossu/volksdata_lua#1
No description provided.