https://bugs.openldap.org/show_bug.cgi?id=9486
--- Comment #4 from Howard Chu hyc@openldap.org --- (In reply to Howard Chu from comment #3)
(In reply to igfoo from comment #2)
Thanks for the quick response. Unfortunately, in the real code, each process only opens an env once, but two processes might do so at the same time.
Are you referring to the "Opening a database can fail if another process is opening or closing it at exactly the same time." caveat? If so, presumably we could just wait a random amount of time and try again. Is there any documentation anywhere about what these failures might look like?
No, the behavior is undefined.
Your best bet is just to stagger the startup of your processes.
Your other option is to just run a dummy process to open the env before any other processes start. Then the env & lockfile will always be already initialized whenever any other process opens it, and the problem goes away. E.g.:
#include <lmdb.h> #include <stdio.h> #include <stdlib.h>
void err(char *func, int err) { fprintf(stderr, "%s failed with %d\n", func, err); exit(1); }
int main(int argc, char **argv) { const char *db_file = "test.db"; MDB_env* env = NULL; int e = mdb_env_create(&env); if(e) err("mdb_env_create", e); e = mdb_env_open(env, db_file, MDB_NOSUBDIR | MDB_NOSYNC, 0644); if(e) err("mdb_env_open", e); printf("Press return to exit: "); fflush(stdout); getchar(); mdb_env_close(env); return 0; }