Skip to content

Clarify session and client usage across threads in the multithreading docs - #4870

Open
Alan4506 wants to merge 1 commit into
boto:developfrom
Alan4506:docs-multithreading-sessions
Open

Alan4506 wants to merge 1 commit into
boto:developfrom
Alan4506:docs-multithreading-sessions

Conversation

@Alan4506

@Alan4506 Alan4506 commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Issue #, if available:
boto/botocore#2898

Description of changes:

Updates the multithreading sections of the clients and session guide pages to:

  • Describe the two safe patterns: create one session outside the threads and pass its clients in (recommended), or create both the session and the client inside each thread.
  • Explain that a new session rereads a service's model files the first time it creates a client for that service.
  • Use session.client() instead of session.resource() in the session page example.

Testing:

I ran the following commands to build the docs to validate the changes render correctly:

# Install Boto3 and dependencies
uv pip install -e .
uv pip install -r requirements-docs.txt

# Build the docs (Note: I modified `boto3/docs/__init__.py` so I only build one service to save time)
cd docs
make clean html

# Host the docs locally and verify updates
cd build/html
python -m http.server

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@Alan4506
Alan4506 requested a review from a team as a code owner November 3, 2026 02:20
The ``Session`` that creates a client is not thread safe, so what matters is
where you call ``session.client()``. Once a client has been created it does not
call back into its session and can be used from any thread. There are two safe
patterns:

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.

What about lazy initialization behind a lock? That seems like another safe pattern and is the main use case from the linked issue

Comment on lines +70 to +73
``Session`` objects are not thread safe and should not be shared across
threads or processes. Clients created from a session are thread safe (with the
caveats in :ref:`guide_clients_multithreading`), and once a client has been
created it does not call back into its session. That gives you two safe

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.

I don’t think this is always true. clients can still use the session’s loader for paginators/waiters and some credential providers may create clients later during refresh. Could we avoid the “does not call back” guarantee and just say the session doesn’t need to be used directly after client creation?

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.

2 participants