-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ingest/powerbi): add timeouts for m-query parsing
- Loading branch information
Showing
4 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
metadata-ingestion/src/datahub/utilities/threading_timeout.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import contextlib | ||
import functools | ||
import platform | ||
from typing import ContextManager | ||
|
||
from stopit import ThreadingTimeout as _ThreadingTimeout, TimeoutException | ||
|
||
__all__ = ["threading_timeout", "TimeoutException"] | ||
|
||
|
||
@functools.lru_cache(maxsize=1) | ||
def _is_cpython() -> bool: | ||
"""Check if we're running on CPython.""" | ||
return platform.python_implementation() == "CPython" | ||
|
||
|
||
def threading_timeout(timeout: float) -> ContextManager[None]: | ||
"""A timeout context manager that uses stopit's ThreadingTimeout underneath. | ||
This is only supported on CPython. | ||
That's because stopit.ThreadingTimeout uses a CPython-internal method to raise | ||
an exception (the timeout error) in another thread. See stopit.threadstop.async_raise. | ||
Reference: https://github.com/glenfant/stopit | ||
Args: | ||
timeout: The timeout in seconds. If <= 0, no timeout is applied. | ||
Raises: | ||
RuntimeError: If the timeout is not supported on the current Python implementation. | ||
TimeoutException: If the timeout is exceeded. | ||
""" | ||
|
||
if timeout <= 0: | ||
return contextlib.nullcontext() | ||
|
||
if not _is_cpython(): | ||
raise RuntimeError( | ||
f"Timeout is only supported on CPython, not {platform.python_implementation()}" | ||
) | ||
|
||
return _ThreadingTimeout(timeout, swallow_exc=False) |
31 changes: 31 additions & 0 deletions
31
metadata-ingestion/tests/unit/utilities/test_threading_timeout.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import time | ||
|
||
import pytest | ||
|
||
from datahub.utilities.threading_timeout import TimeoutException, threading_timeout | ||
|
||
|
||
def test_timeout_no_timeout(): | ||
# Should complete without raising an exception | ||
with threading_timeout(1.0): | ||
time.sleep(0.1) | ||
|
||
|
||
def test_timeout_raises(): | ||
# Should raise TimeoutException | ||
with pytest.raises(TimeoutException): | ||
with threading_timeout(0.1): | ||
time.sleep(0.5) | ||
|
||
|
||
def test_timeout_early_exit(): | ||
# Test that context manager handles other exceptions properly | ||
with pytest.raises(ValueError): | ||
with threading_timeout(1.0): | ||
raise ValueError("Early exit") | ||
|
||
|
||
def test_timeout_zero(): | ||
# Should not raise an exception | ||
with threading_timeout(0.0): | ||
pass |