Skip to content

Conversation

@Yuki-Imajuku
Copy link
Collaborator

@Yuki-Imajuku Yuki-Imajuku commented Dec 11, 2025

Resolve the below error when Google GenAI is used:

An unexpected error occurred during model generation: <asyncio.locks.Event object at 0x7e9258bb5040 [unset]> is bound to a different event loop

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a shared async event loop implementation to handle async-only providers (like Google GenAI) and refactors the safe_generation function to use it instead of the synchronous run_sync method. The implementation provides a thread-safe singleton pattern for managing a background event loop that can be accessed from multiple threads.

Key changes:

  • Adds SharedAsyncLoop class that manages a dedicated background thread with an asyncio event loop
  • Implements singleton pattern with double-checked locking for thread-safe access
  • Replaces agent.run_sync() with shared_async_loop().run(agent.run()) in the safe_generation function

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/ale_bench_eval/shared_async_loop.py New file implementing SharedAsyncLoop class with background event loop management, cleanup handlers, and thread-safe singleton access
src/ale_bench_eval/safe_generation.py Updated to use the shared async loop instead of run_sync for executing agent operations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

self._loop.run_forever()

async def _drain_pending(self) -> None:
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

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

The asyncio.all_tasks() call is missing the loop parameter. When called from a different thread context, asyncio.all_tasks() without arguments returns tasks from the current thread's event loop (which may not exist), not from self._loop. This should be asyncio.all_tasks(self._loop) to ensure it retrieves tasks from the correct event loop that is being shut down.

Suggested change
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
tasks = [t for t in asyncio.all_tasks(self._loop) if t is not asyncio.current_task(self._loop)]

Copilot uses AI. Check for mistakes.
self._loop = asyncio.new_event_loop()
self._thread = threading.Thread(target=self._run_loop, daemon=True)
self._thread.start()
atexit.register(self.shutdown)
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

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

The atexit registration in the constructor can lead to resource leaks if multiple SharedAsyncLoop instances are created and discarded. Each instance registers its own shutdown handler, but when an instance is replaced (e.g., in the shared_async_loop() function), the old instance's atexit handler remains registered but can no longer properly clean up since SHARED_ASYNC_LOOP has been reassigned. Consider unregistering atexit callbacks when instances are replaced, or restructure to only register atexit once for the global instance.

Copilot uses AI. Check for mistakes.
@Yuki-Imajuku
Copy link
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. 👍

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Yuki-Imajuku Yuki-Imajuku requested a review from Copilot December 11, 2025 05:08
@Yuki-Imajuku
Copy link
Collaborator Author

@codex review carefully and report major or minor issues.

@chatgpt-codex-connector
Copy link

Summary

Testing

  • Not run (not requested).

View task →

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Yuki-Imajuku Yuki-Imajuku merged commit 1f9e174 into main Dec 11, 2025
6 checks passed
@Yuki-Imajuku Yuki-Imajuku deleted the bugfix/add-shared-async-loop branch December 11, 2025 21:18
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