Skip to content

Retry on EINTR #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rgbyrnes opened this issue Sep 25, 2018 · 2 comments
Open

Retry on EINTR #242

rgbyrnes opened this issue Sep 25, 2018 · 2 comments

Comments

@rgbyrnes
Copy link

rgbyrnes commented Sep 25, 2018

python-ldap doesn't appear to retry ldap calls that fail with EINTR.

This is fairly easy to reproduce. If we run a loop doing repeated ldap calls, install a signal handler (that does nothing), and then send the handled signal a few times to the process, it fails almost immediately, raising an exception like ...

SERVER_DOWN: {u'info': 'Interrupted system call', 'errno': 4, 'desc': u"Can't contact LDAP server"}

It's also fairly easy to fix, since all of the ldap calls seem to happen in ldap.ldapobject.SimpleLDAPObject._ldap_call. If I add a little wrapper like ...

from errno import EINTR

def _retry_if_interrupted(func, *args, **kwargs):
  """
  Call func, retrying if it raises an LDAPError with errno == EINTR.
  """
  while True:
    try:
      return func(*args, **kwargs)
    except LDAPError as e:
      try:
        if e.args[0]['errno'] != EINTR:
          raise
      except (AttributeError, IndexError, KeyError):
        raise e

... and change the ldap call to ...

        result = _retry_if_interrupted(func, *args, **kwargs)

... then I can no longer reproduce the issue.

We have observed this as the source of sporadic ldap failures in real apps. Requiring callers to do their own EINTR retries seems onerous, and raising exceptions for EINTR "failures" doesn't seem useful, hence the suggestion to retry.

I didn't track down where EINTR originates within OpenLDAP. The OpenLDAP code has many references to EINTR that suggest an intention to retry ... maybe some cases were missed? Retrying by python-ldap seems consistent with that, at least.

Operating system: Linux, RHEL 7.5 with kernel-3.10.0-862.11.6.el7.x86_64
Python version: 2.7.15
python-ldap version: 3.1.0
OpenLDAP version: 2.4.39

rgbyrnes added a commit to rgbyrnes/python-ldap that referenced this issue May 24, 2019
mit-da added a commit to mit-da/python-ldap that referenced this issue Jun 14, 2023
Try and fix issue python-ldap#242 by adding a retry on EINTR and a sleep before attempting to do so.
@spaceone
Copy link
Contributor

doesn't it work when you use ldap.ldapobject.ReconnectLDAPObject instead of ldap.ldapobject.SimpleLDAPObject?

@Breloc
Copy link

Breloc commented Sep 20, 2023

I met the same problem, but I've managed to solve it by enabling OPT_RESTART with :

ldap.set_option(ldap.OPT_RESTART, ldap.OPT_ON)

This option is not well documented, I've found it in openldap/doc/drafts/draft-ietf-ldapext-ldap-c-api-xx.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants