Skip to content

refactor(flaresolverr): replace module-global state with a configured client - #167

Merged
Rushaway merged 2 commits into
masterfrom
refactor/159-flaresolverr-client
Sep 6, 2026
Merged

refactor(flaresolverr): replace module-global state with a configured client#167
Rushaway merged 2 commits into
masterfrom
refactor/159-flaresolverr-client

Conversation

@Rushaway

@Rushaway Rushaway commented Sep 5, 2026

Copy link
Copy Markdown
Member

Summary

  • The bypass URL was a module-level global reassigned via set_flaresolverr_url() on every Init(), with a module-level session next to it — configuration living in mutable global state rather than owned by anything.
  • FlareSolverr is now a small class holding its own URL and session. Torchlight builds one from config (falling back to the previous http://127.0.0.1:8191/v1 default when there's no FlareSolverr section), and FFmpegAudioPlayer reaches it via self.torchlight.flaresolverr.

A leak this surfaced

Init() re-runs on every reconnect (OnDisconnectInit()). That didn't matter while the session was global — it was reused across re-inits. Now that each Torchlight owns one, a naive port would leak an aiohttp session per reconnect. So Init() now closes the outgoing instance's session before replacing it, and __del__ closes the current one (guarded with getattr, since __del__ can run before/after attributes exist).

Closes #159

Test plan

Against a live local stub of the FlareSolverr API:

  • URL derived from config → http://10.0.0.5:9999/v1.
  • No FlareSolverr config section → falls back to the previous default, unchanged.
  • get_cf_session returns the parsed cookies/user-agent and reuses the same session across calls (the pooling win from fix(flaresolverr): reuses a pooled session instead of opening one per call #144 is preserved).
  • close() closes the session and clears the reference.
  • Two instances hold independent URLs — no shared global state anymore.
  • No dangling references to the removed set_flaresolverr_url/close_cf_session; all modules compile.

🤖 Generated with Claude Code

… client

The bypass URL was a module-level global reassigned via
set_flaresolverr_url() on every Init(), with a module-level session
alongside it, so the configuration lived in mutable global state rather
than being owned by anything.

FlareSolverr is now a small class holding its own URL and session.
Torchlight builds one from config (falling back to the previous default
when no FlareSolverr section is present) and FFmpegAudioPlayer reaches it
through self.torchlight.

Init() runs again on every reconnect, which previously did not matter
because the session was global; now that each Torchlight owns one, Init()
closes the outgoing instance's session before replacing it so reconnects
do not leak sessions.

Bumps VERSION to 1.8.20.

Closes #159

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Dolly132 <109222243+Dolly132@users.noreply.github.com>
@Rushaway
Rushaway requested a review from Dolly132 September 5, 2026 13:22

@Rushaway Rushaway left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: the non-obvious part is that moving the session off the module global introduces a per-reconnect leak unless Init() closes the outgoing one -- Init() runs on every OnDisconnect, so that would have quietly accumulated aiohttp sessions on a flappy SMAPI link. Handled it explicitly and verified session reuse still works so #144's pooling benefit isn't lost. @Dolly132 this touches the close_cf_session wiring you added in #144, so worth your eyes specifically.

@Dolly132 Dolly132 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, works fine as intended.

@Rushaway
Rushaway merged commit 277f412 into master Sep 6, 2026
2 checks passed
@Rushaway
Rushaway deleted the refactor/159-flaresolverr-client branch September 6, 2026 10:09
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.

FlareSolverr's bypass URL is module-global mutable state rather than configured

2 participants