Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call Application.post_stop Only if Application.stop was called #4211

Merged
merged 24 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0c27c50
PoC for handling systemexit beter
Bibo-Joshi Mar 12, 2024
fb188fe
Rework `_updater_fetcher`
Bibo-Joshi Mar 29, 2024
9760bdd
Merge branch 'master' into handle-system-exit
Bibo-Joshi Mar 29, 2024
a690fcc
Better shutdown in `__run`
Bibo-Joshi Mar 29, 2024
95c98ff
Support calling `Application.stop_running` within `Application.post_i…
Bibo-Joshi Apr 7, 2024
980a99e
Add a test for `post_init`
Bibo-Joshi Apr 7, 2024
3640255
Merge branch 'master' into handle-system-exit
Bibo-Joshi Apr 7, 2024
5fc0ac5
Merge branch 'master' into handle-system-exit
Bibo-Joshi Apr 17, 2024
f011c60
Improve a docstring
Bibo-Joshi Apr 17, 2024
5fb5b75
Do the tests. let's pray they pass …
Bibo-Joshi Apr 17, 2024
33be81d
Skip post_stop if stop was not called
Bibo-Joshi Apr 17, 2024
b9d5250
Merge branch 'master' into handle-system-exit
Bibo-Joshi Apr 26, 2024
dc45cb0
Merge branch 'master' into handle-system-exit
Bibo-Joshi Apr 28, 2024
a60b7d3
Remove all non-failing tests to see if that does something
Bibo-Joshi Apr 29, 2024
5af59ef
Revert "Remove all non-failing tests to see if that does something"
Bibo-Joshi Apr 29, 2024
e675a43
Update telegram/ext/_application.py
Bibo-Joshi May 10, 2024
e7c74b9
review
Bibo-Joshi May 10, 2024
86f0c91
Review
Bibo-Joshi May 20, 2024
9930475
Merge branch 'handle-system-exit' into post-stop-fix
Bibo-Joshi May 20, 2024
7f1685b
Merge branch 'master' into post-stop-fix
Bibo-Joshi May 20, 2024
a578829
Merge branch 'master' into handle-system-exit
Bibo-Joshi May 20, 2024
07799a2
Merge branch 'handle-system-exit' into post-stop-fix
Bibo-Joshi May 20, 2024
8836a11
Merge branch 'master' into post-stop-fix
Bibo-Joshi May 20, 2024
7087c14
Update telegram/ext/_application.py
Bibo-Joshi May 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions telegram/ext/_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,8 +1086,9 @@ def __run(
loop.run_until_complete(self.updater.stop()) # type: ignore[union-attr]
if self.running:
loop.run_until_complete(self.stop())
if self.post_stop:
loop.run_until_complete(self.post_stop(self))
# post_stop should be called only if stop was called!
if self.post_stop:
loop.run_until_complete(self.post_stop(self))
loop.run_until_complete(self.shutdown())
if self.post_shutdown:
loop.run_until_complete(self.post_shutdown(self))
Expand Down
8 changes: 7 additions & 1 deletion telegram/ext/_applicationbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,13 @@ def post_stop(

Tip:
This can be used for custom stop logic that requires to await coroutines, e.g.
sending message to a chat before shutting down the bot
sending message to a chat before shutting down the bot.

Hint:
The callback will be called only, if :meth:`Application.stop` was indeed called
successfully. For example, if the application is stopped early by calling
:meth:`Application.stop_running` within :meth:`post_init`, then the set callback will
*not* be called.

Example:
.. code::
Expand Down
4 changes: 2 additions & 2 deletions tests/ext/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,6 @@ def after(_, name):
"app_initialize",
"updater_initialize",
"app_shutdown",
"post_stop",
"post_shutdown",
"updater_shutdown",
}
Expand Down Expand Up @@ -2441,7 +2440,8 @@ async def callback(*args, **kwargs):
app.run_polling(close_loop=False)

# The important part here is that start(_polling) are *not* called!
assert called_callbacks == ["post_stop", "post_shutdown"]
# post_stop must not be called either, since we never called stop()
assert called_callbacks == ["post_shutdown"]

assert len(caplog.records) == 1
assert caplog.records[-1].name == "telegram.ext.Application"
Expand Down