Commit Graph

135 Commits

Author SHA1 Message Date
Tulir Asokan
d2de0f3aca Make sessions async
SQLiteSession is not updated, don't try to use it
2022-05-12 10:36:10 +02:00
Lonami Exo
4c771bf2af Fix setting logout result was not checking for future cancellation 2021-02-27 15:14:44 +01:00
Lonami Exo
292a36f760 Handle DestroySessionRes
Should close #1706.
2021-02-27 15:13:53 +01:00
Lonami Exo
8f0de3d285 Fix TypeNotFoundError was not being propagated
Closes #1697. This would cause deadlocks, as the request future
would never be resolved, so await would wait forever.
2021-02-11 19:27:57 +01:00
Lonami Exo
ee0fc5cc29 Fix AttributeError on reconnect with no retries
Closes #1646.
2020-12-11 21:43:39 +01:00
Lonami Exo
becfe2ce7a Start reconnect if a second ping is sent without a pong for the first
May help with #1564.
2020-12-11 17:18:25 +01:00
Lonami Exo
026c992395 Don't try to reconnect when authkey is invalid 2020-10-07 10:40:34 +02:00
Lonami Exo
09f4c5c708 Only reset auth_key on error -404
This error is "auth key not found", and the authorization key should
probably not be reset on other error codes. This might address #1457.
2020-10-05 14:08:21 +02:00
Lonami Exo
185a93a105 Expect BufferError during automatic reconnect
This seems to occur whe the Telegram servers are dying and logging
everyone out.
2020-10-05 14:07:11 +02:00
Lonami Exo
10b2b60415 Fix requests were not re-enqueued if sending failed 2020-09-29 21:07:38 +02:00
yash-dk
1d6fd7898a
Consider all reconnect attempts as retrying (#1557)
This means that a value of 0 retries will no longer try to reconnect.
2020-09-13 09:43:01 +02:00
Lonami Exo
1c3e7dda01 Avoid explicitly passing the loop to asyncio
This behaviour is deprecated and will be removed in future versions
of Python. Technically, it could be considered a bug (invalid usage
causing different behaviour from the expected one), and in practice
it should not break much code (because .get_event_loop() would likely
be the same event loop anyway).
2020-07-25 18:39:35 +02:00
Lonami Exo
1e94fe25fa Log requests that trigger struct.error
The exception hardly provides any valuable information.
This will hopefully help troubleshooting why the error happens.
2020-02-20 13:40:08 +01:00
Lonami Exo
3c253734ac Clear old docs and fix formatting in ConnectionError messages 2020-01-07 12:20:01 +01:00
Lonami Exo
cd37478e31 Don't send pings unless the connection is made
This will hopefully avoid batching tens of ping requests which
we don't care about their results.
2019-12-02 18:36:20 +01:00
Lonami Exo
5dcc30dcc6 Handle connection errors during auth key generation
This should help with spurious server-side disconnects during
auth_key generation, which happen most commonly on user DC
migrations.
2019-10-24 13:48:29 +02:00
Lonami Exo
08b78f0c47 Unify retry loops in mtprotosender._connect
Now the retry count is not twice its value.
2019-10-24 13:40:09 +02:00
Lonami Exo
3039915ce9 Factor out parts from mtprotosender._connect
This will help unifying the retry loop and reconnecting
if the server disconnects us during auth key generation
which will be done in a follow-up commit.
2019-10-24 13:36:32 +02:00
Lonami Exo
e24dd3ad75 Prevent double-connect causing double-reads later
Which leads to "RuntimeError: readexactly() called while another
coroutine is already waiting for incoming data" being raised,
and causing everything to break or halt.
2019-08-11 19:05:11 +02:00
Lonami Exo
f5de2cd9a0 Fix logging bug on disconnect 2019-08-08 09:32:18 +02:00
Lonami Exo
a7a7c4add2 Pass all Updates when building events 2019-06-30 16:34:34 +02:00
Lonami Exo
aa2b3daccc Factor out setting entities to events 2019-06-30 16:34:34 +02:00
Lonami
86cdb7c1f8
Remove accidentally-committed debug print 2019-06-15 23:14:59 +02:00
Lonami Exo
40d32cee95 Assert that dispatched updates are Updates 2019-06-15 21:15:57 +02:00
Lonami Exo
e5f1b2afa3 Update logging calls to use proper formatting 2019-05-17 12:30:13 +02:00
Lonami Exo
c1be0bd2e8 Fix disconnection without a previous connection 2019-05-11 16:53:11 +02:00
Lonami Exo
9965cda968 Save pts and date in a tuple for immutability
This way it is easy and cheap to copy the two required values
to all incoming updates in case we need to getDifference since
the previous pts/date to fetch entities.

This is still a work in progress.
2019-04-10 21:10:34 +04:00
Lonami Exo
ad963fd23e Don't clear pending_ack on disconnect
Upon reconnecting, we must make sure to send all `pending_ack`,
or Telegram may resend some responses (e.g. causing duplicated
updates).
2019-03-28 12:16:15 +01:00
Lonami Exo
5554b414e1 Propagate the last error on reconnect, not always ConnectionError 2019-03-28 10:11:33 +01:00
Lonami Exo
7523869875 Except IOError and not ConnectionError
PySocks' errors do not subclass ConnectionError so the library
was unnecessarily logging them as unexpected, when any IOError
was actually expected.
2019-03-28 09:54:35 +01:00
Lonami Exo
f6fe580eb7 Safer auto reconnect to prevent double connect 2019-03-22 19:01:40 +01:00
Lonami Exo
436fb64289 Prevent double autoreconnect like #1112 2019-03-22 16:21:18 +01:00
Lonami Exo
04ba2e1fc7 Revert disconnect() to be async again (#1133)
It's the only way to properly clean all background tasks,
which the library makes heavy use for in MTProto/Connection
send and receive loops.

Some parts of the code even relied on the fact that it was
asynchronous (it used to return a future so you could await
it and not be breaking).

It's automatically syncified to reduce the damage of being
a breaking change.
2019-03-21 12:21:00 +01:00
Dmitry D. Chernov
fc2977fc0d Tiny style fixes 2019-02-07 06:55:34 +10:00
Dmitry D. Chernov
c8f16a4e89 Fix a couple of inconsistencies in the public interface (#1102)
* Create `_NOT_A_REQUEST` when needed. Currently, modifications    
  in the raised exception would be "global".    
* `retries` parameters were actually attempts. This has been fixed    
  to actually be the amount of retries, so 0 now means don't retry.    
* Helper function to deal with retries instead of using a range with    
  different styles every time.
2019-02-06 19:41:45 +01:00
Lonami Exo
ae4d4ba3ef Fix-up missing loggers from f271316 2019-01-12 12:15:29 +01:00
Tulir Asokan
f271316d7d Make logger fully configurable (#1087) 2019-01-11 15:52:30 +01:00
Lonami Exo
95cf873bad Don't mark the user as connected until successfull connection
The idea behind distinguishing between the user being connected and
the actual connection having been made was to support retries while
the user decided to connect, even if the connection wasn't made yet.

The problem is that _user_connected is used directly to tell whether
the sender is connected or not which will be wrong if an exception
occurs on the initial connection. Since the logic retry isn't used
there we can simply mark as connected once a successfull connection
is made.
2019-01-02 10:53:10 +01:00
Lonami Exo
10b9b4b969 Handle TimeoutError on automatic reconnect 2018-11-29 13:11:34 +01:00
Lonami Exo
cdbd1f6193 Fix valid auth_key never being saved after switching DC 2018-11-03 18:53:26 +01:00
painor
45999001be Added retry_delay parameter for auto-reconnection (#1031) 2018-10-28 10:55:58 +01:00
Lonami Exo
091180b32d Except CancelledError in MTProtoSender send/recv loop
This way, the tasks should not end with the cancelled exception
which should get rid of the warning if any.
2018-10-25 15:54:25 +02:00
Lonami Exo
4562ba9ccf Fix auth_key not actually being saved
This would make the user have to login every time.
2018-10-22 20:58:07 +02:00
Lonami Exo
7dece209a0 Cancel tasks on reconnect instead of awaiting them
This prevents us from locking forever on any task that doesn't
rely on cancellation tokens, in this case, Connection.recv()'s
_recv_queue.get() would never complete after the server closed
the connection.

Additionally, working with cancellation tokens in asyncio is
somewhat annoying to do.

Last but not least removing the Connection._disconnected future
avoids the need to use its state (if an exception was set it
should be retrieved) to prevent asyncio from complaining, which
it was before.
2018-10-21 16:20:05 +02:00
Lonami Exo
0686ec4440 Implement after_msg_id back 2018-10-19 16:53:50 +02:00
Lonami Exo
9dc4009152 Handle the right errors 2018-10-19 14:41:50 +02:00
Lonami Exo
b93b01cb02 Fix manual BadMessageError instantiation 2018-10-19 14:02:20 +02:00
Lonami Exo
6b280dc3bd Clarify MTProtoSender._disconnected 2018-10-19 14:01:03 +02:00
Lonami Exo
945b34b103 Set MTProtoSender.auth_key on its creation 2018-10-19 13:56:11 +02:00
Lonami Exo
8563b9560d Fix auth_key not being set 2018-10-19 13:35:29 +02:00