Skip to content

Commit 3a4c9e8

Browse files
richard78917Richard Wolfert
authored and
Richard Wolfert
committed
[fix] block in close
In case that close() is called while doing read operation, the close() call is blocked and whole application is stucked. Shut-down of socket will resolve this issue and releases thread blocked at readline().
1 parent f426d37 commit 3a4c9e8

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 0.7.0 (2020-XX-XX)
44

5+
- Fixed issue when threads are blocked on close while reading, #11 by @richard78917
6+
57
## 0.6.0 (2020-01-08)
68

79
- Added Python 3.8.* support, #10

pynats/client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def connect(self) -> None:
154154
self._recv(INFO_RE)
155155

156156
def close(self) -> None:
157+
self._socket.shutdown(socket.SHUT_RDWR)
157158
self._socket_file.close()
158159
self._socket.close()
159160

@@ -279,7 +280,11 @@ def _readline(self, *, size: int = None) -> bytes:
279280
read = io.BytesIO()
280281

281282
while True:
282-
line = cast(bytes, self._socket_file.readline())
283+
raw_bytes = self._socket_file.readline()
284+
if not raw_bytes:
285+
raise RuntimeError("unable to read from socket")
286+
287+
line = cast(bytes, raw_bytes)
283288
read.write(line)
284289

285290
if size is not None:

0 commit comments

Comments
 (0)