Skip to content

fix: add httpx.Client cleanup in storage and command parser#12

Merged
devonjones merged 4 commits intomainfrom
fix/httpx-client-cleanup
Jan 16, 2026
Merged

fix: add httpx.Client cleanup in storage and command parser#12
devonjones merged 4 commits intomainfrom
fix/httpx-client-cleanup

Conversation

@devonjones
Copy link
Copy Markdown
Owner

Adds explicit close() methods to properly clean up HTTP clients during graceful shutdown.

Changes:

  • Add PostgresStorage.close() to close httpx.Client
  • Add CommandParser.close() to close httpx.Client
  • Call both close methods in ReflexBot.close() during shutdown
  • Use asyncio.to_thread for non-async close calls
  • Add error handling to continue shutdown on cleanup failures

Benefits:

  • Prevents resource leaks from unclosed HTTP connections
  • Proper cleanup for orchestrated environments (K8s, Docker restarts)
  • Follows best practices for HTTP client lifecycle management

Testing:

  • Type checking passes
  • Will test shutdown behavior after deployment

Resolves cortex-fei3

Add explicit close() methods to PostgresStorage and CommandParser to
properly clean up HTTP clients during graceful shutdown.

Changes:
- Add PostgresStorage.close() to close httpx.Client
- Add CommandParser.close() to close httpx.Client
- Call both close methods in ReflexBot.close() during shutdown
- Use asyncio.to_thread for non-async close calls
- Add error handling to continue shutdown on cleanup failures

Benefits:
- Prevents resource leaks from unclosed connections
- Proper cleanup for orchestrated environments (K8s)
- Follows best practices for HTTP client lifecycle

Resolves cortex-fei3

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @devonjones, 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 enhances the application's stability and resource management by implementing explicit cleanup mechanisms for HTTP clients used within the PostgresStorage and CommandParser services. By ensuring these clients are properly closed during application shutdown, it prevents potential resource leaks and adheres to best practices for HTTP client lifecycle management, which is crucial for applications running in orchestrated environments.

Highlights

  • Resource Cleanup: Introduced explicit close() methods in PostgresStorage and CommandParser to ensure proper shutdown of httpx.Client instances, preventing resource leaks.
  • Graceful Shutdown Integration: Integrated the new close() methods into ReflexBot.close() to be called during the application's graceful shutdown sequence.
  • Asynchronous Execution: Utilized asyncio.to_thread to execute the synchronous close() calls for the storage and command parser components within the asynchronous ReflexBot.close() method.
  • Error Handling: Added robust error handling around the cleanup operations to ensure that the shutdown process continues even if an error occurs during the closing of storage or command parser components.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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

@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 correctly identifies and fixes resource leaks by adding close() methods for httpx.Client instances. My review focuses on a few key areas for improvement. I've identified another resource leak with the PostgreSQL connection not being closed, and I've provided a suggestion to fix it. I also recommend refactoring the new cleanup logic to avoid code duplication. Most importantly, I've raised concerns about using synchronous libraries like httpx.Client in an asynchronous application, which can lead to performance bottlenecks, and strongly recommend migrating to httpx.AsyncClient for non-blocking I/O.

Comment thread src/reflex/services/bot.py Outdated
Comment thread src/reflex/services/commands.py
Comment thread src/reflex/storage/postgres.py Outdated
Comment thread src/reflex/storage/postgres.py Outdated
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@devonjones
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown

@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 addresses potential resource leaks by adding cleanup logic for httpx.Client instances in PostgresStorage and CommandParser. The introduction of the _close_component helper method in ReflexBot is a good design choice, providing a centralized, safe way to close components during shutdown, correctly using asyncio.to_thread for synchronous operations and including robust error handling. My review includes a couple of suggestions to enhance type safety and improve logging consistency, which will contribute to the long-term maintainability of the code. Overall, these changes are a valuable improvement for the application's stability and robustness.

Comment thread src/reflex/services/bot.py Outdated
Comment thread src/reflex/storage/postgres.py Outdated
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@devonjones
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown

@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 important resource management improvements by ensuring httpx.Client instances are properly closed during shutdown. The addition of close() methods to PostgresStorage and CommandParser is a good practice. The new _close_component helper method in ReflexBot is well-designed, correctly using asyncio.to_thread for synchronous cleanup operations and including robust error handling to prevent shutdown failures. Overall, these changes enhance the application's stability and prevent resource leaks. I have one minor suggestion regarding type hinting to improve maintainability.

Comment thread src/reflex/services/bot.py Outdated
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@devonjones devonjones merged commit 79913c5 into main Jan 16, 2026
2 checks passed
@devonjones devonjones deleted the fix/httpx-client-cleanup branch January 16, 2026 20:57
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.

1 participant