Не доверять кэшу сессий после рестарта API

Флаг очистки кэша жил только в памяти процесса. Сценарий: logout при
недоступном Valkey (отзыв записан в PostgreSQL, tombstone нет) -> рестарт
API -> Valkey вернулся. Новый процесс читал устаревшую положительную запись,
и отозванный токен получал 200 на /users/me и 201 на отправку сообщения
до истечения TTL записи (до 60 s).

Теперь процесс стартует с включённым флагом и выполняет FLUSHDB перед
первым использованием кэша. Добавлен регрессионный тест
test_logout_during_outage_then_api_restart, evidence и отчёт обновлены.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-03 10:31:12 +03:00
co-authored by Claude Opus 5.5
parent 8e97518120
commit ed012ba09d
9 changed files with 126 additions and 81 deletions
+30
View File
@@ -274,6 +274,35 @@ def test_valkey_outage(api: Api, mode: str) -> None:
check(api.me(token)[0] == 200, "live session works after recovery")
def test_logout_during_outage_then_api_restart(api: Api) -> None:
print("\n== logout while Valkey hangs, API restart, Valkey returns")
user = api.register()
token = api.token(user["username"])
check(api.me(token)[0] == 200, "token cached before outage")
check(valkey_has_session(token), "positive cache entry exists in Valkey")
_, chat, _, _ = api.create_chat(token, user["id"])
compose("pause", "valkey")
try:
check(api.logout(token)[0] == 204,
"logout committed in PostgreSQL, tombstone not written")
# A new API process knows nothing about the failed invalidation.
compose("restart", "api")
check(api.wait_ready(60), "API restarted while Valkey is unreachable")
finally:
compose("unpause", "valkey")
# The stale positive entry (TTL <= 60 s) is still in Valkey now.
status, _, _, _ = api.me(token)
check(status == 401, f"revoked token -> 401 after API restart (got {status})")
status, _, _, _ = api.call(
"POST", f"/api/v1/chats/{chat.get('id')}/messages",
body={"text": "must be rejected"},
headers={"Authorization": f"Bearer {token}"},
)
check(status == 401, f"revoked token cannot send messages (got {status})")
def test_database_outage(api: Api) -> None:
print("\n== PostgreSQL stop: limited 503, anonymous is never trusted")
user = api.register()
@@ -335,6 +364,7 @@ def main() -> int:
if not args.skip_outages:
test_valkey_outage(api, "stop")
test_valkey_outage(api, "pause")
test_logout_during_outage_then_api_restart(api)
test_database_outage(api)
if failures: