Skip to content

fix(gateway): thread-safe PairingStore with atomic writes#5656

Merged
teknium1 merged 1 commit into
NousResearch:mainfrom
CharlieKerfoot:fix/pairing-store-thread-safety
Apr 6, 2026
Merged

fix(gateway): thread-safe PairingStore with atomic writes#5656
teknium1 merged 1 commit into
NousResearch:mainfrom
CharlieKerfoot:fix/pairing-store-thread-safety

Conversation

@CharlieKerfoot

Copy link
Copy Markdown
Contributor

Problem

The gateway runs multiple platform adapters (Telegram, Discord, Slack, etc.)
concurrently in threads sharing a single PairingStore instance. Without locking,
concurrent pairing requests raced on the same read-modify-write cycle, which could:

  • Drop an approved user from the approved list
  • Lose a pending pairing code
  • Allow lockout bypass via a race on _rate_limits.json

_secure_write() also used path.write_text() (non-atomic), so a crash mid-write
could leave a truncated JSON file.

Fix

  • _secure_write() replaced with tempfile.mkstemp + os.fsync + os.replace
    readers always see a complete file, never a partial write
  • PairingStore gains self._lock (threading.RLock) — all composite
    read-modify-write methods (generate_code, approve_code, revoke,
    clear_pending) hold it for their full duration

Testing

  1. Start the gateway with two or more platform adapters configured
  2. Trigger simultaneous pairing requests from different platforms
  3. Verify both codes appear in hermes pair list and neither is lost

@CharlieKerfoot CharlieKerfoot force-pushed the fix/pairing-store-thread-safety branch from 4343bd7 to 9a6eff7 Compare April 6, 2026 23:22
@teknium1 teknium1 merged commit e9b5864 into NousResearch:main Apr 6, 2026
5 of 8 checks passed
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.

2 participants