Skip to content

fix: prevent handle inheritance in subprocess to resolve stdio deadlock#154

Merged
matt-landers merged 1 commit into
googleanalytics:mainfrom
gitrishiom:fix/windows-stdio-subprocess-deadlock
May 15, 2026
Merged

fix: prevent handle inheritance in subprocess to resolve stdio deadlock#154
matt-landers merged 1 commit into
googleanalytics:mainfrom
gitrishiom:fix/windows-stdio-subprocess-deadlock

Conversation

@gitrishiom
Copy link
Copy Markdown
Contributor

Description

This PR addresses the remaining Windows deadlock issue over stdio transport.

While PR #151 successfully moved client initialization to a background thread to prevent main-thread blocking, the underlying OS-level collision still persists: on Windows, the subprocess.Popen call spawned by google.auth.default() defaults to stdin=None. This causes the child process (e.g., gcloud) to inherit the parent's stdin handle. Because the parent's stdin is actively bound to the ProactorEventLoop (Overlapped I/O) managing the MCP stdio transport, this shared reference corrupts the pipe state, resulting in a hard crash ([WinError 6] The handle is invalid and ValueError: I/O operation on closed pipe).

To resolve this without altering the core initialization architecture or removing gcloud feature parity for Windows users, this fix introduces a prevent_stdio_inheritance context manager. It temporarily patches subprocess.Popen to enforce stdin=subprocess.DEVNULL during the google.auth.default() call. This isolates the standard file handles, preventing destructive inheritance while allowing gcloud to successfully fetch credentials.

Changes

  • Wrapped google.auth.default() in analytics_mcp/tools/client.py with a prevent_stdio_inheritance context manager.
  • Ensures 100% feature parity for developers relying on Application Default Credentials via gcloud.
  • Verified locally on Windows 11 using a stdio integration harness. The server no longer hangs on initialization.
  • Formatted with black and wrapped to the 80-character limit required by nox.

Resolves #134

Copy link
Copy Markdown

@selutions selutions left a comment

Choose a reason for hiding this comment

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

docs/tutorials/48-intent-based-authorization.md

Comment thread analytics_mcp/tools/client.py Outdated
@gitrishiom gitrishiom force-pushed the fix/windows-stdio-subprocess-deadlock branch from 530d307 to 5213b0d Compare May 15, 2026 09:30
@matt-landers matt-landers self-requested a review May 15, 2026 12:38
@matt-landers matt-landers merged commit f1a2f03 into googleanalytics:main May 15, 2026
5 checks passed
@matt-landers
Copy link
Copy Markdown
Member

This will go out with the 0.6.0 release next week.

@gitrishiom
Copy link
Copy Markdown
Contributor Author

This will go out with the 0.6.0 release next week.

Thanks. Good to hear the PR is accepted & merged into main. All the best.

@Gavin-Attard-120feet
Copy link
Copy Markdown

@matt-landers does this also resolve #156 ?

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.

get_account_summaries and other tools hang indefinitely when using user OAuth (ADC) credentials on Python 3.14

5 participants