8000 Call `Application.post_stop` Only if `Application.stop` was called (#… · gtkacz/python-telegram-bot@b496fab · GitHub
[go: up one dir, main page]

Skip to content

Commit b496fab

Browse files
authored
Call Application.post_stop Only if Application.stop was called (python-telegram-bot#4211)
1 parent 637b8e2 commit b496fab

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

telegram/ext/_application.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,9 @@ def __run(
10861086
loop.run_until_complete(self.updater.stop()) # type: ignore[union-attr]
10871087
if self.running:
10881088
loop.run_until_complete(self.stop())
1089-
if self.post_stop:
1090-
loop.run_until_complete(self.post_stop(self))
1089+
# post_stop should be called only if stop was called!
1090+
if self.post_stop:
1091+
loop.run_until_complete(self.post_stop(self))
10911092
loop.run_until_complete(self.shutdown())
10921093
if self.post_shutdown:
10931094
loop.run_until_complete(self.post_shutdown(self))

telegram/ext/_applicationbuilder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,13 @@ def post_stop(
13341334
13351335
Tip:
13361336
This can be used for custom stop logic that requires to await coroutines, e.g.
1337-
sending message to a chat before shutting down the bot
1337+
sending message to a chat before shutting down the bot.
1338+
1339+
Hint:
1340+
The callback will be called only, if :meth:`Application.stop` was indeed called
1341+
successfully. For example, if the application is stopped early by calling
1342+
:meth:`Application.stop_running` within :meth:`post_init`, then the set callback will
1343+
*not* be called.
13381344
13391345
Example:
13401346
.. code::

tests/ext/test_application.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,6 @@ def after(_, name):
21342134
"app_initialize",
21352135
"updater_initialize",
21362136
"app_shutdown",
2137-
"post_stop",
21382137
"post_shutdown",
21392138
"updater_shutdown",
21402139
}
@@ -2441,7 +2440,8 @@ async def callback(*args, **kwargs):
24412440
app.run_polling(close_loop=False)
24422441

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

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

0 commit comments

Comments
 (0)
0