Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Ensure Safe Handler Looping inApplication.process_update/error#4802

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Bibo-Joshi wants to merge9 commits intomaster
base:master
Choose a base branch
Loading
fromsafe-handler-loop

Conversation

Bibo-Joshi
Copy link
Member

@Bibo-JoshiBibo-Joshi commentedMay 25, 2025
edited by aelkheir
Loading

Based on user inputhttps://t.me/pythontelegrambotgroup/779167

Closes#4803

Noerrorhandlersareregistered,loggingexception.Traceback (mostrecentcalllast):File"/home/kuzunohalit/.local/lib/python3.12/site-packages/telegram/ext/_application.py",line1170,in_create_task_callbackreturnawaitcoroutine# type: ignore[misc]File"/home/kuzunohalit/.local/lib/python3.12/site-packages/telegram/ext/_application.py",line1232,in_process_update_wrapperawaitself._update_processor.process_update(update,self.process_update(update))File"/home/kuzunohalit/.local/lib/python3.12/site-packages/telegram/ext/_baseupdateprocessor.py",line170,inprocess_updateawaitself.do_process_update(update,coroutine)File"/home/kuzunohalit/.local/lib/python3.12/site-packages/telegram/ext/_baseupdateprocessor.py",line195,indo_process_updateawaitcoroutineFile"/home/kuzunohalit/.local/lib/python3.12/site-packages/telegram/ext/_application.py",line1259,inprocess_updateforhandlersinself.handlers.values():RuntimeError:dictionarychangedsizeduringiteration

@Bibo-JoshiBibo-Joshi added the 🔌 bugpr description: bug labelMay 25, 2025
Copy link
Member

@aelkheiraelkheir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM

At first I thought the exception being raised is reasonable, because i'm not sure of a use case where one would need to add handlers dynamically.

And the fact that it was not working, means that PTB users are also just discovering these use cases.

Maybe bot builders (?) where users of the bot add custom command/messages handers base on their input, also then how can the callback part be dynamic.

But that's for users to answer. We can have it.

Just one thought considering how this should work, for now if one dynamically adds a handler in a different group than the one currently selected, it will not be considered for the same update currently being processed, as the list of handlers was copied long before.

would that be a normal/abnormal behavior?

@Bibo-Joshi
Copy link
MemberAuthor

LGTM

At first I thought the exception being raised is reasonable, because i'm not sure of a use case where one would need to add handlers dynamically.

And the fact that it was not working, means that PTB users are also just discovering these use cases.

Maybe bot builders (?) where users of the bot add custom command/messages handers base on their input, also then how can the callback part be dynamic.

But that's for users to answer. We can have it.

At least I'd say since we're offering add/remove_handler at runtime, it should not throw an exception, independently on how exactly that's achieved :D

Just one thought considering how this should work, for now if one dynamically adds a handler in a different group than the one currently selected, it will not be considered for the same update currently being processed, as the list of handlers was copied long before.

would that be a normal/abnormal behavior?

Yes, I'd say that's expected. I think there are a few different options one could choosev

  • No adding/removing handlers at runtime -> would be breaking
  • Apply a lock around the handlers -> would maybe be an option if we starting from scratch. The lock would be an asyncio.Lock making add/remove_handler coroutine functions. That would be a breaking change. Also it's unclear to me if that would be a desirable interface at all
  • Simply ensure safe looping as here. I think it's reasonable to argue that once an update has started being processed, changes to the logic of how updates are processed should not affect it. And as you've pointed out above, this appwars to be an edge case. I'll still add some hints in the docs

Also I've realized that we need the same logic in

https://github.com/python-telegram-bot/python-telegram-bot/blob/master/src%2Ftelegram%2Fext%2F_application.py#L1261

where we loop over the handler list within the group ..

* cover also process-error* cover non-critical list-loops* document behavior as implementation detail
@Bibo-JoshiBibo-Joshi changed the titleEnsure Safe Handler Looping inApplication.process_updateEnsure Safe Handler Looping inApplication.process_update/errorMay 26, 2025
@Bibo-Joshi
Copy link
MemberAuthor

Explanation of additional changes:

  • For consistency, I also ensured that changing handlers within a group is discarded. Note that this currently doesnot raise an exception as changing a list while iterating over it is safe. However, we don't actually document how the effect onprocess_update is, so that IMO it's okay to see this as non-breaking.
  • changing error_handlers is now safe similar to the handlers change
  • I've added documentation hints stating that this is the current behavior but should be considered an implementation detail. This is mostly based on a gut feeling that making this documented behavior may restrict us from refactoring theApplication internals in the future. If you think it's too cautios I'm open to removing that hint.

@aelkheir
Copy link
Member

it's reasonable to argue that once an update has started being processed, changes to the logic of how updates are processed should not affect it.

👍

If you think it's too cautios I'm open to removing that hint.

the hint+warning are sensible imo

@Bibo-Joshi
Copy link
MemberAuthor

mh, apparently some test case is in a deadlock now 😵‍💫 will have to investigate. not today, though

@Bibo-Joshi
Copy link
MemberAuthor

hanging tests apparently were already fixed by2373596 🕺

@Bibo-JoshiBibo-Joshi requested a review fromaelkheirMay 28, 2025 20:02
Copy link
Member

@aelkheiraelkheir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Just one minor comment.

hanging tests apparently were already fixed by2373596

that's weird cause checks were hanging for me too in#4750. maybe#4801 fixed it (?) i'll merge and see.

Co-authored-by: Abdelrahman Elkheir <90580077+aelkheir@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@aelkheiraelkheiraelkheir approved these changes

@harshil21harshil21Awaiting requested review from harshil21

Assignees
No one assigned
Labels
🔌 bugpr description: bug
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Error With Adding Handler
2 participants
@Bibo-Joshi@aelkheir

[8]ページ先頭

©2009-2025 Movatter.jp