Skip to content

[BUG] Commands hang #455

Closed
Closed

Description

@trystan2k

Bug Description

Hi !

Previous I had installed basic-memory 0.14.3, via homebrew and it was working perfectly. In fact, I still have this exactly same version installed in my work Mac and still works.
Then today I run brew upgrade basic-memory and during the install it showed this error:

==> Fetching downloads for: basic-memory
✔︎ Formula basic-memory (0.16.2)                                                                                                                               [Verified      1.1MB/  1.1MB]
==> Installing basic-memory from basicmachines-co/basic-memory
==> uv tool install basic-memory==0.16.2 --no-cache
Error: Failed changing dylib ID of /opt/homebrew/Cellar/basic-memory/0.16.2/libexec/python/cpython-3.14.2-macos-aarch64-none/lib/itcl4.2.4/libitcl4.2.4.dylib
  from libitcl4.2.4.dylib
    to /opt/homebrew/opt/basic-memory/libexec/python/cpython-3.14.2-macos-aarch64-none/lib/itcl4.2.4/libitcl4.2.4.dylib
Error: Failed to fix install linkage
Updated load commands do not fit in the header of /opt/homebrew/Cellar/basic-memory/0.16.2/libexec/python/cpython-3.14.2-macos-aarch64-none/lib/itcl4.2.4/libitcl4.2.4.dylib. /opt/homebrew/Cellar/basic-memory/0.16.2/libexec/python/cpython-3.14.2-macos-aarch64-none/lib/itcl4.2.4/libitcl4.2.4.dylib needs to be relinked, possibly with -headerpad or -headerpad_max_install_names
The formula built, but you may encounter issues using it or linking other
formulae against it.
==> Caveats
Basic Memory has been installed as a command-line tool.

To get started:
  basic-memory --help

For use with Claude Desktop, add to your claude_desktop_config.json:
  https://memory.basicmachines.co/integrations/claude-desktop
==> Summary
🍺  /opt/homebrew/Cellar/basic-memory/0.16.2: 14,114 files, 176.8MB, built in 11 seconds
==> Running `brew cleanup basic-memory`...
Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`.
Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`).
Execution time: 14.62s

After that, most of the commands I use (like write-note, read-note, even the --help ones), show the correct output, write the notes, but the process never finish, I need to Ctrl+C to finish it, then it show this error

^CException ignored while joining a thread in _thread._shutdown():
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/basic-memory/0.16.2/libexec/python/cpython-3.14.2-macos-aarch64-none/lib/python3.14/threading.py", line 1583, in _shutdown
    _thread_shutdown()
KeyboardInterrupt:

No matter how long I wait, it hangs forever.

I tried to use Claude Code to help me to find the problem and it tried a lot of things, like install using uv tool, downgrading back to 0.14.3, reset the database, delete the database, installed forcing python 3.13, change some dependency versions.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Install version 0.16.2, 0.16.1, 0.16.0, 0.15.0, 0.14.3
  2. Run command bm tool write-note --help and others. bm --help works without hang, for example
  3. Use tool/feature write-note, read-note
  4. See error

Expected Behavior

It should not hang and exit successfully

Actual Behavior

The output or action is performed but the control is never returned to the terminal, I need to Ctrl+C it. The problem is that when using inside Claude Code, it hangs forever stopping the flow.

Environment

basic-memory version: 0.14.3 (also affects 0.16.2)

  • Installation method: uv tool install basic-memory==0.14.3
  • Python version: 3.13.7
  • OS: macOS 26.1 (Sequoia) Build 25B78
  • Architecture: ARM64 (Apple Silicon)
  • pydantic version: 2.11.7
  • fastmcp version: 2.10.2

Additional Context

Possible Solution

Activity

  1. phernandez commented on Dec 16, 2025

    @phernandez
    Member

    hi @trystan2k when you say the commands hang, are you using the cli commands? or is the mcp hanging. If you can share any logs, that may be helpful. They will be in

    ~/.basic-memory/basic-memory-*.log

  2. phernandez commented on Dec 16, 2025

    @phernandez
    Member

    @claude can you investigate the issue the user is reporting?

  3. claude commented on Dec 16, 2025

    @claude
    Contributor

    Claude finished @phernandez's task in 5m 16s —— View job • claude/issue-455-20251216-2004 • Create PR ➔


    Investigation Complete ✅

    Root Cause: Missing await on task cancellation in FastAPI lifespan shutdown (src/basic_memory/api/app.py:69)

    Impact: CLI commands hang indefinitely when trying to exit after completing their work


    The Problem

    When CLI commands like write-note and read-note execute:

    1. They use get_client() context manager with ASGITransport
    2. ASGITransport triggers FastAPI's lifespan context manager on first request
    3. Lifespan startup creates background sync task with infinite loop (WatchService)
    4. When CLI command completes, AsyncClient closes → triggers lifespan shutdown
    5. Bug: Lifespan calls task.cancel() but doesn't await the task
    6. Context manager tries to exit but task is still running
    7. Command hangs indefinitely waiting for resources to clean up

    The Fix

    Changed the lifespan shutdown to properly await task cancellation:

    if app.state.sync_task:
        logger.info("Stopping sync...")
        app.state.sync_task.cancel()
        try:
            await app.state.sync_task  # ✅ Wait for cancellation
        except asyncio.CancelledError:
            logger.info("Sync task cancelled successfully")

    This follows the standard asyncio pattern for graceful task cancellation as documented in Python's asyncio documentation.

    Why This Started in v0.15.1

    SPEC-16 (#344) introduced proper async client context manager pattern. This made resource cleanup more correct, which exposed the pre-existing bug in the lifespan shutdown code that wasn't properly awaiting task cancellation.

    Changes Made

    • src/basic_memory/api/app.py:
      • Added await app.state.sync_task after cancellation (line 71-73)
      • Added explicit app.state.sync_task = None when sync is disabled (line 62)

    Testing Notes

    The fix ensures that when the FastAPI lifespan context manager exits:

    1. The background sync task is cancelled
    2. We wait for the cancellation to complete
    3. CancelledError is caught and logged
    4. Resources are properly cleaned up
    5. Command returns control to the terminal

    Branch |

  4. changed the title [-][BUG][/-] [+][BUG] Commands hang[/+] on Dec 16, 2025
  5. added a commit that references this issue on Dec 16, 2025
    8ea5dde
  6. phernandez commented on Dec 16, 2025

    @phernandez
    Member

    Thanks @trystan2k I'll see if I can get a bug fix release out really soon.
    PR - #456

  7. trystan2k commented on Dec 16, 2025

    @trystan2k
    Author

    basic-memory-api.log

    Thanks @trystan2k I'll see if I can get a bug fix release out really soon. PR - #456

    Just to confirm, I am getting this when running as CLI, not MCP (didn't test with MCP).

    I attached the only log file that is in the folder you mentioned, in case it still helps.

    Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions