Adds scrapegraphai/utils/event_loop.py with one helper, run_coroutine_sync: it uses asyncio.run() when no loop is running, and otherwise executes the coroutine on its own loop in a worker thread, since both asyncio.run() and run_until_complete() raise inside a running loop. The four crash sites now use it: ChromiumLoader.lazy_load, browser_base_fetch, GraphIteratorNode.execute and GenerateAnswerFromImageNode.execute. The two nodes had the check inverted, calling run_until_complete precisely when the loop was running. The worker thread follows the same idea as AbstractGraph.run_safe_async, which already offloads the blocking call to an executor. Plain scripts are unchanged: with no running loop the path is still a direct asyncio.run() on the calling thread.
Verified on Windows with Python 3.12: a real Playwright fetch through ChromiumLoader.load() from inside asyncio.run(...) now returns the page instead of raising, same for GraphIteratorNode and GenerateAnswerFromImageNode (the latter also no longer leaks a cancelled-task traceback), exceptions raised inside coroutines reach the caller unchanged, and the nine test files CI runs pass before and after (66 passed both times). Note on #1087: it refactors the scraping_fn selection a few lines above this change inside lazy_load; if it lands first I will rebase, the overlap is trivial. Happy to add a mocked unit test for the helper and register it in test-suite.yml if you want one.
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
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.
Fixes #1116.
Adds scrapegraphai/utils/event_loop.py with one helper, run_coroutine_sync: it uses asyncio.run() when no loop is running, and otherwise executes the coroutine on its own loop in a worker thread, since both asyncio.run() and run_until_complete() raise inside a running loop. The four crash sites now use it: ChromiumLoader.lazy_load, browser_base_fetch, GraphIteratorNode.execute and GenerateAnswerFromImageNode.execute. The two nodes had the check inverted, calling run_until_complete precisely when the loop was running. The worker thread follows the same idea as AbstractGraph.run_safe_async, which already offloads the blocking call to an executor. Plain scripts are unchanged: with no running loop the path is still a direct asyncio.run() on the calling thread.
Verified on Windows with Python 3.12: a real Playwright fetch through ChromiumLoader.load() from inside asyncio.run(...) now returns the page instead of raising, same for GraphIteratorNode and GenerateAnswerFromImageNode (the latter also no longer leaks a cancelled-task traceback), exceptions raised inside coroutines reach the caller unchanged, and the nine test files CI runs pass before and after (66 passed both times). Note on #1087: it refactors the scraping_fn selection a few lines above this change inside lazy_load; if it lands first I will rebase, the overlap is trivial. Happy to add a mocked unit test for the helper and register it in test-suite.yml if you want one.