fix(kompress): degrade when a native dep is installed but unloadable (#3133)

## Description

`_is_pytorch_available()` and `_is_onnx_available()` catch only
`ImportError`. An
installed-but-unloadable native dependency raises `OSError` instead: on
Windows
without the MSVC redistributable, `import torch` fails with `WinError
126` loading
`c10.dll`.

So a probe whose contract is to return a bool raises instead, and
`is_kompress_available()` raises with it.
`content_router._get_kompress()` guards its
call with `except ImportError` too, so the `OSError` propagates past
both guards into
the request path rather than degrading to "kompress unavailable".

Because the compressor is only cached on success, this repeats on every
request, and
Python drops failed modules from `sys.modules`, so each one pays a full
`torch` import
that ends in the same error.

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [x] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `_is_pytorch_available()` / `_is_onnx_available()` now catch
`(ImportError, OSError)`, so an installed-but-unloadable native
dependency degrades to "unavailable" instead of raising out of a
bool-returning probe.
- Both probes are `lru_cache`d. The answer cannot change within a
process, and nothing else memoises a *failing* import, so without this
the full failing `torch` import is retried on every request.
- Added `test_unloadable_native_dep_degrades_instead_of_raising`, which
simulates the Windows condition (onnxruntime absent so the `or` cannot
short-circuit, torch raising `OSError`) and asserts both the graceful
`False` and the single import attempt.
- `test_module_import_without_torch` now calls `cache_clear()` around
its assertion, which memoising makes necessary.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [ ] Manual testing performed

### Test Output

```text
$ uv run --extra dev pytest tests/test_transforms/ -q
======================= 384 passed, 67 skipped in 18.13s =======================

$ uvx ruff check .
All checks passed!

$ uv run --extra dev mypy headroom/transforms/kompress_compressor.py
Success: no issues found in 1 source file
```

## Real Behavior Proof

- Environment: Reproduced from a Windows Server 2022 (10.0.20348) box
where all three of vcruntime140.dll, vcruntime140_1.dll and msvcp140.dll
are absent from System32, so the installed torch cannot load its DLLs.
The behaviour was then isolated on macOS 26.5 / CPython 3.10.18 by
simulating that condition, since the failure is an exception type and
not platform-specific logic.
- Exact command / steps: On the Windows box, the packaged proxy logged
`OSError: [WinError 126] ... Error loading "...\torch\lib\c10.dll"`
escaping `_is_pytorch_available` at kompress_compressor.py:489 via
`_load_kompress` at :1002. To isolate, patch `builtins.__import__` so
`onnxruntime`/`optimum` raise `ImportError` (absent) and `torch` raises
`OSError` (installed, unloadable), then call `is_kompress_available()`
directly and through the same `try/except ImportError` guard
`content_router._get_kompress()` uses, counting torch import attempts
across 3 requests.
- Observed result: Before the change, `is_kompress_available()` raised
`OSError` rather than returning a bool, the error escaped the caller's
`except ImportError` on 3/3 requests, and torch was imported 4 times.
After the change, it returns `False`, 0/3 requests raise, and torch is
imported once. This is covered by the new test.
- Not tested: The fixed build has not been run end-to-end on the Windows
box, because installing the MSVC redistributable there would remove the
very condition under test. Whether kompress then loads successfully on
Windows once the redistributable is present is out of scope for this PR,
which only makes its absence degrade correctly.

## Runtime Rollout Safety

- Rollout-managed feature(s): None. This is an availability probe on an
existing optional dependency, not a gated feature.
- Minimum rollout channel: N/A
- Stable/default behavior changed: No behaviour change where the
dependencies import cleanly. The probe returns the same value; only the
installed-but-unloadable case changes, from raising to returning False.
- Kill switch / disable path: N/A. Existing kompress controls
(HEADROOM_KOMPRESS_ENDPOINT, the router's own gates) are untouched.
- Unsafe override required: No
- Qualification impact: None expected. The one existing test that
changed did so because memoising the probe makes an explicit
cache_clear() necessary; its assertion is unchanged.
- Rollback path: Revert the commit. There is no persisted state or
migration.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines


🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: JD Davis <mxjerrett@gmail.com>
This commit is contained in:
gglucass
2026-10-01 00:03:11 -05:00
committed by GitHub
co-authored by Claude Opus 5 JD Davis
parent f8645257c0
commit 8f3d677354
2 changed files with 62 additions and 2 deletions
+14 -2
View File
@@ -23,6 +23,7 @@ import re
import threading
import time
from dataclasses import dataclass
from functools import lru_cache
from typing import Any, Literal
from ..config import TransformResult
@@ -494,6 +495,7 @@ def _kompress_content_signature(content: str) -> Any:
)
@lru_cache(maxsize=1)
def _is_onnx_available() -> bool:
"""Check if ONNX Runtime is available (lightweight, no torch needed)."""
try:
@@ -501,10 +503,13 @@ def _is_onnx_available() -> bool:
import transformers # noqa: F401
return True
except ImportError:
except (ImportError, OSError):
# OSError: the package is installed but its native library will not
# load. See _is_pytorch_available for the full case.
return False
@lru_cache(maxsize=1)
def _is_pytorch_available() -> bool:
"""Check if full PyTorch stack is available (requires [ml] extra)."""
try:
@@ -513,7 +518,14 @@ def _is_pytorch_available() -> bool:
import transformers # noqa: F401
return True
except ImportError:
except (ImportError, OSError):
# An installed-but-unloadable native dependency raises OSError, not
# ImportError: on Windows without the MSVC redistributable, importing
# torch fails with WinError 126 loading c10.dll. Catching only
# ImportError let that escape a probe whose contract is to return a
# bool, and callers guard the call with `except ImportError` too, so it
# propagated into the request path instead of degrading to "no
# kompress".
return False
@@ -39,11 +39,59 @@ class TestLazyImports:
_is_pytorch_available,
)
# The probe memoises, so a real answer from an earlier call would
# otherwise mask the patch above.
_is_pytorch_available.cache_clear()
# Without both torch and onnxruntime, should return False
assert _is_pytorch_available() is False
_is_pytorch_available.cache_clear()
# Note: is_kompress_available() may still return True if onnxruntime
# was already imported before patching. Test the individual checkers.
def test_unloadable_native_dep_degrades_instead_of_raising(self) -> None:
"""An installed-but-unloadable torch raises OSError, not ImportError.
On Windows without the MSVC redistributable, `import torch` fails with
WinError 126 loading c10.dll. The probe caught only ImportError, so the
OSError escaped a function whose contract is to return a bool -- and the
request-path gate in content_router guards with `except ImportError`
too, so it propagated into request handling instead of degrading to
"kompress unavailable".
"""
import builtins
from headroom.transforms import kompress_compressor as kmod
kmod._is_onnx_available.cache_clear()
kmod._is_pytorch_available.cache_clear()
real_import = builtins.__import__
attempts = {"torch": 0}
def fake_import(name: str, *args: object, **kwargs: object) -> object:
if name in ("onnxruntime", "optimum"):
raise ImportError(f"No module named {name!r}")
if name == "torch":
attempts["torch"] += 1
raise OSError(
"[WinError 126] The specified module could not be found. "
'Error loading "torch\\lib\\c10.dll" or one of its dependencies.'
)
return real_import(name, *args, **kwargs)
with patch.object(builtins, "__import__", fake_import):
# Must degrade, not raise.
assert kmod._is_pytorch_available() is False
assert kmod.is_kompress_available() is False
# Cached: a failing import is not retried per request. Python drops
# failed modules from sys.modules, so nothing memoises it for us.
for _ in range(5):
assert kmod.is_kompress_available() is False
assert attempts["torch"] == 1
kmod._is_onnx_available.cache_clear()
kmod._is_pytorch_available.cache_clear()
def test_dataclasses_importable_without_torch(self) -> None:
"""KompressConfig, KompressResult, KompressCompressor are importable without torch."""
from headroom.transforms.kompress_compressor import (