Skip to content

Commit db1cf35

Browse files
authored
Merge pull request #217 from veithly/fix/gemini
Refactor LLMManager's cleanup process to enhance event loop handling during shutdown.
2 parents 5fec8c1 + 615689c commit db1cf35

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

spoon_ai/llm/manager.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,22 @@ def cleanup_sync():
238238
# Try to get the running event loop (Python 3.10+ safe)
239239
try:
240240
loop = asyncio.get_running_loop()
241-
# If we're inside a running loop, schedule cleanup as a task
241+
# If we're inside a running loop, schedule cleanup as a task (best-effort).
242+
# We intentionally do NOT block here.
242243
asyncio.create_task(self.cleanup())
243244
except RuntimeError:
244-
# No running loop, create a new one for cleanup
245-
try:
246-
loop = asyncio.new_event_loop()
247-
asyncio.set_event_loop(loop)
248-
loop.run_until_complete(self.cleanup())
249-
loop.close()
250-
except Exception as e:
251-
logger.debug(f"Cleanup skipped: {e}")
245+
# No running loop (common during interpreter shutdown).
246+
#
247+
# IMPORTANT:
248+
# Do not create a new event loop here. Some third-party SDKs
249+
# (notably google-genai) schedule async cleanup tasks in
250+
# `__del__` when a loop is running. Spinning up a loop solely
251+
# for atexit cleanup can therefore cause noisy shutdown warnings:
252+
# "Task was destroyed but it is pending!" / "aclose was never awaited".
253+
#
254+
# Users who need deterministic cleanup should call
255+
# `await get_llm_manager().cleanup()` explicitly.
256+
return
252257
except Exception as e:
253258
# Silently skip cleanup on shutdown errors
254259
logger.debug(f"Cleanup error (safe to ignore at shutdown): {e}")

0 commit comments

Comments
 (0)