From 71cdf742e4a537fc979a9da157931bbc090dea40 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Tue, 25 Nov 2025 03:10:12 +0000 Subject: [PATCH] fix: remove loop.close() to prevent executor shutdown in MCP daemon thread The MCP server runs file synchronization in a daemon thread with its own event loop. Previously, the finally block called loop.close() which shut down the ThreadPoolExecutor before background sync tasks completed. This caused background tasks using aiofiles.os.scandir() to fail with 'cannot schedule new futures after shutdown' errors, preventing files from being indexed in the database. Since this is a daemon thread meant to run for the lifetime of the MCP server process, the event loop should never be closed. The OS will clean up resources when the process exits. Fixes #443 Co-authored-by: jope-bm --- src/basic_memory/cli/commands/mcp.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/basic_memory/cli/commands/mcp.py b/src/basic_memory/cli/commands/mcp.py index b477f728..1c452ac3 100644 --- a/src/basic_memory/cli/commands/mcp.py +++ b/src/basic_memory/cli/commands/mcp.py @@ -67,8 +67,9 @@ if not config.cloud_mode_enabled: loop.run_until_complete(initialize_file_sync(app_config)) except Exception as e: logger.error(f"File sync error: {e}", err=True) - finally: - loop.close() + # Note: Do NOT close the loop here! This is a daemon thread that should + # run until process exit. Closing the loop shuts down the ThreadPoolExecutor + # which breaks aiofiles operations in background sync tasks. logger.info(f"Sync changes enabled: {app_config.sync_changes}") if app_config.sync_changes: