mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-23 01:46:35 +03:00
Add a lock around TcpClient.close() to try avoiding None.close()
This commit is contained in:
parent
1d36bbfbf4
commit
d79dccc923
|
@ -3,12 +3,15 @@ import errno
|
||||||
import socket
|
import socket
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from io import BytesIO, BufferedWriter
|
from io import BytesIO, BufferedWriter
|
||||||
|
from threading import Lock
|
||||||
|
|
||||||
|
|
||||||
class TcpClient:
|
class TcpClient:
|
||||||
def __init__(self, proxy=None, timeout=timedelta(seconds=5)):
|
def __init__(self, proxy=None, timeout=timedelta(seconds=5)):
|
||||||
self._proxy = proxy
|
self._proxy = proxy
|
||||||
self._socket = None
|
self._socket = None
|
||||||
|
self._closing_lock = Lock()
|
||||||
|
|
||||||
if isinstance(timeout, timedelta):
|
if isinstance(timeout, timedelta):
|
||||||
self._timeout = timeout.seconds
|
self._timeout = timeout.seconds
|
||||||
elif isinstance(timeout, int) or isinstance(timeout, float):
|
elif isinstance(timeout, int) or isinstance(timeout, float):
|
||||||
|
@ -48,8 +51,13 @@ class TcpClient:
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Closes the connection"""
|
"""Closes the connection"""
|
||||||
|
if self._closing_lock.locked():
|
||||||
|
# Already closing, no need to close again (avoid None.close())
|
||||||
|
return
|
||||||
|
|
||||||
|
with self._closing_lock:
|
||||||
try:
|
try:
|
||||||
if self.connected:
|
if self._socket is not None:
|
||||||
self._socket.shutdown(socket.SHUT_RDWR)
|
self._socket.shutdown(socket.SHUT_RDWR)
|
||||||
self._socket.close()
|
self._socket.close()
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user