Skip to content

cargo test -p nodespace-nlp-engine deadlocks: parallel integration tests race on shared llama.cpp/Metal backend init #1761

Closed

Description

@malibio

Overview

cargo test -p nodespace-nlp-engine hangs indefinitely. The build completes, then the integration_tests binary sits at 0% CPU forever with no output. It is not a build-lock or compilation problem — the process is alive but making no progress, and cargo buffers all output until completion, so it is indistinguishable from a slow build until you inspect the process tree.

Run the same binary with --test-threads=1 and all 11 tests pass in 13 seconds.

Root cause

packages/nlp-engine/tests/integration_tests.rs contains 11 tests that each construct an embedding service and load a real GGUF onto Metal:

test_service_initialization      test_single_embedding_generation
test_asymmetric_embeddings       test_batch_embedding_generation
test_cache_functionality         test_semantic_similarity
test_blob_roundtrip              test_concurrent_requests
test_device_info                 test_empty_text_handling
test_long_text_handling

Cargo's default harness runs these in parallel across threads. Several threads then race to initialize the shared llama.cpp backend / Metal device simultaneously and deadlock. The backend is process-global (see backend() in packages/nlp-engine/src/chat/mod.rs and get_or_init_backend() in packages/nlp-engine/src/embedding.rs), so concurrent first-time initialization is not safe.

Reproduction

cargo test -p nodespace-nlp-engine --release          # hangs at 0% CPU, no output
ps aux | grep integration_tests                       # alive, 0.0% CPU, no children

# same binary, serial:
target/release/deps/integration_tests-<hash> --test-threads=1
# => test result: ok. 11 passed; 0 failed; finished in 13.07s

Why it has gone unnoticed

The pre-push gate (scripts/test-gate.ts, ADR-047) runs test:all, and bun run test is frontend-only, so nothing in the normal workflow invokes this target directly. It only bites someone running the crate's Rust tests by hand — where it looks like an infinite build rather than a deadlock.

Suggested fixes

  1. Serialize the target. Add a harness/test-threads setting for this test binary, or gate the model-loading tests behind a shared static Mutex so only one initializes the backend at a time.
  2. Make backend init re-entrant-safe. A OnceLock/Once around the global backend that other threads block on cleanly rather than deadlocking would fix the class of problem, not just this target.
  3. Fail fast instead of hanging. A timeout wrapper would turn a silent infinite hang into an actionable failure.

Option 2 is the real fix; 1 is the cheap mitigation.

Acceptance Criteria

  • cargo test -p nodespace-nlp-engine completes without hanging, in parallel or serial
  • All 11 integration tests still pass
  • Concurrent first-time backend initialization is safe (or provably serialized) rather than deadlocking
  • The failure mode for a genuinely stuck model load is a timeout/error, not a silent 0%-CPU hang

Context

Found while verifying #1758 (chat context sizing). Unrelated to that change — confirmed pre-existing, and the hang reproduces independently of the sizing fix.

Activity

  1. added
    bug
    on Jul 28, 2026
  2. self-assigned this
    on Jul 28, 2026
  3. mstomar125 commented on Jul 28, 2026

    @mstomar125
    Contributor

    Picking this up (mstomar125) — will make the global llama.cpp/Metal backend init serialize cleanly (OnceLock/Once) so cargo test -p nodespace-nlp-engine no longer deadlocks under the default parallel harness, with all integration tests still passing.

  4. mstomar125 commented on Jul 28, 2026

    @mstomar125
    Contributor

    Resolved in #1769 (merged). The 11 model-loading integration tests are now serialized with #[serial] so first-time llama.cpp/Metal backend init can't race under the parallel harness, and the backend-init + model-load is bounded by a 120s worker-thread timeout that fails fast instead of hanging at 0% CPU. cargo test -p nodespace-nlp-engine now passes without --test-threads=1; the Mutex drop-at-exit backend design is unchanged.

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

Metadata

Metadata

Assignees

Labels

backendbugrust

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions