Skip to content

Commit 5c2e3ce

Browse files
authored
Merge pull request #287 – Fix reconnection handling
#287
2 parents 532ffc0 + daf266a commit 5c2e3ce

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Lib/ldap/ldapobject.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,14 +1166,18 @@ def reconnect(self,uri,retry_max=1,retry_delay=60.0):
11661166
counter_text,uri
11671167
))
11681168
try:
1169-
# Do the connect
1170-
self._l = ldap.functions._ldap_function_call(ldap._ldap_module_lock,_ldap.initialize,uri)
1171-
self._restore_options()
1172-
# StartTLS extended operation in case this was called before
1173-
if self._start_tls:
1174-
SimpleLDAPObject.start_tls_s(self)
1175-
# Repeat last simple or SASL bind
1176-
self._apply_last_bind()
1169+
try:
1170+
# Do the connect
1171+
self._l = ldap.functions._ldap_function_call(ldap._ldap_module_lock,_ldap.initialize,uri)
1172+
self._restore_options()
1173+
# StartTLS extended operation in case this was called before
1174+
if self._start_tls:
1175+
SimpleLDAPObject.start_tls_s(self)
1176+
# Repeat last simple or SASL bind
1177+
self._apply_last_bind()
1178+
except ldap.LDAPError:
1179+
SimpleLDAPObject.unbind_s(self)
1180+
raise
11771181
except (ldap.SERVER_DOWN,ldap.TIMEOUT):
11781182
if __debug__ and self._trace_level>=1:
11791183
self._trace_file.write('*** %s reconnect to %s failed\n' % (
@@ -1185,7 +1189,6 @@ def reconnect(self,uri,retry_max=1,retry_delay=60.0):
11851189
if __debug__ and self._trace_level>=1:
11861190
self._trace_file.write('=> delay %s...\n' % (retry_delay))
11871191
time.sleep(retry_delay)
1188-
SimpleLDAPObject.unbind_s(self)
11891192
else:
11901193
if __debug__ and self._trace_level>=1:
11911194
self._trace_file.write('*** %s reconnect to %s successful => repeat last operation\n' % (

Tests/t_ldapobject.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,23 @@ def test104_reconnect_restore(self):
736736
l2 = pickle.loads(l1_state)
737737
self.assertEqual(l2.whoami_s(), 'dn:'+bind_dn)
738738

739+
def test105_reconnect_restore(self):
740+
l1 = self.ldap_object_class(self.server.ldap_uri, retry_max=2, retry_delay=1)
741+
bind_dn = 'cn=user1,'+self.server.suffix
742+
l1.simple_bind_s(bind_dn, 'user1_pw')
743+
self.assertEqual(l1.whoami_s(), 'dn:'+bind_dn)
744+
self.server._proc.terminate()
745+
self.server.wait()
746+
try:
747+
l1.whoami_s()
748+
except ldap.SERVER_DOWN:
749+
pass
750+
else:
751+
self.assertEqual(True, False)
752+
finally:
753+
self.server._start_slapd()
754+
self.assertEqual(l1.whoami_s(), 'dn:'+bind_dn)
755+
739756

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy