noirello / bonsai

Simple Python 3 module for LDAP, using libldap2 and winldap C libraries.
MIT License
117 stars 33 forks source link

Conflict with pycurl (async code) #16

Closed okorolev closed 6 years ago

okorolev commented 6 years ago

Hi!

Code:

    import asyncio
    import pycurl # code work whitout this import
    from bonsai import LDAPClient, LDAPSearchScope

    async def do():
        LDAP_SERVER = ''
        LDAP_USER = ''  
        LDAP_PASSWORD = ''

        client = LDAPClient(LDAP_SERVER)
        client.set_credentials("SIMPLE", (LDAP_USER, LDAP_PASSWORD))

        async with client.connect(is_async=True) as conn:
            await conn.search(
                base="cn=users_catalog,ou=users,dc=<DC>",
                scope=LDAPSearchScope.SUBTREE,
            )

    loop = asyncio.get_event_loop()
    loop.run_until_complete(do())

Output:

Assertion failed: (LDAP_VALID( ld )), function ldap_set_option, file options.c, line 456.

Platform:

$ python -V                                                                                                                            
Python 3.6.3
$ uname -a
Darwin MacBook-Pro-admin.local 17.0.0 Darwin Kernel Version 17.0.0: Thu Aug 24 21:48:19 PDT 2017; root:xnu-4570.1.46~2/RELEASE_X86_64 x86_64
$ clang -v                                                                                                                          
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin17.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
noirello commented 6 years ago

Thanks for reporting it. I've successfully reproduced the bug. It doesn't even have to be an asynchronous connection, just importing pycurl.

It looks very odd. I'll look into it thoroughly, as soon as I can.

noirello commented 6 years ago

I think I've found the source of the problem:

So when importing both modules, the interpreter loads two different versions of the libldap library causing this weird assertion failure that you've experienced.

To prove my theory I built curl on a Mac without LDAP/LDAPS support, built and installed the pycurl module (linked with my LDAPless libcurl library) then I was able to import both pycurl and bonsai and ran the example.

But unfortunately, I don't have a fix that can solve this. The best solution that I read so far was rebuilding the libcurl to also use the newer libldap library. I haven't come across any other solution that can solve this somehow with code or configuration (on bonsai's side).