Repository navigation
Remove nest_asyncio - #390
Draft
CodexVeritas wants to merge 2 commits into
Draft
CodexVeritas wants to merge 2 commits into
CodexVeritas wants to merge 2 commits into
Conversation
nest_asyncio was applied globally on import so async code could call sync wrappers that use asyncio.run. Without it: - Sync wrappers use a new run_coroutine_synchronously helper. With no running loop it is asyncio.run; inside a running loop (Jupyter, async callers) it runs the coroutine on a fresh loop in a worker thread, copying context vars so cost managers still apply, and logs a warning. - Internal async code awaits async versions instead of sync wrappers. Adds MetaculusClient.get_all_open_questions_from_tournament_async, get_benchmark_questions_async, and async_batching.run_coroutines_while_removing_and_logging_exceptions_async. - Template bots run tournament mode (seasonal + minibench) in one event loop, since their class-level asyncio.Semaphore can't be reused across loops. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…list test Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Removes the
nest-asynciodependency and its fournest_asyncio.apply()calls, including the one that ran onimport forecasting_tools. The public sync API keeps working.Changes
forecasting_tools.util.event_loops.run_coroutine_synchronously. Every sync wrapper that used to callasyncio.run(...)now calls this.asyncio.run.MonetaryCostManagerandHardLimitManagerlimits still apply.MetaculusClient.get_all_open_questions_from_tournament_async,MetaculusClient.get_benchmark_questions_async, andasync_batching.run_coroutines_while_removing_and_logging_exceptions_async. The sync versions now wrap these.ForecastBot.forecast_on_tournament, question gathering inrun_bots.py,Benchmarker.run_benchmark, the Q4 template and Q3 AskNews bots, the key-factors and niche-list researchers, the research coordinator, and the simulator and base-rate Streamlit pages.async_batching.run_coroutinesno longer usesasyncio.get_event_loop(). Without the patch, that call fails after an earlierasyncio.run, and on Python 3.14 it fails when no loop exists.asyncio.runcovering both the seasonal tournament and MiniBench. Their class-levelasyncio.Semaphorebinds to the first event loop it is contended in. With twoasyncio.runcalls, the second one raisesRuntimeError: ... is bound to a different event loop.uv.lockonly loses the twonest-asynciolines under forecasting-tools, anduv lock --checkpasses.nest-asynciostays in the lock as a dependency offirecrawl-py, but nothing imports it.Behavior changes for library users
asyncio.run()inside Jupyter no longer works. The patch is what made it reentrant. Use top-levelawaitinstead, orpip install nest_asyncioand callnest_asyncio.apply()in the notebook.asyncio.run()more than once while sharing a contended asyncio primitive now fail. Before, everyasyncio.runin a thread reused the same loop. The common case is a copy of an older template-botmain.pywith a class-level_concurrency_limiter = asyncio.Semaphore(...)that runs the seasonal tournament and MiniBench in two separateasyncio.runcalls. The fix is to run both in oneasyncio.run, as the templates in this PR now do._asyncversion.forecast_on_tournamentnow callsget_all_open_questions_from_tournament_async, andBenchmarkernow callsget_benchmark_questions_async.@agent_toolfunctions that callasyncio.run) depend on the openai-agents version. Versions that run sync tools in a worker thread, such as 0.20, see no change. Older versions now go through the worker-thread path instead of relying on the patch.Because of 1 and 2, this probably warrants a minor version bump (0.4.0) so
^0.3pins don't pick it up automatically. The public bot template'smain.pyshould be updated at the same time.Testing
mainwith nest_asyncio stubbed out, 41 unit tests fail (run_bots question gathering, async_batching, the refreshing bucket rate limiter, hard limit managers). All of them pass on this branch.test_event_loops.pycovers:run_coroutinesafter an earlierasyncio.run__main__end to end, and Python 3.13/3.14.🤖 Generated with Claude Code