Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions python/python/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import os
import sys
import tempfile
from typing import Optional

import pytest
Expand Down Expand Up @@ -86,6 +88,28 @@ def pytest_addoption(parser):
)


def _isolate_torch_inductor_cache(workerinput) -> None:
"""Give each xdist worker its own torch.compile cache directory.

`lance.torch.distance` decorates six kernels with `@torch.compile`, so every
worker that reaches the torch accelerator compiles the same functions into
torch's single default cache directory. Inductor writes each generated module
by replacing it, which POSIX allows while another process holds it open and
Windows refuses, so the shared directory surfaces as an intermittent
`PermissionError` out of the codecache rather than a test failure.

Only under xdist: a single process cannot race itself, and leaving torch's
default alone there keeps the cache warm across runs. The per-worker
directories live under the same temp root for the same reason.
"""
if workerinput is None:
return
root = os.environ.get("TORCHINDUCTOR_CACHE_DIR") or os.path.join(
tempfile.gettempdir(), "lance_torchinductor"
)
os.environ["TORCHINDUCTOR_CACHE_DIR"] = os.path.join(root, workerinput["workerid"])


def pytest_configure(config):
config.addinivalue_line(
"markers",
Expand All @@ -103,6 +127,7 @@ def pytest_configure(config):
)

workerinput = getattr(config, "workerinput", None)
_isolate_torch_inductor_cache(workerinput)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug fix has no committed regression test, so the worker isolation can regress without detection and the current head does not meet the repository requirement that every bug fix have corresponding tests. Please add a lightweight test for _isolate_torch_inductor_cache covering None (serial execution leaves the environment unchanged), two worker IDs (distinct child directories), and a pre-set cache root. Those mapping cases exercise the contract directly without needing to reproduce Windows file locking.

if workerinput is not None:
from compat.compat_decorator import use_version_snapshot

Expand Down
Loading