Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit a48d27f

Browse files
committed
Always run crypto-related functions in the dedicated thread
1 parent 7dda167 commit a48d27f

File tree

4 files changed

+13
-19
lines changed

4 files changed

+13
-19
lines changed

pyrogram/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ class ContinuePropagation(StopAsyncIteration):
4646

4747
CRYPTO_EXECUTOR_SIZE_THRESHOLD = 512
4848

49-
crypto_executor = ThreadPoolExecutor(2, thread_name_prefix="CryptoWorker")
49+
crypto_executor = ThreadPoolExecutor(1, thread_name_prefix="CryptoWorker")

pyrogram/connection/transport/tcp/tcp_abridged_o.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818

1919
import logging
2020
import os
21-
2221
from typing import Optional
2322

24-
from pyrogram import utils
25-
23+
import pyrogram
2624
from pyrogram.crypto import aes
2725
from .tcp import TCP
2826

@@ -60,7 +58,7 @@ async def connect(self, address: tuple):
6058
async def send(self, data: bytes, *args):
6159
length = len(data) // 4
6260
data = (bytes([length]) if length <= 126 else b"\x7f" + length.to_bytes(3, "little")) + data
63-
payload = await utils.maybe_run_in_executor(aes.ctr256_encrypt, data, len(data), self.loop, *self.encrypt)
61+
payload = await self.loop.run_in_executor(pyrogram.crypto_executor, aes.ctr256_encrypt, data, *self.encrypt)
6462

6563
await super().send(payload)
6664

@@ -85,4 +83,4 @@ async def recv(self, length: int = 0) -> Optional[bytes]:
8583
if data is None:
8684
return None
8785

88-
return await utils.maybe_run_in_executor(aes.ctr256_decrypt, data, len(data), self.loop, *self.decrypt)
86+
return await self.loop.run_in_executor(pyrogram.crypto_executor, aes.ctr256_decrypt, data, *self.decrypt)

pyrogram/session/session.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from io import BytesIO
2626

2727
import pyrogram
28-
from pyrogram import __copyright__, __license__, __version__, utils
28+
from pyrogram import __copyright__, __license__, __version__
2929
from pyrogram import raw
3030
from pyrogram.connection import Connection
3131
from pyrogram.crypto import mtproto
@@ -217,8 +217,10 @@ async def restart(self):
217217
await self.start()
218218

219219
async def handle_packet(self, packet):
220-
data = await utils.maybe_run_in_executor(
221-
mtproto.unpack, BytesIO(packet), len(packet), self.loop,
220+
data = await self.loop.run_in_executor(
221+
pyrogram.crypto_executor,
222+
mtproto.unpack,
223+
BytesIO(packet),
222224
self.session_id,
223225
self.auth_key,
224226
self.auth_key_id
@@ -360,8 +362,10 @@ async def _send(self, data: TLObject, wait_response: bool = True, timeout: float
360362
log.debug(f"Sent:")
361363
log.debug(message)
362364

363-
payload = await utils.maybe_run_in_executor(
364-
mtproto.pack, message, len(message), self.loop,
365+
payload = await self.loop.run_in_executor(
366+
pyrogram.crypto_executor,
367+
mtproto.pack,
368+
message,
365369
self.current_salt.salt,
366370
self.session_id,
367371
self.auth_key,

pyrogram/utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,3 @@ async def parse_text_entities(
309309
"message": text,
310310
"entities": entities
311311
}
312-
313-
314-
async def maybe_run_in_executor(func, data, length, loop, *args):
315-
return (
316-
func(data, *args)
317-
if length <= pyrogram.CRYPTO_EXECUTOR_SIZE_THRESHOLD
318-
else await loop.run_in_executor(pyrogram.crypto_executor, func, data, *args)
319-
)

0 commit comments

Comments
 (0)