node-LDAP icon indicating copy to clipboard operation
node-LDAP copied to clipboard

Can't connect, this.bind issue

Open guillaumefe opened this issue 7 years ago • 2 comments

I surely miss something.

I use slapd on debian 8.

When I put this.bind into connect, it never finishes (even if I put connecttimeout to 1 or 10 or whatever).

On the other hands ldap.bind works well in the new LDAP callback.

const LDAP = require('ldap-client');

var connect = function(ctx) {
    ctx.bind({
        binddn: 'cn=admin,dc=my,dc=io',
        password: 'pass'
    }, function(err) {
        if(err) console.log(err);
    }); 
}

var ldap = new LDAP({
    uri:             'ldap://localhost',
        validatecert:    false,
        connecttimeout:  -1,
        base:            'dc=my,dc=io',
        attrs:           '*',
        filter:          '(objectClass=*)',
        scope:           2,
        connect:         function(err) {
            if(err) console.log(err);
            connect(this);
        },
        disconnect:      function() {},        
}, function(err) {
    if(err) console.log(err);
    connect(ldap);
});

I'm stuck, please could you help me to understand or explain best what is happening?

guillaumefe avatar Dec 30 '17 13:12 guillaumefe

Up please

guillaumefe avatar Jan 04 '18 08:01 guillaumefe

I just hit this too; you write a connect function as per the example in the README.md and it locks the whole VM up, so it's not yielding at all. Nasty.

I was able to work around this as follows, instead of this (which locks nodejs up):

new LDAP({
    connect: function () {
        this.simplebind({
            binddn: binddn,
            password: password,
        }, function (err) {
            console.log("this is never called");
        });
        console.log("this is never called; this.simplebind never returns");
    },
});

I instead used process.nextTick like this, which then works as expected. Obviously connect will complete without the bind necessarily finishing but in my case this is not an issue as I wrapped the whole thing in a promise, which can be resolved on successful bind, thus hiding the whole problem from the caller:

new LDAP({
    connect: function () {
        process.nextTick(() => {
            this.simplebind({
                binddn: binddn,
                password: password,
            }, function (err) {
                console.log("this IS now called, with an error if something went wrong");
            });
            console.log("this IS now called; this.simplebind returns");
        });
    },
});

I guess there is a deadlock, or a race condition that stops bind working in the connect call synchronously, but I've not looked into it.

mwri avatar Mar 28 '19 09:03 mwri