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

fix socket shutdown bug #143

Merged
merged 1 commit into from
Jun 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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