-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Fix security issue for retry package (#48767)
I randomly find lancedb issue: lancedb/lancedb#1480 which discloses a high-severity CVE Considering as lancedb, ray only has one use case for `retry` package, I took the same approach as lancedb/lancedb#1749, which names all variables better with unit and default value. --------- Signed-off-by: dentiny <dentinyhao@gmail.com>
- Loading branch information
Showing
6 changed files
with
140 additions
and
15 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
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 @@ | ||
"""Utils on retry.""" | ||
|
||
import time | ||
from functools import wraps | ||
from typing import Tuple | ||
|
||
# Default configuration for retry. | ||
_DEFAULT_MAX_RETRY_COUNT: int = 10 | ||
_DEFAULT_INIT_DELAY_SEC: int = 1 | ||
_DEFAULT_MAX_DELAY_SEC: int = 30 | ||
_DEFAULT_BACKOFF: int = 2 | ||
_DEFAULT_JITTER_SEC: int = 1 | ||
_DEFAULT_EXCEPTIONS: Tuple[Exception] = (Exception,) | ||
|
||
|
||
def retry( | ||
max_retry_count: int = _DEFAULT_MAX_RETRY_COUNT, | ||
init_delay_sec: int = _DEFAULT_INIT_DELAY_SEC, | ||
max_delay_sec: int = _DEFAULT_MAX_DELAY_SEC, | ||
backoff: int = _DEFAULT_BACKOFF, | ||
jitter_sec: int = _DEFAULT_JITTER_SEC, | ||
exceptions: Tuple[Exception] = _DEFAULT_EXCEPTIONS, | ||
): | ||
def wrapper(fn): | ||
@wraps(fn) | ||
def wrapped(*args, **kwargs): | ||
for cur_retry_count in range(max_retry_count): | ||
try: | ||
return fn(*args, **kwargs) | ||
except exceptions: | ||
if cur_retry_count + 1 == max_retry_count: | ||
raise | ||
|
||
sleep_sec = min( | ||
init_delay_sec * (backoff**cur_retry_count) + jitter_sec, | ||
max_delay_sec, | ||
) | ||
time.sleep(sleep_sec) | ||
|
||
return wrapped | ||
|
||
return wrapper |
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,75 @@ | ||
from ray_release import retry | ||
|
||
import sys | ||
import pytest | ||
|
||
|
||
def test_retry_with_no_error(): | ||
invocation_count = 0 | ||
|
||
# Function doesn't raise exception; use a dummy value to check invocation. | ||
@retry.retry() | ||
def no_error_func() -> int: | ||
nonlocal invocation_count | ||
invocation_count += 1 | ||
return 1 | ||
|
||
assert no_error_func() == 1 | ||
assert invocation_count == 1 | ||
|
||
|
||
# Test senario: exception count is less than retry count. | ||
def test_retry_with_limited_error(): | ||
invocation_count = 0 | ||
|
||
# Function doesn't raise exception; use a dummy value to check invocation. | ||
@retry.retry(init_delay_sec=1, jitter_sec=1) | ||
def limited_error() -> int: | ||
nonlocal invocation_count | ||
|
||
invocation_count += 1 | ||
|
||
if invocation_count == 1: | ||
raise Exception("Manual exception") | ||
return 1 | ||
|
||
assert limited_error() == 1 | ||
assert invocation_count == 2 | ||
|
||
|
||
# Test senario: exception count exceeds retry count. | ||
def test_retry_with_unlimited_error(): | ||
invocation_count = 0 | ||
|
||
@retry.retry(init_delay_sec=1, jitter_sec=1, backoff=1, max_retry_count=3) | ||
def unlimited_error() -> int: | ||
nonlocal invocation_count | ||
|
||
invocation_count += 1 | ||
raise Exception("Manual exception") | ||
|
||
with pytest.raises(Exception, match="Manual exception"): | ||
unlimited_error() | ||
assert invocation_count == 3 | ||
|
||
|
||
def test_retry_on_certain_errors(): | ||
invocation_count = 0 | ||
|
||
# Function doesn't raise exception; use a dummy value to check invocation. | ||
@retry.retry(init_delay_sec=1, jitter_sec=1, exceptions=(KeyError,)) | ||
def limited_error() -> int: | ||
nonlocal invocation_count | ||
|
||
invocation_count += 1 | ||
|
||
if invocation_count == 1: | ||
raise KeyError("Manual exception") | ||
return 1 | ||
|
||
assert limited_error() == 1 | ||
assert invocation_count == 2 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(pytest.main(["-sv", __file__])) |
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 |
---|---|---|
|
@@ -14,7 +14,6 @@ pyyaml | |
pybuildkite | ||
PyGithub | ||
requests | ||
retry | ||
twine == 5.0.0 | ||
docker >= 7.1.0 | ||
|
||
|
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