mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
test(python): isolate the background-loop fork scenario (#5142)
## Summary Follow-up to #5111. `test_forked_child_starts_background_loop_lazily` forked the shared pytest process. On CI that process may already own native Tokio/parking_lot state from earlier tests such as `test_authz.py`, so the forked child can crash in a non-Python native thread during `table_names()` for reasons unrelated to this scenario. Run the entire fork scenario in a fresh `python -c` subprocess. The parent still starts `LOOP` via `parent-before`, forks while holding `LOOP._start_lock` (released in `finally`), and the child still verifies that it sees no inherited loop, thread, or background threads before using the real native sync API (`connect("memory://")` / `table_names()`). The child must then gain exactly one live daemon thread, while the parent's original thread stays alive and serves `parent-after`. This is test isolation, not a production fix or a root-cause claim. The original assertions and cleanup are preserved, with a 40s outer timeout for `join(20)`, `terminate()` + `join(5)`, `queue.get(5)`, and diagnostics. ## CI evidence The failure was observed on a docs-only main run (`b4459f35`), so it is not caused by a product change in that run: - [Minimum dependencies job 136441279987](https://github.com/lancedb/lancedb/actions/runs/44780367013/job/136441279987): timed out at ~20s. - [Linux Python 3.13 job 136441280394](https://github.com/lancedb/lancedb/actions/runs/44780367013/job/136441280394): native SIGSEGV in a non-Python native thread during `table_names()` after `connect`, following `test_authz.py` in the same stage. ## Validation - `python/tests/test_background_loop.py`: 4 passed. - Same pytest process, `test_authz.py` followed by `test_background_loop.py`: 83 passed. - `uv run make check`: cargo fmt, ruff format, cargo clippy, and ruff check passed. No production code changes.
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import multiprocessing as mp
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import textwrap
|
||||
import traceback
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -14,7 +12,7 @@ import pytest
|
||||
BACKGROUND_THREAD_NAME = "LanceDBBackgroundEventLoop"
|
||||
|
||||
|
||||
def _run_in_subprocess(code: str) -> subprocess.CompletedProcess:
|
||||
def _run_in_subprocess(code: str, timeout: int = 30) -> subprocess.CompletedProcess:
|
||||
env = os.environ.copy()
|
||||
env["PYTHONPATH"] = os.pathsep.join(path for path in sys.path if path)
|
||||
return subprocess.run(
|
||||
@@ -23,7 +21,7 @@ def _run_in_subprocess(code: str) -> subprocess.CompletedProcess:
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env=env,
|
||||
timeout=30,
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
|
||||
@@ -142,45 +140,6 @@ def test_concurrent_first_use_initializes_one_background_thread():
|
||||
_assert_subprocess_ok(result)
|
||||
|
||||
|
||||
def _fork_child_starts_lazily(queue):
|
||||
def background_threads():
|
||||
import threading
|
||||
|
||||
return [
|
||||
thread
|
||||
for thread in threading.enumerate()
|
||||
if thread.name == BACKGROUND_THREAD_NAME
|
||||
]
|
||||
|
||||
try:
|
||||
from lancedb.background_loop import LOOP
|
||||
|
||||
inherited_thread_is_none = LOOP.thread is None
|
||||
inherited_loop_is_none = LOOP.loop is None
|
||||
background_threads_before = [thread.name for thread in background_threads()]
|
||||
|
||||
import lancedb
|
||||
|
||||
db = lancedb.connect("memory://")
|
||||
tables = db.table_names()
|
||||
|
||||
worker = LOOP.thread
|
||||
queue.put(
|
||||
{
|
||||
"error": None,
|
||||
"inherited_thread_is_none": inherited_thread_is_none,
|
||||
"inherited_loop_is_none": inherited_loop_is_none,
|
||||
"background_threads_before": background_threads_before,
|
||||
"tables": tables,
|
||||
"thread_alive": False if worker is None else worker.is_alive(),
|
||||
"thread_daemon": False if worker is None else worker.daemon,
|
||||
"background_thread_count": len(background_threads()),
|
||||
}
|
||||
)
|
||||
except BaseException:
|
||||
queue.put({"error": traceback.format_exc()})
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.platform != "linux",
|
||||
reason=(
|
||||
@@ -189,48 +148,100 @@ def _fork_child_starts_lazily(queue):
|
||||
),
|
||||
)
|
||||
def test_forked_child_starts_background_loop_lazily():
|
||||
import asyncio
|
||||
# Fork from a fresh interpreter: earlier tests in a shared pytest process
|
||||
# may have left native runtime state that is not part of this scenario.
|
||||
result = _run_in_subprocess(
|
||||
f"""
|
||||
import asyncio
|
||||
import multiprocessing as mp
|
||||
import threading
|
||||
import traceback
|
||||
|
||||
from lancedb.background_loop import LOOP
|
||||
from lancedb.background_loop import LOOP
|
||||
|
||||
assert LOOP.run(asyncio.sleep(0, result="parent-before")) == "parent-before"
|
||||
parent_thread = LOOP.thread
|
||||
assert parent_thread is not None
|
||||
BACKGROUND_THREAD_NAME = {BACKGROUND_THREAD_NAME!r}
|
||||
|
||||
ctx = mp.get_context("fork")
|
||||
queue = ctx.Queue()
|
||||
def background_threads():
|
||||
return [
|
||||
thread
|
||||
for thread in threading.enumerate()
|
||||
if thread.name == BACKGROUND_THREAD_NAME
|
||||
]
|
||||
|
||||
# Simulate forking while another thread held the initialization lock. The
|
||||
# child must replace the inherited lock instead of trying to acquire it.
|
||||
LOOP._start_lock.acquire()
|
||||
try:
|
||||
proc = ctx.Process(target=_fork_child_starts_lazily, args=(queue,))
|
||||
proc.start()
|
||||
finally:
|
||||
LOOP._start_lock.release()
|
||||
def fork_child(queue):
|
||||
try:
|
||||
from lancedb.background_loop import LOOP
|
||||
|
||||
proc.join(timeout=20)
|
||||
if proc.is_alive():
|
||||
proc.terminate()
|
||||
proc.join(timeout=5)
|
||||
inherited_thread_is_none = LOOP.thread is None
|
||||
inherited_loop_is_none = LOOP.loop is None
|
||||
background_threads_before = [
|
||||
thread.name for thread in background_threads()
|
||||
]
|
||||
|
||||
import lancedb
|
||||
|
||||
db = lancedb.connect("memory://")
|
||||
tables = db.table_names()
|
||||
|
||||
worker = LOOP.thread
|
||||
queue.put(
|
||||
dict(
|
||||
error=None,
|
||||
inherited_thread_is_none=inherited_thread_is_none,
|
||||
inherited_loop_is_none=inherited_loop_is_none,
|
||||
background_threads_before=background_threads_before,
|
||||
tables=tables,
|
||||
thread_alive=False if worker is None else worker.is_alive(),
|
||||
thread_daemon=False if worker is None else worker.daemon,
|
||||
background_thread_count=len(background_threads()),
|
||||
)
|
||||
)
|
||||
except BaseException:
|
||||
queue.put(dict(error=traceback.format_exc()))
|
||||
|
||||
assert LOOP.run(asyncio.sleep(0, result="parent-before")) == "parent-before"
|
||||
parent_thread = LOOP.thread
|
||||
assert parent_thread is not None
|
||||
|
||||
ctx = mp.get_context("fork")
|
||||
queue = ctx.Queue()
|
||||
|
||||
# Simulate forking while another thread held the initialization lock. The
|
||||
# child must replace the inherited lock instead of trying to acquire it.
|
||||
LOOP._start_lock.acquire()
|
||||
try:
|
||||
proc = ctx.Process(target=fork_child, args=(queue,))
|
||||
proc.start()
|
||||
finally:
|
||||
LOOP._start_lock.release()
|
||||
|
||||
proc.join(timeout=20)
|
||||
if proc.is_alive():
|
||||
proc.kill()
|
||||
proc.join()
|
||||
pytest.fail("Forked child hung starting the background loop")
|
||||
proc.terminate()
|
||||
proc.join(timeout=5)
|
||||
if proc.is_alive():
|
||||
proc.kill()
|
||||
proc.join()
|
||||
raise AssertionError("Forked child hung starting the background loop")
|
||||
|
||||
assert proc.exitcode == 0, f"child exited with code {proc.exitcode}"
|
||||
payload = queue.get(timeout=5)
|
||||
queue.close()
|
||||
assert payload["error"] is None, payload["error"]
|
||||
assert payload["inherited_thread_is_none"] is True
|
||||
assert payload["inherited_loop_is_none"] is True
|
||||
assert payload["background_threads_before"] == []
|
||||
assert payload["tables"] == []
|
||||
assert payload["thread_alive"] is True
|
||||
assert payload["thread_daemon"] is True
|
||||
assert payload["background_thread_count"] == 1
|
||||
assert proc.exitcode == 0, "child exited with code %s" % proc.exitcode
|
||||
payload = queue.get(timeout=5)
|
||||
queue.close()
|
||||
assert payload["error"] is None, payload["error"]
|
||||
assert payload["inherited_thread_is_none"] is True
|
||||
assert payload["inherited_loop_is_none"] is True
|
||||
assert payload["background_threads_before"] == []
|
||||
assert payload["tables"] == []
|
||||
assert payload["thread_alive"] is True
|
||||
assert payload["thread_daemon"] is True
|
||||
assert payload["background_thread_count"] == 1
|
||||
|
||||
# The child's reset must not disturb the parent's loop.
|
||||
assert LOOP.thread is parent_thread
|
||||
assert parent_thread.is_alive()
|
||||
assert LOOP.run(asyncio.sleep(0, result="parent-after")) == "parent-after"
|
||||
# The child's reset must not disturb the parent's loop.
|
||||
assert LOOP.thread is parent_thread
|
||||
assert parent_thread.is_alive()
|
||||
assert LOOP.run(asyncio.sleep(0, result="parent-after")) == "parent-after"
|
||||
""",
|
||||
# join(20) + terminate join(5) + queue get(5), plus startup and traceback.
|
||||
timeout=40,
|
||||
)
|
||||
_assert_subprocess_ok(result)
|
||||
|
||||
Reference in New Issue
Block a user