https://bugs.openldap.org/show_bug.cgi?id=10518
Issue ID: 10518 Summary: Every write transaction leaks a mutex on Windows Product: LMDB Version: unspecified Hardware: x86_64 OS: Windows Status: UNCONFIRMED Keywords: needs_review Severity: normal Priority: --- Component: liblmdb Assignee: bugs@openldap.org Reporter: bhargavabhat@gmail.com Target Milestone: ---
Created attachment 1152 --> https://bugs.openldap.org/attachment.cgi?id=1152&action=edit Create and use the mutex for the duration of the env
I see that on master3 branch, every write transaction leaks a mutex.
LMDB creates a new mutex on every non-read-only transaction in `mdb_txn_renew0`.
In `mdb_txn_end`, when the write transaction finishes, it sets `mode` to 0 and unlocks it.
``` if (!txn->mt_parent) { env->me_txn = NULL; mode = 0; if (env->me_txns) UNLOCK_MUTEX(env->me_wmutex); } ```
Later, it does this.
``` if (mode & MDB_END_FREE) { if (!F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) pthread_mutex_destroy(&txn->mt_child_mutex); // never reached free(txn); } ```
So we just don't close the mutex. It's probably not a big deal on non-Windows platforms, but on Windows, the number of handles owned by the process keeps increasing which limits the total number of write transactions we can do in the lifetime of the process.
I didn't really understand why we need to create a mutex on every non-read-only transaction. Can we just tie the lifetime of the mutex to the environment instead of the transaction?
I'm attaching a patch which tries to fix it.
https://bugs.openldap.org/show_bug.cgi?id=10518
Bhargava Srinarasi bhargavabhat@gmail.com changed:
What |Removed |Added ---------------------------------------------------------------------------- CC| |bhargavabhat@gmail.com
--- Comment #1 from Bhargava Srinarasi bhargavabhat@gmail.com --- Created attachment 1153 --> https://bugs.openldap.org/attachment.cgi?id=1153&action=edit Reproduction steps
I'm also attaching a function which reproduces the problem.
https://bugs.openldap.org/show_bug.cgi?id=10518
--- Comment #2 from Howard Chu hyc@openldap.org --- Thanks for the report. This bug was introduced in 10161b20fef84de5a07afdcbce3b59aea1123a6e for ITS#10395.
Unfortunately your patch will also leak mutexes when nested write transactions are used. We'll have to work out a different fix.
https://bugs.openldap.org/show_bug.cgi?id=10518
Howard Chu hyc@openldap.org changed:
What |Removed |Added ---------------------------------------------------------------------------- See Also| |https://bugs.openldap.org/s | |how_bug.cgi?id=10395
https://bugs.openldap.org/show_bug.cgi?id=10518
Howard Chu hyc@openldap.org changed:
What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |RESOLVED Resolution|--- |TEST
--- Comment #3 from Howard Chu hyc@openldap.org --- Fixed in git 741c1e8c899ddad6937297f66354c8e77c482f4f and 79d8edbe2e19e194cbbc0c2dd864ae61e819403a
https://bugs.openldap.org/show_bug.cgi?id=10518
--- Comment #4 from Quanah Gibson-Mount quanah@openldap.org --- mdb.RE/1.0:
• 79d8edbe by Howard Chu at 2026-06-08T16:56:53+01:00 ITS#10518 lmdb: fix mutex leak from ITS#10395
mdb.master3:
• 741c1e8c by Howard Chu at 2026-06-08T16:56:11+01:00 ITS#10518 lmdb: fix mutex leak from ITS#10395
https://bugs.openldap.org/show_bug.cgi?id=10518
Quanah Gibson-Mount quanah@openldap.org changed:
What |Removed |Added ---------------------------------------------------------------------------- Assignee|bugs@openldap.org |hyc@openldap.org Keywords|needs_review | Target Milestone|--- |1.0.0
https://bugs.openldap.org/show_bug.cgi?id=10518
--- Comment #5 from Bhargava Srinarasi bhargavabhat@gmail.com --- Works now. Thanks!
https://bugs.openldap.org/show_bug.cgi?id=10518
Quanah Gibson-Mount quanah@openldap.org changed:
What |Removed |Added ---------------------------------------------------------------------------- Status|RESOLVED |VERIFIED
https://bugs.openldap.org/show_bug.cgi?id=10518
Quanah Gibson-Mount quanah@openldap.org changed:
What |Removed |Added ---------------------------------------------------------------------------- Resolution|TEST |FIXED