Skip to content

Commit 866259f

Browse files
author
Luke Bakken
committed
Fixes in using socket.shutdown on older Python
1 parent fe46c6d commit 866259f

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

riak/transports/tcp/connection.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import errno
12
import socket
23
import struct
34

@@ -9,12 +10,12 @@
910
from riak.security import SecurityError, USE_STDLIB_SSL
1011
from riak.transports.pool import BadResource
1112

12-
if not USE_STDLIB_SSL:
13-
from OpenSSL.SSL import Connection
14-
from riak.transports.security import configure_pyopenssl_context
15-
else:
13+
if USE_STDLIB_SSL:
1614
import ssl
1715
from riak.transports.security import configure_ssl_context
16+
else:
17+
from OpenSSL.SSL import Connection
18+
from riak.transports.security import configure_pyopenssl_context
1819

1920

2021
class TcpConnection(object):
@@ -208,7 +209,16 @@ def close(self):
208209
Closes the underlying socket of the PB connection.
209210
"""
210211
if self._socket:
211-
self._socket.shutdown(socket.SHUT_RDWR)
212+
if USE_STDLIB_SSL:
213+
# NB: Python 2.7.8 and earlier does not have a compatible
214+
# shutdown() method due to the SSL lib
215+
try:
216+
self._socket.shutdown(socket.SHUT_RDWR)
217+
except IOError as e:
218+
# NB: sometimes this is the exception if the initial
219+
# connection didn't succeed correctly
220+
if e.errno != errno.EBADF:
221+
raise
212222
self._socket.close()
213223
del self._socket
214224

0 commit comments

Comments
 (0)