Fix stale timer after loop close in BoundedWarning and validate BeginRateLimiter.configure

Open
#52 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
fastapi, python
Domain
backend, testing

Research direction

Start with server/orcaster_server/auth/rate_limit.py, especially BoundedWarning.hit and BeginRateLimiter.configure, then read the related cases in server/tests/auth/test_begin_rate_limit.py. Add coverage for warnings whose timer belongs to a closed loop, including KeyedBoundedWarning, and for limiter validation. Done means the pending warning is emitted on the new loop, invalid values raise ValueError, zero per-client rate remains disabled, and the Unreleased changelog is updated.

Written by the indexing model from the issue text.

Description

bug P3 server

Problem

Two small defects in server/orcaster_server/auth/rate_limit.py.

  1. A timer that belongs to a closed event loop is never replaced.
    BoundedWarning.hit schedules the trailing flush only when _flush_handle is None (server/orcaster_server/auth/rate_limit.py:314). If the loop that owns the handle closes before the timer runs, the pending callback is discarded and the handle stays set. No new timer is scheduled, so the trailing count stays pending until a later request arrives after the window. KeyedBoundedWarning reuses BoundedWarning, so it has the same defect.
    A server process normally has one loop for its whole life, so mostly tests, a restarted loop in the same process and shutdown are affected.

  2. BeginRateLimiter.configure does not validate its arguments (server/orcaster_server/auth/rate_limit.py:196). For example BeginRateLimiter(30, 0) builds a limiter with a burst of 0 that rejects every request. AuthConfig already validates these values (server/tests/auth/test_begin_rate_limit.py:830), but direct construction does not.

The BoundedWarning docstring says "at most one per interval seconds" (server/orcaster_server/auth/rate_limit.py:252). The code emits a leading line and a trailing line per window (server/tests/auth/test_begin_rate_limit.py:278), so the docstring should say two.

Steps to reproduce

Closed loop:

  1. On loop A, call hit() twice on a BoundedWarning with a long interval. The first hit emits, the second leaves a pending count and a timer.
  2. Close loop A before the timer runs.
  3. On loop B, call hit() inside the window, then run loop B past the end of the window.
  4. Only the first line is logged and the count is still pending.

Validation: BeginRateLimiter(30, 0).check("client") returns a wait time on the first call.

Proposed change

  • Store the loop together with the handle. In hit, schedule a new timer when the handle is missing, cancelled, or its loop is closed (loop.is_closed()).
  • State in the BoundedWarning docstring that it must be used only from the event-loop thread, and that it emits at most two lines per window (leading and trailing).
  • Validate in configure the same rules as AuthConfig: per-client rate >= 0 and burst >= 1, and the same for the per-network pair. Raise ValueError. Keep per_minute == 0 meaning "level disabled".

Acceptance criteria

  • A test shows that a BoundedWarning whose timer belongs to a closed loop emits its pending count after the window closes in a new loop.
  • The same test covers KeyedBoundedWarning with two successive loops.
  • BeginRateLimiter(30, 0) and BeginRateLimiter(-1, 10) raise ValueError (test), and BeginRateLimiter(0, 1) still disables the per-client level.
  • Existing deployments keep working without config changes: default settings and values accepted by AuthConfig are still accepted.
  • CHANGELOG entry under Unreleased.

Open questions

  • Should the 429 log line carry a truncated or hashed client key? Today the message has no client value (server/orcaster_server/auth/rate_limit.py:259), which also means an operator cannot tell who sends a flood from this log alone. The alternative is to keep the log and document where to find the source (the reverse proxy log).
  • register/begin uses the same limiter as login/begin (server/orcaster_server/auth/router.py:142, server/orcaster_server/auth/router.py:306). A register/begin request that returns 410 (bad invitation) or 400 (invalid display name) creates nothing but still spends a token. Should it be refunded, or only documented? A refund lets a probe run without cost.
Dominant language
Python
Stars
0
Forks
0
Avg merge
1d 1h
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AInvirion/orcaster

All issues in AInvirion/orcaster

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.