matrix-nio icon indicating copy to clipboard operation
matrix-nio copied to clipboard

Callbacks receive events multiple times

Open jaywink opened this issue 3 years ago • 2 comments

I have a bot which catches invite events, joins a room and then sends a welcome message. The callback registered for RoomMemberEvent receives consistently the same event twice.

Here is a log from the bot showing the event $d7zxNDepZ2Vwh3Exc0vIemnuul37ml8_1xXAbbZxgeE is received twice. I added a set to track events in callbacks to ensure duplicates are filtered out.

Code for registering the callbacks here

Matrix-nio 0.16.0 Python 3.8 Bot running in Docker

2021-02-13 16:02:41,479 | middleman.callbacks [DEBUG] Callback received event: $chmJewX4DPDSegxOLusky8OomJfxzzZMYdMWFG-YF1c
2021-02-13 16:02:41,479 | middleman.callbacks [DEBUG] Got invite to !8HyXC4anc9bdZOgG:jasonrobinson.me from @jaywink:jasonrobinson.me.
2021-02-13 16:02:41,615 | middleman.callbacks [INFO] Joined !8HyXC4anc9bdZOgG:jasonrobinson.me
2021-02-13 16:02:41,712 | middleman.callbacks [DEBUG] Callback received event: $d7zxNDepZ2Vwh3Exc0vIemnuul37ml8_1xXAbbZxgeE
2021-02-13 16:02:41,713 | middleman.callbacks [DEBUG] Received a room member event for Jason Robinson 🐍🍻 | @middlemanbot:basshero.org: join
2021-02-13 16:02:41,713 | middleman.callbacks [INFO] Sending welcome message to room !8HyXC4anc9bdZOgG:jasonrobinson.me
2021-02-13 16:02:41,955 | middleman.callbacks [DEBUG] Callback received event: $d7zxNDepZ2Vwh3Exc0vIemnuul37ml8_1xXAbbZxgeE
2021-02-13 16:02:41,955 | middleman.callbacks [DEBUG] Skipping $d7zxNDepZ2Vwh3Exc0vIemnuul37ml8_1xXAbbZxgeE as it's already processed

Am I doing something wrong or do callbacks potentially get called many times for the same event?

jaywink avatar Feb 13 '21 16:02 jaywink

Hi @jaywink, I haven’t had a chance to look over your code yet, but is there a chance you’ve registered the callback twice? You should only see an event once.

phildenhoff avatar Feb 16 '21 22:02 phildenhoff

I see the very same error, playing around with simplematrixbotlib running matrix-nio in version 0.19.0. Print the event callbacks I see

>>> bot.api.async_client.event_callbacks
[ClientCallback(func=<bound method Callbacks.invite_callback of <simplematrixbotlib.callbacks.Callbacks object at 0x7f12c39d3d90>>, filter=<class 'nio.events.invite_events.InviteMemberEvent'>),
 ClientCallback(func=<function log_message at 0x7f12c7b901f0>, filter=<class 'nio.events.room_events.RoomMessageText'>),
 ClientCallback(func=<function echo at 0x7f12c7b90280>, filter=<class 'nio.events.room_events.RoomMessageText'>),
 ClientCallback(func=<function leave at 0x7f12c7b90310>, filter=<class 'nio.events.room_events.RoomMessageText'>),
 ClientCallback(func=<function quit at 0x7f12c7b903a0>, filter=<class 'nio.events.room_events.RoomMessageText'>),
 ClientCallback(func=<function hello_on_join at 0x7f12c7b904c0>, filter=<class 'nio.events.room_events.RoomMemberEvent'>)]

The InvitememberEvent callback on top is just the autojoin callback simplematrixbotlib provides. The RoomMemberEvent callback is called twice with the exact same event ID.

Happy to help debug this, since the solution of caching event IDs oneself is a bit inelegant.

behrmann avatar Mar 02 '22 17:03 behrmann