Skip to content

handle http server close gracefully in idp#3251

Merged
psasidhar merged 2 commits intomasterfrom
idp-http
Mar 24, 2026
Merged

handle http server close gracefully in idp#3251
psasidhar merged 2 commits intomasterfrom
idp-http

Conversation

@havetisyan
Copy link
Copy Markdown
Collaborator

Description

In the idp http flow, sometimes, the http server doesn't stay alive long enough to serve the /close page. The solution uses an intermediate authCode channel inside registerHandlers:

/oauth2/callback stores the raw query in the internal authCode channel, then redirects to /close -- the auth code is no longer visible in the browser URL bar
/close serves the HTML page, then drains authCode and forwards it to the outer code channel -- this triggers the shutdown goroutine
Since server.Shutdown() is graceful, it waits for the in-flight /close handler to finish writing its response before actually stopping the server

Contribution Checklist:

  • The pull request does not introduce any breaking changes
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

Attach Screenshots (Optional)

Signed-off-by: Henry Avetisyan <hga@yahooinc.com>
@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 critical issue in the IDP HTTP flow where the server could terminate prematurely, preventing the /close page from being successfully served. The solution introduces an intermediate channel to manage the authentication code transfer, ensuring that the /close handler can complete its operation and trigger a graceful server shutdown. This change enhances the robustness and reliability of the IDP authentication process by preventing race conditions related to server closure.

Highlights

  • Graceful HTTP Server Shutdown: Implemented a mechanism to ensure the HTTP server gracefully shuts down, specifically allowing the /close page handler to complete its response before the server stops, resolving a potential race condition.
  • Intermediate authCode Channel: Introduced a new buffered channel, authCode, within registerHandlers to temporarily store the OAuth2 callback query parameters.
  • OAuth2 Callback Handler Update: Modified the /oauth2/callback handler to store the raw query in the authCode channel and then issue an HTTP redirect to the /close endpoint, preventing the auth code from being visible in the browser URL bar.
  • Close Page Handler Enhancement: Updated the /close handler to retrieve the authentication code from the authCode channel, forward it to the main code channel, and then close the main code channel, triggering the server shutdown process.
  • Test Suite Expansion: Added a new test, TestRegisterHandlersCallbackFollowRedirect, to thoroughly validate the end-to-end flow, including the redirect from /oauth2/callback to /close and the subsequent processing of the authentication code.
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
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 refactors the IdP authentication flow to more gracefully handle HTTP server shutdown, resolving a race condition. The approach of using an intermediate channel to delay the shutdown signal until after the final page is served is sound. The changes are accompanied by good test coverage. I have a couple of suggestions to improve the robustness of the implementation and its tests.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Henry Avetisyan <havetisy@yahoo.com>
@psasidhar psasidhar merged commit 17a93cf into master Mar 24, 2026
8 checks passed
@psasidhar psasidhar deleted the idp-http branch March 24, 2026 15:15
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