Skip to content

Use a per-thread requests.Session instead of module-level sessions - #981

Merged
LeMyst merged 1 commit into
masterfrom
improve-sessions
Sep 19, 2026
Merged

LeMyst merged 1 commit into
masterfrom
improve-sessions

Conversation

@LeMyst

@LeMyst LeMyst commented Jun 6, 2026 •

Copy link
Copy Markdown
Owner

Replace the shared module-level requests.Session objects (helpers_session, default_session) in wbi_helpers with a session created lazily per thread.

  • requests.Session isn't thread-safe: sharing one global session across threads (multi-threaded bots) could mix connections, cookies and headers.
  • Connections are still kept alive between calls within a thread (no TCP/TLS handshake per call, unlike creating a new session each call).
  • Used by mediawiki_api_call (when no session/login is given), execute_sparql_query and download_entity_ttl.
  • Backward compatibility: wbi_helpers.default_session and wbi_helpers.helpers_session are still accessible via a module __getattr__, returning the current thread's session with a DeprecationWarning.
  • Tests added: session reused within a thread, distinct across threads, used for anonymous calls, deprecated aliases.

Rebased on master (was conflicting with #983 / #999).

🤖 Generated with Claude Code

@LeMyst
LeMyst force-pushed the improve-sessions branch from d56f96f to bb95e4f Compare July 3, 2026 21:20
Replace the shared module-level helpers_session/default_session with a
lazily created session per thread (requests.Session isn't thread-safe).
Connections are still kept alive between calls within a thread.
wbi_helpers.default_session and helpers_session remain available through
a module __getattr__ that emits a DeprecationWarning.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@LeMyst LeMyst changed the title Avoid module-level requests.Session instances Use a per-thread requests.Session instead of module-level sessions Sep 19, 2026
@LeMyst LeMyst closed this Sep 19, 2026
@LeMyst LeMyst reopened this Sep 19, 2026
@LeMyst
LeMyst merged commit ba901dd into master Sep 19, 2026
28 checks passed
@LeMyst
LeMyst deleted the improve-sessions branch September 19, 2026 15:44
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