Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timeouts #234

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
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
24 changes: 23 additions & 1 deletion ragna/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import functools
import inspect
import os
import sys
import threading
from pathlib import Path
from typing import Any, Callable, Optional, Union
Expand Down Expand Up @@ -72,12 +74,15 @@ def handle_localhost_origins(origins: list[str]) -> list[str]:


def timeout_after(
seconds: float = 5, *, message: str = ""
seconds: float = 30, *, message: str = ""
) -> Callable[[Callable], Callable]:
timeout = f"Timeout after {seconds:.1f} seconds"
message = timeout if message else f"{timeout}: {message}"

def decorator(fn: Callable) -> Callable:
if is_debugging():
return fn

@functools.wraps(fn)
def wrapper(*args: Any, **kwargs: Any) -> Any:
result: Any = TimeoutError(message)
Expand All @@ -103,3 +108,20 @@ def target() -> None:
return wrapper

return decorator


# Vendored from pytest-timeout
# https://github.com/pytest-dev/pytest-timeout/blob/d91e6d8d69ad706e38a2c9de461a72c4d19777ff/pytest_timeout.py#L218-L247
def is_debugging() -> bool:
trace_func = sys.gettrace()
trace_module = None
if trace_func:
trace_module = inspect.getmodule(trace_func) or inspect.getmodule(
trace_func.__class__
)
if trace_module:
parts = trace_module.__name__.split(".")
for name in {"pydevd", "bdb", "pydevd_frame_evaluator"}:
if any(part.startswith(name) for part in parts):
return True
return False
2 changes: 1 addition & 1 deletion ragna/deploy/_cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def check_api_available() -> bool:
try:
if process is not None:

@timeout_after(30)
@timeout_after()
def wait_for_api() -> None:
while not check_api_available():
time.sleep(0.5)
Expand Down
18 changes: 0 additions & 18 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import contextlib
import inspect
import socket
import subprocess
import sys
Expand All @@ -20,23 +19,6 @@ def background_subprocess(*args, stdout=sys.stdout, stderr=sys.stdout, **kwargs)
process.communicate()


# Vendored from pytest-timeout
# https://github.com/pytest-dev/pytest-timeout/blob/d91e6d8d69ad706e38a2c9de461a72c4d19777ff/pytest_timeout.py#L218-L247
def is_debugging():
trace_func = sys.gettrace()
trace_module = None
if trace_func:
trace_module = inspect.getmodule(trace_func) or inspect.getmodule(
trace_func.__class__
)
if trace_module:
parts = trace_module.__name__.split(".")
for name in {"pydevd", "bdb", "pydevd_frame_evaluator"}:
if any(part.startswith(name) for part in parts):
return True
return False


def get_available_port():
with socket.socket() as s:
s.bind(("", 0))
Expand Down
Loading