Skip to content

fix: safely close all ClientSessions with ContextVar#605

Merged
nuzant merged 6 commits intomainfrom
fw/close-session
Nov 21, 2025
Merged

fix: safely close all ClientSessions with ContextVar#605
nuzant merged 6 commits intomainfrom
fw/close-session

Conversation

@garrett4wade
Copy link
Collaborator

Description

#589 introduced a bug that ClientSessions are not properly closed when workflow.arun_episode exits. We should use a ContextVar instead of a threading.local() to reuse the ClientSession object.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not
    work as expected)
  • Documentation update
  • Code refactoring (no functional changes)
  • Performance improvement
  • Test coverage improvement

Checklist

  • I have read the Contributing Guide
  • I have run formatting tools (pre-commit or manual)
  • I have run relevant unit tests and they pass
  • I have added tests for new functionality
  • I have updated documentation if needed
  • My branch is up to date with main
  • This PR introduces breaking changes (if yes, fill out details below)
  • If this PR changes documentation, I have built and previewed it locally with
    jb build docs
  • No critical issues raised by AI reviewers (/gemini review)

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @garrett4wade, 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 introduces a critical fix for improper aiohttp.ClientSession closure within asynchronous workflows. By migrating the session management mechanism from threading.local() to contextvars.ContextVar, the changes ensure that each ClientSession is correctly scoped to its specific asynchronous task and is reliably closed upon the task's completion. This enhancement prevents resource leaks and contributes to the overall stability and efficiency of the application.

Highlights

  • Session Management Refactor: Replaced threading.local() with contextvars.ContextVar for managing aiohttp.ClientSession objects, providing more robust and context-aware session handling in asynchronous environments.
  • ClientSession Lifecycle: Implemented explicit creation and closing of ClientSession instances within the scope of workflow.arun_episode using ContextVar, ensuring sessions are properly isolated and reliably closed upon task completion.
  • Bug Fix: Addressed a bug (related to fix: reuse aiohttp.ClientSession in agenerate #589) where ClientSessions were not consistently closed after workflow.arun_episode exits, preventing potential resource leaks and improving application stability.
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
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 addresses a bug concerning the improper closure of ClientSession objects by transitioning from threading.local() to contextvars.ContextVar. This change correctly scopes sessions to asynchronous tasks, ensuring they are properly managed and closed. My review includes a few suggestions to enhance code quality and maintainability, such as correcting a typo, updating a docstring, and improving the encapsulation of session management logic.

garrett4wade and others added 4 commits November 20, 2025 22:01
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@garrett4wade
Copy link
Collaborator Author

/gemini review

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 correctly addresses a bug causing ClientSession leaks by replacing threading.local with contextvars.ContextVar. The introduction of the managed_session async context manager is an excellent pattern for ensuring robust session lifecycle management within asyncio tasks. I've identified a critical resource leak in the fallback logic within agenerate where a locally created session might not be closed upon an exception. I've provided a fix using a try...finally block. Additionally, I've suggested a minor docstring update for better clarity. Overall, this is a strong fix, and with the suggested improvements, it will be even more reliable.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@garrett4wade garrett4wade added the safe-to-test Ready to run unit-tests in a PR. label Nov 20, 2025
Copy link
Collaborator

@nuzant nuzant left a comment

Choose a reason for hiding this comment

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

LGTM.

@nuzant nuzant merged commit 1a51b40 into main Nov 21, 2025
7 checks passed
@nuzant nuzant deleted the fw/close-session branch November 21, 2025 03:25
Bruce-rl-hw pushed a commit to Bruce-rl-hw/AReaL-vllm that referenced this pull request Dec 4, 2025
* safely close all ClientSessions with ContextVar

* update comments

* Update areal/core/remote_inf_engine.py

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* use async context manager to manage client sessions

* .

* Update areal/core/remote_inf_engine.py

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe-to-test Ready to run unit-tests in a PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants