Skip to content

Commit

Permalink
Add more useful logging on invalid packet length received
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Jan 13, 2018
1 parent 0d429f5 commit c5e969d
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions telethon/network/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This module holds both the Connection class and the ConnectionMode enum,
which specifies the protocol to be used by the Connection.
"""
import logging
import os
import struct
from datetime import timedelta
Expand All @@ -14,6 +15,8 @@
from ..extensions import TcpClient
from ..errors import InvalidChecksumError

__log__ = logging.getLogger(__name__)


class ConnectionMode(Enum):
"""Represents which mode should be used to stabilise a connection.
Expand Down Expand Up @@ -181,6 +184,21 @@ def _recv_tcp_full(self):
packet_len_seq = self.read(8) # 4 and 4
packet_len, seq = struct.unpack('<ii', packet_len_seq)

# Sometimes Telegram seems to send a packet length of 0 (12)
# and after that, just a single byte. Not sure what this is.
# TODO Figure out what this is, and if there's a better fix.
if packet_len <= 12:
__log__.error('Read invalid packet length %d, '
'reading data left:', packet_len)
while True:
try:
__log__.error(repr(self.read(1)))
except TimeoutError:
break
# Connection reset and hope it's fixed after
self.conn.close()
raise ConnectionResetError()

body = self.read(packet_len - 12)
checksum = struct.unpack('<I', self.read(4))[0]

Expand Down

0 comments on commit c5e969d

Please sign in to comment.