-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
[core] Fix security issue for retry package #48767
Merged
aslonnie
merged 12 commits into
ray-project:master
from
dentiny:hjiang/fix-retry-security
Nov 17, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b956372
fix security issue for retry package
dentiny 1bc8bd9
fix dependency file
dentiny c43a0fd
Add unit test for retry
dentiny 06c66d9
add test on retry failure
dentiny f336c57
move retry file around
dentiny c1d8ef6
Add test to build
dentiny 655d602
Update retry config name
dentiny c013610
update import
dentiny 9a786c7
Add param to add exceptions
dentiny 24383b9
Add exceptions test
dentiny 0e91ab8
only handle certain errors
dentiny ead3c85
type annotation
dentiny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these assignment types are implied by the way.