fix: Token refresh race condition causing EGW00133 error #42

Closed
opened 2026-02-05 00:01:14 +09:00 by agentson · 0 comments
Collaborator

Priority: HIGH

Problem

"접근토큰 발급 잠시 후 다시 시도하세요(1분당 1회)" (EGW00133)
Token refresh failed (403)

여러 요청이 동시에 토큰 만료를 감지하고 갱신을 시도하면서 1분당 1회 제한에 걸림

Root Cause

src/broker/kis_api.py:_ensure_token()에 동시성 제어가 없어서 여러 코루틴이 동시에 토큰 갱신 API 호출

Solution

class KISBroker:
    def __init__(self, ...):
        self._token_lock = asyncio.Lock()
    
    async def _ensure_token(self) -> str:
        async with self._token_lock:
            # Re-check after acquiring lock
            now = asyncio.get_event_loop().time()
            if self._access_token and now < self._token_expires_at:
                return self._access_token
            # ... refresh token

Files

  • src/broker/kis_api.py: _ensure_token() method

Tests

  • Add concurrent token refresh test
  • Verify only one API call made when multiple coroutines request token
## Priority: HIGH ## Problem ``` "접근토큰 발급 잠시 후 다시 시도하세요(1분당 1회)" (EGW00133) Token refresh failed (403) ``` 여러 요청이 동시에 토큰 만료를 감지하고 갱신을 시도하면서 1분당 1회 제한에 걸림 ## Root Cause `src/broker/kis_api.py:_ensure_token()`에 동시성 제어가 없어서 여러 코루틴이 동시에 토큰 갱신 API 호출 ## Solution ```python class KISBroker: def __init__(self, ...): self._token_lock = asyncio.Lock() async def _ensure_token(self) -> str: async with self._token_lock: # Re-check after acquiring lock now = asyncio.get_event_loop().time() if self._access_token and now < self._token_expires_at: return self._access_token # ... refresh token ``` ## Files - `src/broker/kis_api.py`: `_ensure_token()` method ## Tests - Add concurrent token refresh test - Verify only one API call made when multiple coroutines request token
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#42