Skip to content

Conversation

@madisoncarter1234
Copy link

Summary

Fixes #3789

When a session is explicitly provided to HTTPProvider, it should be used for all requests regardless of which thread makes them. Previously, the session was cached with a thread-specific key (threading.get_ident()), so other threads/greenlets would create their own sessions, defeating the purpose of providing a shared session.

Changes

  • HTTPSessionManager: Added explicit_session parameter. When set, cache_and_return_session() returns it directly, bypassing the thread-based cache.
  • HTTPProvider: Pass the user's session to the manager constructor instead of caching it post-construction.
  • Tests: Added regression tests verifying session sharing across threads.

Impact

This is critical for applications using:

  • gevent with Celery workers (common in production)
  • Threading for concurrent requests

Without this fix, a setup with 5000 greenlets could create up to 5000 separate requests.Session instances, each with its own connection pool, leading to thousands of unclosed RPC connections.

Test Plan

  • Added test_user_provided_session_shared_across_threads - verifies explicit session is used by all threads
  • Added test_no_explicit_session_creates_per_thread_sessions - verifies default behavior unchanged

  Fixes ethereum#3789

  When a session is explicitly provided to HTTPProvider, it should be
  used for ALL requests regardless of which thread makes them. Previously,
  the session was cached with a thread-specific key, so other threads
  would create their own sessions, defeating the purpose.

  This change stores the explicit session in HTTPSessionManager and
  returns it directly, bypassing the thread-based cache entirely.
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.

HTTPProvider ignores provided session when used with gevent/threading - creates new sessions per thread

2 participants