Skip to content

Fix slow local startup caused by secret manager initialization#6314

Merged
nick-nlb merged 3 commits into
datacommonsorg:masterfrom
nick-nlb:secret-manager-client-fix
May 27, 2026
Merged

Fix slow local startup caused by secret manager initialization#6314
nick-nlb merged 3 commits into
datacommonsorg:masterfrom
nick-nlb:secret-manager-client-fix

Conversation

@nick-nlb
Copy link
Copy Markdown
Contributor

Issue

b/516817315

Description

Following an update to grpcio from 1.76.0 to 1.80.0, we started to see a major lag in Flask server startup when running locally on MacOS.

For reference, the grpcio update was in Website: 6172. It was effected because of a major vulnerability in a library that used grpcio as a dependency.

This change likely altered behavior in terms of default timeouts, retry policies, etc, in such a way that the observed delay began to manifest.

The Fix

There are two parts to this fix. One is to refactor the secret manager client to a singleton. The pattern used here is similar to that found in server/lib/vertex_ai.py (without the threading locking as this only occurs on server start up).

This does solve the problem in itself, but produced warnings because of how Flask works in debug mode. It forks a child worker for actually serving the requests (with the master monitoring the file system for changes, among other things). The gRPC connections are long-lived and are forked along with the child worker, causing a warning in the underlying library that this is not safe.

The second part to the fix is to switch the transport to rest. This fixes the problem even without the refactoring (as well as the above issue with refactoring, because the rest requests are stateless and do not maintain a persistent connection, and so have no fork safety issues.

@gemini-code-assist
Copy link
Copy Markdown
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 significant performance regression in local Flask server startup observed after updating the grpcio library. By transitioning the Secret Manager client to a singleton and switching its transport layer to REST, the changes eliminate persistent connection overhead and resolve fork-related issues, ensuring a smoother development experience.

Highlights

  • Secret Manager Singleton: Refactored the Secret Manager client into a singleton pattern to prevent redundant initialization and improve startup performance.
  • Transport Protocol Switch: Switched the Secret Manager transport from gRPC to REST to resolve fork safety warnings in Flask debug mode and eliminate startup latency.
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 the 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 counterproductive. 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
Copy Markdown
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 introduces a global _secret_client variable in server/__init__.py to cache and reuse the GCP Secret Manager client across function calls, initializing it with the REST transport when first needed. There are no review comments to address, and I have no additional feedback to provide.

@nick-nlb nick-nlb marked this pull request as ready for review May 26, 2026 19:20
@nick-nlb nick-nlb requested a review from juliawu May 26, 2026 19:20
Copy link
Copy Markdown
Contributor

@juliawu juliawu left a comment

Choose a reason for hiding this comment

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

Thank you for the much needed fix!

Comment thread server/__init__.py
@nick-nlb nick-nlb merged commit 664d5e3 into datacommonsorg:master May 27, 2026
17 checks passed
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