fix: 스케줄러 단계별 타임아웃 — KIS 무응답 시 EOD 청산 누락 방지

로그 분석(2026-09-22): 15:03:46 진입 스캔 이후 데이트레이딩 스케줄러가 16:52 재배포까지
1시간 49분간 아무 로그도 남기지 않음 = KIS API 무응답(500/Server disconnected 연속)으로
await가 반환되지 않아 루프 정지 → 15:20 EOD 강제청산 미실행 → 포지션 익일 보유.
- _guard(): 각 단계를 asyncio.wait_for로 감싸 초과 시 취소 후 다음 사이클 진행
- 단계별 상한: MDD 30s / watchlist 120s / ORB·EOD청산 300s / 청산·진입 180s
- MDD 판단 불가 시 보수적으로 신규 진입 차단
- 테스트 3건

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-23 10:18:00 +09:00
parent a659de09e3
commit d8550a2241
2 changed files with 72 additions and 7 deletions

View File

@@ -0,0 +1,42 @@
"""스케줄러 단계 타임아웃 가드.
2026-09-22 15:03~16:52: KIS API 무응답으로 데이트레이딩 스케줄러가 1시간 49분 멈춰
15:20 EOD 강제청산을 통째로 건너뜀. 단계마다 타임아웃을 둬 루프가 계속 돌아야 한다.
"""
import asyncio
import pytest
from app.services.daytrading import scheduler as sch
@pytest.mark.asyncio
async def test_guard_returns_value_on_success():
async def ok():
return "done"
assert await sch._guard(ok(), "step") == "done"
@pytest.mark.asyncio
async def test_guard_times_out_and_cancels_hung_step():
cancelled = asyncio.Event()
async def hang():
try:
await asyncio.sleep(60)
except asyncio.CancelledError:
cancelled.set()
raise
result = await sch._guard(hang(), "행 스텝", timeout=0.05)
assert result is None
await asyncio.sleep(0) # 취소 전파
assert cancelled.is_set(), "타임아웃 시 멈춘 단계는 취소되어야 함"
@pytest.mark.asyncio
async def test_guard_does_not_swallow_other_errors():
async def boom():
raise RuntimeError("API 오류")
with pytest.raises(RuntimeError):
await sch._guard(boom(), "step")