Skip to content

Comments

fix: handle missing event loop in close() during interpreter shutdown#208

Open
haosenwang1018 wants to merge 1 commit intoHKUDS:mainfrom
haosenwang1018:fix/event-loop-finalize
Open

fix: handle missing event loop in close() during interpreter shutdown#208
haosenwang1018 wants to merge 1 commit intoHKUDS:mainfrom
haosenwang1018:fix/event-loop-finalize

Conversation

@haosenwang1018
Copy link

Closes #135

When the atexit handler calls close(), the default event loop policy may already be torn down during interpreter shutdown, causing asyncio.run() to raise RuntimeError: There is no current event loop in thread 'MainThread'.

This fix adds a fallback: if asyncio.run() fails, we manually create a new event loop with asyncio.new_event_loop() and run the finalization there. This ensures storages are properly finalized regardless of when the cleanup runs.

@LarFii
Copy link
Collaborator

LarFii commented Feb 24, 2026

Thanks for the fix.

I reviewed the change and found two potential issues:

  1. Blocking: RuntimeError handling is too broad in the fallback path
    In raganything/raganything.py, the new except RuntimeError: around asyncio.run(self.finalize_storages()) may also catch RuntimeError raised inside finalize_storages() itself (not only event-loop-policy teardown errors).
    That can:

    • mask the original failure reason,
    • trigger a second finalize attempt unexpectedly,
    • make debugging harder.
  2. Non-blocking: manual loop should be set as current loop during fallback
    In the new_event_loop() branch, it is safer to call asyncio.set_event_loop(loop) before run_until_complete(...), then clear it with asyncio.set_event_loop(None) in finally.
    Some downstream code may still call asyncio.get_event_loop() during finalize; without setting the current loop, fallback can still fail in those cases.

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.

[Bug]: Warning: Failed to finalize RAGAnything storages: There is no current event loop in thread 'MainThread'.

2 participants