OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

Import deadlock in openviking.server.bootstrap on Windows — __getattr__ lazy loading + recursive import lock

Open asla1208 opened this issue 4 months ago • 1 comments


title: Import deadlock in openviking.server.bootstrap on Windows — getattr lazy loading + recursive import lock labels: bug, windows assignees: ''

Description

import openviking.server.bootstrap (and transitively openviking.service.core) deadlocks on Windows. The root cause is a recursive import lock triggered by the getattr lazy-loading mechanism in openviking/storage/init.py combined with eager top-level imports in openviking/service/core.py.

Environment

  • OS: Windows 11
  • Python: 3.12.5
  • openviking: 0.4.11 (pip installed)
  • Reproducer: python -c "import openviking.server.bootstrap" hangs indefinitely

Root Cause

The deadlock chain is:

  1. openviking.service.core does a top-level rom openviking.storage import VikingDBManager (line ~24)
  2. openviking.storage.getattr("VikingDBManager") is triggered (since VikingDBManager is lazy-loaded via _LAZY_IMPORTS)
  3. getattr calls importlib.import_module("openviking.storage.vikingdb_manager")
  4. ikingdb_manager.py (module-level) does rom openviking.storage.queuefs.embedding_msg import EmbeddingMsg
  5. This triggers another import openviking.storage → Python finds it already partially initialized in sys.modules → tries getattr("queuefs") → importlib.import_module("openviking.storage.queuefs") → import lock contention with step 1

The core issue: openviking/storage/init.py uses getattr + _LAZY_IMPORTS to defer sub-module loading. However, this deferred loading happens during another openviking.service.core import that already holds the import lock for openviking.storage. When the lazily-loaded sub-module tries to import sibling sub-modules of openviking.storage, it attempts to acquire the same import lock, causing a deadlock.

Additional Hanging Modules

Further tracing shows even direct imports of certain sub-modules also deadlock:

  • import openviking.storage.content_write — hangs (imports openviking.storage.queuefs.queue_processor which deadlocks)
  • import openviking.service.fs_service — hangs
  • import openviking.service.debug_service — hangs

Minimal Reproducer

`python

hangs indefinitely:

import openviking.storage.content_write `

Even with sentence_transformers and orch uninstalled (to rule out those known Windows issues), the deadlock persists.

Suggestion

Consider replacing the getattr-based lazy loading with explicit eager imports for the sub-modules that are most commonly imported by service modules. Specifically, adding these to storage/init.py would break the circular import chain:

python from openviking.storage.vikingdb_manager import VikingDBManager, VikingDBManagerProxy from openviking.storage.queuefs import QueueManager, get_queue_manager, init_queue_manager from openviking.storage.viking_fs import VikingFS, get_viking_fs, init_viking_fs

This mirrors the approach used for openviking.storage.errors (which is already eagerly imported in storage/init.py).

Alternatively, a safer lazy-loading mechanism that doesn't rely on importlib.import_module() re-entering the parent package's import lock.

asla1208 avatar Jul 28 '26 05:07 asla1208

Thanks — I checked the current import chain, and the deadlock-relevant structure described in this issue is still present.

In the current tree:

  • openviking.service.core still imports VikingDBManager from openviking.storage at module top level
  • openviking.storage.__init__ still resolves that symbol through __getattr__ + _LAZY_IMPORTS
  • openviking.storage.vikingdb_manager still imports openviking.storage.queuefs.* during module import

So the effective sequence is still:

  1. import openviking.service.core
  2. trigger openviking.storage.__getattr__("VikingDBManager")
  3. import openviking.storage.vikingdb_manager
  4. re-enter sibling imports under openviking.storage while the parent package is only partially initialized

That means the recursive import shape reported here has not been structurally eliminated.

To be precise, I have not re-run a fresh Windows reproduction on current HEAD, so I am not claiming a newly confirmed runtime hang. But based on the current code, this issue should not be considered fixed at the import-structure level.

The cleanest fix would be to break this path entirely, either by importing VikingDBManager directly from openviking.storage.vikingdb_manager in core.py, or by eagerly exporting the commonly used runtime symbols from openviking.storage.__init__ instead of resolving them lazily through __getattr__.

baojun-zhang avatar Jul 29 '26 12:07 baojun-zhang