fix(tokens): claim redis socket tokens atomically

Draft
AN
2mo ago
+82-35

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Description

closes #6879

RedisTokenManager.link_token_to_sid previously checked whether a token existed before storing its socket record in a separate Redis command. Concurrent workers could both observe an absent token and then associate it with different sockets, allowing two connections to share the same client state.

This change claims each token with an atomic Redis SET using NX and the existing expiration. A worker that loses the claim retries with a new UUID (capped at 3 attempts), while Redis errors continue to use the local token-manager fallback.

Greptile feedback is addressed in the follow-up commit: retry cap + cleaned concurrency test that asserts the NX-wins-first-caller contract.

Testing

  • uv run pytest tests/units/utils/test_token_manager.py -q (29 passed, 4 skipped)
  • uv run ruff check .
  • uv run ruff format . --check

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?