crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] QdrantEdgeStorage crashes on Windows during orphaned-shard cleanup (os.kill signal 0 unsupported)

Open josephkamau32 opened this issue 2 months ago • 4 comments

Description

QdrantEdgeStorage._cleanup_orphaned_shards() (lib/crewai/src/crewai/memory/storage/qdrant_edge_storage.py) uses os.kill(pid, 0) — the POSIX idiom for checking whether a process is alive without sending a real signal — and only catches ProcessLookupError (dead process) and PermissionError (alive, inaccessible).

On Windows, sending signal 0 isn't supported the same way, and Python's os.kill raises a generic OSError instead of ProcessLookupError. That isn't caught, so it propagates and crashes QdrantEdgeStorage.init for any Windows user whose storage directory contains a leftover worker- shard from a previous process (an ordinary situation after a crash or force-kill).

Actual behavior: QdrantEdgeStorage initialization crashes with an unhandled OSError (see traceback under "Evidence / logs / error messages" below) instead of silently skipping or cleaning up the orphaned shard.

Steps to Reproduce

Run the existing test on Windows:

``` pytest lib/crewai/tests/memory/test_qdrant_edge_storage.py::test_orphaned_shard_cleanup -v ```

Expected behavior

Orphaned-shard cleanup should detect that the owning process is no longer running (on Windows as well as POSIX) and clean up the shard without raising, matching the behavior already verified by the existing test test_orphaned_shard_cleanup.

Screenshots/Code snippets

Not applicable — see the traceback under "Evidence / logs / error messages" below.

Operating System

Windows 11

Python Version

3.12

crewAI Version

1.15.22

crewAI Tools Version

1.15.22

Virtual Environment

Venv

Evidence

Reproduced via the existing test suite on Windows:

uv run pytest lib/crewai/tests/memory/test_qdrant_edge_storage.py::test_orphaned_shard_cleanup -v

Traceback:

lib\crewai\tests\memory\test_qdrant_edge_storage.py:284: in test_orphaned_shard_cleanup
    s2 = _make_storage(str(base))
         ^^^^^^^^^^^^^^^^^^^^^^^^
lib\crewai\tests\memory\test_qdrant_edge_storage.py:27: in _make_storage
    return QdrantEdgeStorage(path=path, vector_dim=vector_dim)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib\crewai\src\crewai\memory\storage\qdrant_edge_storage.py:143: in __init__
    self._cleanup_orphaned_shards()
lib\crewai\src\crewai\memory\storage\qdrant_edge_storage.py:822: in _cleanup_orphaned_shards
    os.kill(pid, 0)
E   OSError: [WinError 11] An attempt was made to load a program with an incorrect format

The test simulates an orphaned shard using a fake, guaranteed-dead PID, and expects _cleanup_orphaned_shards to detect and clean it up. On Windows, os.kill(pid, 0) raises a generic OSError rather than ProcessLookupError, so it's uncaught and crashes storage initialization.

Possible Solution

Yes — os.kill(pid, 0) needs a cross-platform liveness check. On Windows this can be done via ctypes.OpenProcess/CloseHandle instead of relying on POSIX signal semantics, following the same sys.platform != "win32" pattern already used in lancedb_storage.py in this codebase. I have a fix ready and will open a PR referencing this issue.

Additional context

  • Actual Python version tested: 3.13.6 (not listed in the dropdown above; closest available option, 3.12, was selected instead).
  • This is invisible in CI: .github/workflows/tests.yml runs tests on ubuntu-latest only, with no Windows job in the matrix, so this code path has never been exercised on Windows in CI.
  • Impact: any Windows user running QdrantEdgeStorage will hit this crash on initialization if their storage directory contains a leftover worker- shard directory from a previous process (e.g. after a crash or force-kill) — an ordinary, not-edge-case situation.

josephkamau32 avatar Sep 21 '26 10:09 josephkamau32

I'd like to take this on!

The-SMG avatar Sep 23 '26 16:09 The-SMG

Hi @josephkamau32, the root cause is crisp: os.kill(pid, 0) raises a plain OSError on Windows (not ProcessLookupError), so the orphaned-shard cleanup's narrow except clause lets it propagate and crash __init__. The fix is to broaden the caught exception (or use a cross-platform process.is_running() that handles Windows) so a leftover worker-<pid> shard is skipped or cleaned safely.

this exact fix pattern — find leaked daemons/orphaned resources from a previous run and dispose of them safely — is what I'd reach for; here's a working reference implementation from a drop-in fix library I maintain (published, runnable):

# cline_orphan_daemon_reaper.py
# For #14700: finds leaked cline remote-helper daemons re-parented to PID 1
# (the OOM culprits) and prints safe kill commands.

import os, signal, sys

HELPER_MARK = "cline-remote-helper"

def find_orphans():
    out = []
    for pid in os.listdir("/proc"):
        if not pid.isdigit():
            continue
        try:
            stat = open(f"/proc/{pid}/stat").read().split()
            ppid = int(stat[3])
            name = stat[1].strip("()")
            if HELPER_MARK not in name:
                continue
            status = open(f"/proc/{pid}/status").read()
            rss_kb = int(status.split("VmRSS:")[1].split()[0])
            if ppid == 1:                  # healthy daemons are sidecar children
                out.append((int(pid), rss_kb))
        except (OSError, IndexError, ValueError):
            continue
    return out

if __name__ == "__main__":
    orphans = find_orphans()
    if not orphans:
        print("no orphaned helper daemons found")
        sys.exit(0)
    total_mb = sum(r for _, r in orphans) // 1024
    print(f"{len(orphans)} leaked daemon(s), ~{total_mb} MB held:")
    for pid, rss in orphans:
        print(f"  kill -TERM {pid}   # {rss//1024} MB")
    # dry-run by default; uncomment to actually reap:
    # for pid, _ in orphans: os.kill(pid, signal.SIGTERM)

File: https://github.com/chenhz01/llm_agent_dropin_fixes/blob/main/src/llm_agent_dropin_fixes/state_persist/cline_orphan_daemon_reaper.py

Adapt the find_orphans logic to probe shard PIDs cross-platform (catch OSError too), and skip-or-remove the stale shard instead of raising.

chenhz01 avatar Oct 01 '26 13:10 chenhz01

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 01 '26 12:11 github-actions[bot]

Still interested in getting this merged. Happy to address any feedback or rebase if needed. Thanks!

josephkamau32 avatar Nov 02 '26 14:11 josephkamau32