Skip to content

fix(sessions): resolve async deadlock in multiplexed session manager#1520

Merged
sinhasubham merged 1 commit intomainfrom
async_deadlock_fix
Mar 25, 2026
Merged

fix(sessions): resolve async deadlock in multiplexed session manager#1520
sinhasubham merged 1 commit intomainfrom
async_deadlock_fix

Conversation

@sinhasubham
Copy link
Contributor

This PR resolves a critical deadlock issue when acquiring or maintaining a multiplexed session asynchronously.
The bug occurs because DatabaseSessionsManager previously used a synchronous threading.Lock around self._get_multiplexed_session() and _maintain_multiplexed_session(). When a thread attempts to await the multiplexed session creation (return await ...) while holding a synchronous thread lock, the entire asyncio event loop becomes blocked for any other coroutine trying to access the lock.

@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: spanner Issues related to the googleapis/python-spanner API. labels Mar 24, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical deadlock that previously occurred in the asynchronous multiplexed session manager. The core problem stemmed from using a synchronous threading.Lock in an asynchronous context, which caused the entire asyncio event loop to block when a coroutine attempted to await while holding the lock. By migrating to an asynchronous CrossSync.Lock, the system can now correctly manage concurrent access to shared resources without freezing the event loop, thereby enhancing the stability and performance of asynchronous operations.

Highlights

  • Deadlock Resolution: Resolved a critical deadlock issue in the asynchronous multiplexed session manager by replacing threading.Lock with CrossSync.Lock. This change allows async with statements to be used with the lock, preventing the asyncio event loop from blocking when awaiting session creation or maintenance.
  • Concurrency Safety: Ensured that concurrent acquisition and maintenance of multiplexed sessions can proceed without blocking, significantly improving the reliability and responsiveness of the Spanner client library in asynchronous contexts.
  • Code Style Refinements: Applied consistent tuple unpacking syntax across various files, simplifying assignments and improving code readability.
  • New Unit Test: Added a dedicated unit test to verify that concurrent calls to _get_multiplexed_session no longer result in a deadlock, confirming the effectiveness of the fix.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves a critical deadlock in the asynchronous multiplexed session manager by correctly switching from a synchronous threading.Lock to an async-aware CrossSync.Lock. The inclusion of a new concurrency test is a great addition that validates the fix. Most other changes are stylistic improvements. I've identified one minor issue involving what appears to be dead code, which should be removed for better code clarity.

@sinhasubham sinhasubham marked this pull request as ready for review March 25, 2026 08:26
@sinhasubham sinhasubham requested review from a team as code owners March 25, 2026 08:26
Copy link
Contributor

@bhatt4982 bhatt4982 left a comment

Choose a reason for hiding this comment

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

LGTM...

@sinhasubham sinhasubham merged commit c37532c into main Mar 25, 2026
23 of 24 checks passed
@sinhasubham sinhasubham deleted the async_deadlock_fix branch March 25, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/python-spanner API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants