Skip to content

Commit

Permalink
Merge pull request #287 – Fix reconnection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Viktorin authored and GitHub committed Jan 29, 2020
2 parents 532ffc0 + daf266a commit 5c2e3ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
21 changes: 12 additions & 9 deletions Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1166,14 +1166,18 @@ def reconnect(self,uri,retry_max=1,retry_delay=60.0):
counter_text,uri
))
try:
# Do the connect
self._l = ldap.functions._ldap_function_call(ldap._ldap_module_lock,_ldap.initialize,uri)
self._restore_options()
# StartTLS extended operation in case this was called before
if self._start_tls:
SimpleLDAPObject.start_tls_s(self)
# Repeat last simple or SASL bind
self._apply_last_bind()
try:
# Do the connect
self._l = ldap.functions._ldap_function_call(ldap._ldap_module_lock,_ldap.initialize,uri)
self._restore_options()
# StartTLS extended operation in case this was called before
if self._start_tls:
SimpleLDAPObject.start_tls_s(self)
# Repeat last simple or SASL bind
self._apply_last_bind()
except ldap.LDAPError:
SimpleLDAPObject.unbind_s(self)
raise
except (ldap.SERVER_DOWN,ldap.TIMEOUT):
if __debug__ and self._trace_level>=1:
self._trace_file.write('*** %s reconnect to %s failed\n' % (
Expand All @@ -1185,7 +1189,6 @@ def reconnect(self,uri,retry_max=1,retry_delay=60.0):
if __debug__ and self._trace_level>=1:
self._trace_file.write('=> delay %s...\n' % (retry_delay))
time.sleep(retry_delay)
SimpleLDAPObject.unbind_s(self)
else:
if __debug__ and self._trace_level>=1:
self._trace_file.write('*** %s reconnect to %s successful => repeat last operation\n' % (
Expand Down
17 changes: 17 additions & 0 deletions Tests/t_ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,23 @@ def test104_reconnect_restore(self):
l2 = pickle.loads(l1_state)
self.assertEqual(l2.whoami_s(), 'dn:'+bind_dn)

def test105_reconnect_restore(self):
l1 = self.ldap_object_class(self.server.ldap_uri, retry_max=2, retry_delay=1)
bind_dn = 'cn=user1,'+self.server.suffix
l1.simple_bind_s(bind_dn, 'user1_pw')
self.assertEqual(l1.whoami_s(), 'dn:'+bind_dn)
self.server._proc.terminate()
self.server.wait()
try:
l1.whoami_s()
except ldap.SERVER_DOWN:
pass
else:
self.assertEqual(True, False)
finally:
self.server._start_slapd()
self.assertEqual(l1.whoami_s(), 'dn:'+bind_dn)


if __name__ == '__main__':
unittest.main()

0 comments on commit 5c2e3ce

Please sign in to comment.