Skip to content

fix(python): 0.2.1 — bounded SSE backlog, debounced persistence, gunicorn --preload examples (#74, #77, #86) - #96

Merged
akosidencio merged 6 commits into
devfrom
fix/v0.2.1-python-sdk
Nov 30, 2026
Merged

akosidencio merged 6 commits into
devfrom
fix/v0.2.1-python-sdk

Conversation

@akosidencio

Copy link
Copy Markdown
Member

Python SDK fixes for 0.2.1: #74, #77, #86.

#74: bounded stream backlog

  • Each event counts its size plus 256 bytes against a per-stream budget until its task (listeners included) has finished. That covers queued and in-flight events.
  • If an event doesn't fit, the reader pauses up to 250 ms while the queue drains. If it still doesn't fit, the stream is dropped and queued events are discarded. The client reconnects with the usual backoff and merges the new replay by revision, so nothing is applied out of order.
  • Each connection is its own StreamRequest, and events from a replaced stream are ignored.
  • New option max_stream_backlog_bytes: default 64 MiB, minimum 1 MiB.
  • An overflow shows in get_health() as a reconnect plus a last_error that names the option. No field was added, so health schema 1 stays aligned with Node.
  • Flood test: 300 × 256 KiB events, a slow listener and a 4 MiB budget. Every peak backlog stayed within the budget, and the client caught up to the final revision.
  • RSS test in a fresh process with a 16 MiB budget: peak growth 18.6–24.8 MiB over 6 runs, against a limit of budget + 32 = 48 MiB. In the unbounded control run the queue reached 106 MiB.

#77: debounced snapshot persistence

  • New option persist_interval_ms, default 500. The first change after a quiet period is written at once, then at most one write per interval, always of the newest flags.
  • The snapshot is taken as a shallow copy under the lock. It is encoded in 256-flag steps (so other threads get the GIL back) and written outside the lock.
  • Snapshots are numbered, and a separate write lock makes sure an older one never overwrites a newer one.
  • close() writes the newest snapshot if it changed. A failed write keeps the in-memory state and is retried. The temp-file + rename write is unchanged.
  • Test results:
    • 1,000 deltas → 1–2 writes (limit 3).
    • 10,000 flags: lock hold p99 189–405 µs, and evaluation latency on another thread p99 23–59 µs (limit 1 ms).
    • The final revision survives close and restart.

#86: Django and Flask examples under gunicorn --preload

  • Django creates the client only in workers (post_worker_init) and closes it in worker_exit. flags.get_client() checks the process id.
  • The gunicorn configs are split into gunicorn_django.conf.py and gunicorn_flask.conf.py, so the documented Flask command boots from examples/.
  • test_examples.py runs both under real gunicorn -w 2 --preload:
    • a flag evaluates in each worker;
    • exactly 2 streams open, none from the master;
    • each worker closes its client on SIGTERM.
  • The CI sdk-python job installs the frameworks and sets CHECKGATE_REQUIRE_EXAMPLES=1, so these tests cannot skip there.

Results (macOS): pytest sdks/python/tests passes 180 on 3.11 and on 3.13, up from 161. The fault-injection suite sdks/conformance/python.mjs passes 27/27. Linux and Windows are covered by CI only.

Maintainer decisions:

  1. A dedicated stream_overflows health field would need a schema change shared with the other SDKs.
  2. Snapshot encoding and writing still run on the sync thread, outside the lock: about 55–125 ms per interval at 10,000 flags. A writer thread would remove that wait.
  3. The example file names and the Django example's API changed (gunicorn.conf.py → gunicorn_django.conf.py, flags.client → flags.get_client()). These are examples, not package API.

Refs #74, #77, #86

Events read from the stream were handed to the sync thread through an
unbounded queue. Each event now counts against a per-stream budget
(max_stream_backlog_bytes, default 64 MiB) from when it is read until its
task has run. An event that does not fit pauses the reader for up to
250 ms; if it still does not fit, the stream and its queued events are
dropped and the client reconnects. The replay merges with the flags
held, and events of a replaced stream are never applied.

The overflow is reported in get_health() through existing schema-1
fields (reconnect_count, last_error kind network).
With storage, every task that changed flags re-encoded the whole flag set
under the client lock and wrote it. Snapshots are now taken at most once
per persist_interval_ms (new option, default 500 ms; the first change
after a quiet period at once), as a shallow copy under the lock, and
encoded (256 flags per step, so other threads get the GIL back) and
written after the lock is released. Writes are serialized and numbered:
an older snapshot never overwrites a newer one. close() flushes a
pending snapshot; a failed write leaves the flags in memory untouched
and is retried on the next change or close().
…icorn configs (#86)

With gunicorn --preload, AppConfig.ready() runs in the master, so the
Django example created its client there and every worker raised
CheckgateForkError. flags.get_client() now creates the client on first
use in the process that uses it; gunicorn_django.conf.py connects it in
post_worker_init and closes it in worker_exit.

The Django settings lived in examples/gunicorn.conf.py, which gunicorn
loads from the working directory without -c, so the documented Flask
command failed to boot from examples/. They are now
gunicorn_django.conf.py, next to gunicorn_flask.conf.py (worker_exit
closes the Flask client).
…oad (#86)

test_examples.py starts each example under gunicorn -w 2 --preload
against the fake server, checks that each worker evaluates a flag (by
the access log's worker pid), that only the two workers open a stream
(none from the master), and that a graceful shutdown closes the client
in each worker. CI installs gunicorn, Django and Flask and sets
CHECKGATE_REQUIRE_EXAMPLES=1, so these tests cannot skip there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant