Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #143 from maralla/socket_shutdown
Browse files Browse the repository at this point in the history
fix socket shutdown bug
  • Loading branch information
wooparadog committed Jun 27, 2015
2 parents 5319ad2 + 40f97ec commit 4fe4bf6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
27 changes: 27 additions & 0 deletions tests/test_socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-

import pytest

from thriftpy.transport.socket import TSocket, TServerSocket
from thriftpy.transport import TTransportException


def test_close():
server = TServerSocket(host="localhost", port=12345)
client = TSocket(host="localhost", port=12345)

server.listen()
client.open()

c = server.accept()

client.close()

with pytest.raises(TTransportException) as e:
c.read(1024)
assert "TSocket read 0 bytes" in e.value.message

c.write(b"world")
c.close()

assert c.handle is None
9 changes: 7 additions & 2 deletions thriftpy/transport/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ def _resolveAddr(self):
socket.AI_PASSIVE | socket.AI_ADDRCONFIG)

def close(self):
if self.handle:
if not self.handle:
return

try:
self.handle.shutdown(socket.SHUT_RDWR)
self.handle.close()
self.handle = None
except socket.error:
pass
self.handle = None


class TSocket(TSocketBase):
Expand Down

0 comments on commit 4fe4bf6

Please sign in to comment.