Skip to content

Commit b23b41b

Browse files
committed
Lock the send method for every tcp mode, not only for tcp_full
1 parent dde01cc commit b23b41b

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

pyrogram/connection/connection.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20+
import threading
2021
import time
2122

2223
from .transport import *
@@ -34,6 +35,7 @@ class Connection:
3435
def __init__(self, ipv4: str, mode: int = 1):
3536
self.address = (ipv4, 80)
3637
self.mode = self.MODES.get(mode, TCPAbridged)
38+
self.lock = threading.Lock()
3739
self.connection = None
3840

3941
def connect(self):
@@ -53,7 +55,8 @@ def close(self):
5355
self.connection.close()
5456

5557
def send(self, data: bytes):
56-
self.connection.send(data)
58+
with self.lock:
59+
self.connection.send(data)
5760

5861
def recv(self) -> bytes or None:
5962
return self.connection.recv()

pyrogram/connection/transport/tcp/tcp_full.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import logging
2020
from binascii import crc32
2121
from struct import pack, unpack
22-
from threading import Lock
2322

2423
from .tcp import TCP
2524

@@ -30,22 +29,19 @@ class TCPFull(TCP):
3029
def __init__(self):
3130
super().__init__()
3231
self.seq_no = None
33-
self.lock = Lock()
3432

3533
def connect(self, address: tuple):
3634
super().connect(address)
3735
self.seq_no = 0
3836
log.info("Connected!")
3937

4038
def send(self, data: bytes):
41-
with self.lock:
42-
# 12 = packet_length (4), seq_no (4), crc32 (4) (at the end)
43-
data = pack("<II", len(data) + 12, self.seq_no) + data
44-
data += pack("<I", crc32(data))
39+
# 12 = packet_length (4), seq_no (4), crc32 (4) (at the end)
40+
data = pack("<II", len(data) + 12, self.seq_no) + data
41+
data += pack("<I", crc32(data))
42+
self.seq_no += 1
4543

46-
self.seq_no += 1
47-
48-
super().sendall(data)
44+
super().sendall(data)
4945

5046
def recv(self) -> bytes or None:
5147
length = self.recvall(4)

0 commit comments

Comments
 (0)