On Fri, Sep 05, 2025 at 02:47:52PM +0530, venugopal chinnakotla wrote:
> Hi Team,
>
> We are working on migration of nsldap C sdk to OpenLDAP C sdk for our
> application client code.
>
> We are using OpenLDAP 2.6.7.
>
> As part of this migration activity,we used ldap_initialize() API for
> initializing and getting LDAP Handle. Our application supports multi
> threading. So, ldap_initialize() API will be called from multi threaded
> code. For example, 2 threads will call ldap_initialize() at same
> time/simultaneously.
>
> When 2 threads call ldap_initialize() at same time, sometimes observed a
> race condition during this API call. Due to this, the next api calls
> ldap_simple_bind_s or ldap_start_tls_s are getting failed for 2 threads and
> it is not recovered automatically until restarts our applications. We
> observed this race condition multiple times.
>
> Because of this issue, we are unable to start our application and it
> becomes a blocker for us to proceed further.
>
> After checking ldap_initialze() API code from openldap source code, noticed
> that race condition happened while initializing/setting global options.
>
> Are there any known issues with OpenLDAP when we use it in multi thread
> supported applications?
>
> Could you please check and provide a solution.
Hi Venugopal,
libldap documentation[0] explicitly asks that libldap initialisation be
done in a thread safe way (=not concurrently as you're doing). Ignoring
that (at your peril), you should be calling ldap_get/set_option() first
to give the library a chance to initialise itself **before** you call
ldap_initialize() to set up a new session, again as the documentation
suggests.
[0]. Quoting from https://openldap.org/software/man.cgi?query=ldap_initialize
Note: the first call into the LDAP library also initializes the global
options for the library. As such the first call should be single-
threaded or otherwise protected to insure that only one call is active.
It is recommended that ldap_get_option() or ldap_set_option() be used
in the program's main thread before any additional threads are created.
See ldap_get_option(3).
Regards,
--
Ondřej Kuzník
Senior Software Engineer
Symas Corporation http://www.symas.com
Packaged, certified, and supported LDAP solutions powered by OpenLDAP
On Fri, Sep 05, 2025 at 03:26:28PM +0530, venugopal chinnakotla wrote:
> Hi Team,
>
> We are working on migration of nsldap C sdk to OpenLDAP c sdk for our
> application client code. As part of this activity, replacing API one by one
> and also looking for constant replacement.
>
> In NSLDAP C SDK: /* * Thread function callbacks (an API extension -- *
> LDAP_API_FEATURE_X_THREAD_FUNCTIONS). */ #define LDAP_OPT_THREAD_FN_PTRS
> 0x05 /* 5 - API extension */
>
> Now, we are looking for similar functionality within OpenLDAP to use it in
> our client program. I Went through OpenLDAP documents and source code, but
> to my knowledge, did not get any equivalent one.
>
> Could you please provide more details on this to achieve equivalent
> functionality of LDAP_OPT_THREAD_FN_PTRS with OpenLDAP?
Hi Venugopal,
in libldap, these functions are not runtime settable, the correct
implementation is selected at compile time based on the target
environment, usually PThreads or NT Threads.
Regards,
--
Ondřej Kuzník
Senior Software Engineer
Symas Corporation http://www.symas.com
Packaged, certified, and supported LDAP solutions powered by OpenLDAP