Ruby-ldap timeout

Hello list,

Does anyone know if ruby-ldap supports connection timeouts? We have LDAP integrated into our authentication system to provide LDAP authentication as well. It works fine if you input a valid host/port and if the LDAP server is responding ok. However, if the host doesn’t respond for some reason (could be an invalid IP), ruby-ldap hangs there waiting forever, hanging the application, something that obvisouly could cause potential problems (since it uses passenger).

Any ideas on how to handle that?



Hi Marcelo,

If that ldap lib doesn't include any timeout settings/params for
timing out long-running ldap calls (which if you're talking about ruby-
net-ldap, it doesn't at this time), then one way would be to wrap
those potentially-long-running calls in ruby's timeout (http://ruby- to at least force a timeout on your
app's side of the process, something like:

  TIMEOUT_SECS = 10 # or whatever the max should be.

  timeout_status = nil
    timeout_status = Timeout::timeout(TIMEOUT_SECS) do
      # do stuff that might take too long ....
  rescue Timeout::Error => te
    # log it and ...


Hi Jeff,

Thanks! Every single day I’m amazed by what’s possible with Ruby, the more I learn, the less I know, but I guess that’s good :slight_smile:

This led me to the SystemTimer gem. The Timeout class was failing, I guess because it is external call that “cross the barriers”, as explained here: SystemTimer works like a charm :wink:



Correction. SystemTimer works when binding, but does not work with the search / search2 method(s). If the LDAP server hangs for any reason, SystemTimer fails to kill the thread, and even though the Timeout::Error exception is thrown, it is only after much more time than what you define in the timeout_after(time) method.

I’ve tried to contact Philippe, the author regarding this, let’s see how it goes. Meanwhile, if anyone has a clue on why it is failing, please share!




Does anyone have any idea on why it fails?