Make stopping the client safe#664
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens shutdown behavior for the Telegram client/TDLib wrapper by preventing indefinite blocking during stop() and by guarding TDLib calls after the underlying TDLib client handle has been destroyed.
Changes:
- Add a
close_timeoutparameter toTelegram.stop()and implement bounded waiting in_close()while polling authorization state. - Introduce
ClientDestroyedErrorand ensureTDJson.send/receive/td_executeraise instead of passing a NULL handle into the C library. - Extend tests and changelog entries to cover safe shutdown scenarios and listener behavior when the client is destroyed.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/test_telegram_methods.py | Adds regression tests ensuring stop() and _close() finish under timeouts/errors and listener exits on destroyed client. |
| tests/test_tdjson.py | Adds tests that TDJson methods raise after stop and do not call underlying C functions. |
| telegram/utils.py | Updates AsyncResult.wait() timeout type to float to support sub-second timeouts. |
| telegram/tdjson.py | Adds ClientDestroyedError, tracks the client handle as nullable, and guards C calls via _get_client(). |
| telegram/client.py | Adds bounded shutdown (close_timeout), improves robustness of stop(), and prevents listener spinning on destroyed-client usage. |
| docs/source/changelog.rst | Documents the new safe-stop behavior and the post-stop exception change. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
205
to
208
| result = self.get_authorization_state() | ||
| self.authorization_state = self._wait_authorization_result(result) | ||
| self.authorization_state = self._wait_authorization_result(result, timeout=time_left) | ||
| logger.info("Authorization state: %s", self.authorization_state) | ||
| time.sleep(0.5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes two ways
stop()can break the client._close()waits for theCLOSEDauthorization state with no timeout andraise_exc=True. When tdlib does not answer,stop()blocks forever. When it answers with an error, the exception escapes beforeself._stopped.set(), soidle()never returns, the listener thread keeps running and the tdlib client is never destroyed.TDJson.stop()setstd_json_clienttoNone, butsend,receiveandtd_executekept passing it to tdlib. ctypes turnsNoneintoNULLand tdlib dereferences it, so any call afterstop()killed the process instead of raising. They now raiseClientDestroyedError, and the listener thread breaks out of its loop on it instead of spinning.