Skip to content

fix: add idempotency to prevent duplicate messages on network redelivery (#10)#19

Open
rafabd1 wants to merge 1 commit intomainfrom
fix/issue-10-idempotency-duplicate-messages
Open

fix: add idempotency to prevent duplicate messages on network redelivery (#10)#19
rafabd1 wants to merge 1 commit intomainfrom
fix/issue-10-idempotency-duplicate-messages

Conversation

@rafabd1
Copy link
Owner

@rafabd1 rafabd1 commented Mar 14, 2026

Fix: Add idempotency to prevent duplicate messages on network redelivery

Closes #10

Problem

When I2P network conditions cause redelivery of the same message (a documented behavior of I2P's unreliable delivery semantics), the same message appears multiple times in the chat interface. This creates:

  1. User confusion: Users see the same message repeated
  2. Message clutter: Chat history becomes unreadable with duplicates
  3. No detection mechanism: The application has no way to know if a message was already received

This is particularly problematic on unstable I2P connections during router bootstrapping or when tunnels degrade.

Solution

Implement message ID tracking for idempotency:

  1. HashSet tracking:

    • Added received_message_ids: Mutex<HashSet<String>> to AppState
    • Tracks all message IDs that have been successfully processed
    • Uses HashSet for O(1) lookup performance
  2. Duplicate detection in handle_incoming_message:

    • Before processing, check if message ID already exists in the set
    • If duplicate: skip silently (with debug logging)
    • If new: insert into set and process normally
  3. Cleanup on session close:

    • When close_session is called, clear the received_message_ids set
    • Allows the same IDs to be used in new sessions
    • Prevents unbounded growth of the set across sessions

Security considerations

  • Message IDs are UUIDs or similar unique identifiers (not sensitive)
  • Tracking is in-memory only (never persisted to disk)
  • Cleared on session close to prevent memory leaks

Implementation details

  • Uses std::collections::HashSet for efficient duplicate detection
  • Mutex protects concurrent access from receive loop and session close
  • Duplicate messages are silently skipped (logged in debug builds for troubleshooting)
  • Message ID tracking is scoped to the current session lifecycle

…ery (#10)

- Add HashSet to track received message IDs in AppState
- Check for duplicate message IDs before processing in handle_incoming_message
- Silently ignore duplicate messages (log in debug builds)
- Clear message ID tracking when session closes to allow reusing IDs in new sessions
- Prevents duplicate messages from I2P network redelivery from appearing in chat

This fixes issue #10 where I2P network redelivery could cause the same message
to appear multiple times in the chat. Now messages are tracked by ID and
duplicates are automatically filtered out.
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.

No idempotency: Duplicate messages on network redelivery

1 participant