Skip to content

fix(matrix): prevent bot self-reply loop with user_id mismatch and event dedup#768

Open
voidborne-d wants to merge 1 commit intoRightNow-AI:mainfrom
voidborne-d:fix/matrix-self-message-loop
Open

fix(matrix): prevent bot self-reply loop with user_id mismatch and event dedup#768
voidborne-d wants to merge 1 commit intoRightNow-AI:mainfrom
voidborne-d:fix/matrix-self-message-loop

Conversation

@voidborne-d
Copy link

Problem

Fixes #757 — Matrix bot gets stuck in an infinite reply loop, responding to the same message every ~3 seconds.

Root Cause

Two issues combine to cause this:

  1. User ID mismatch: The sync loop uses self.user_id (config value) to filter out own messages, but Matrix server delegation or casing differences can cause this to not match the sender field in timeline events. The /whoami endpoint returns the canonical user ID, but start() discards this validated value and passes the config value to the spawn instead.

  2. No event dedup: If the sync token has a race condition on reconnect, events can be delivered more than once with no protection against re-processing.

Fix

  1. Use the validated user ID from /whoami (already called in start()) instead of the config self.user_id for the self-message filter in the sync loop.

  2. Add an event ID dedup set (HashSet<String>, capped at 500) as defense-in-depth to skip already-processed events on sync token races or reconnects.

Changes

  • crates/openfang-channels/src/matrix.rs: 26 lines added, 4 removed
    • let user_id = validated_user instead of self.user_id.clone()
    • seen_events HashSet for event dedup before processing
    • Moved event_id extraction earlier for dedup check

…ent dedup

Two fixes for the Matrix bot stuck in infinite reply loop (RightNow-AI#757):

1. Use validated user ID from /whoami instead of config value for
   self-message filtering. Matrix server delegation or casing
   differences can cause the configured user_id to not match the
   sender field in timeline events, so the bot processes its own
   replies and enters an infinite loop.

2. Add event ID dedup set to prevent re-processing the same event
   on sync token races or reconnects. This is a defense-in-depth
   measure that also protects against edge cases where /sync returns
   overlapping event windows.

Fixes RightNow-AI#757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Matrix bot stuck in loop

1 participant