-
Notifications
You must be signed in to change notification settings - Fork 285
works #1531
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
Open
BenCowen
wants to merge
1
commit into
main
Choose a base branch
from
customer-retries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
works #1531
Changes from all commits
Commits
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 hidden or 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,95 @@ | ||
| # --- | ||
| # cmd: ["modal", "run", "05_scheduling.custom_retries"] | ||
| # --- | ||
|
|
||
| # # Custom retries by exception type | ||
| # There are two types of retries in Modal: | ||
| # 1. When a function execution is interrupted by [preemption](https://modal.com/docs/guide/preemption#preemption), the input will be retried. This behavior is not configurable at this time. | ||
| # 2. When a function execution fails, ie by a raised Exception, Modal will retry the function call if you have [`modal.Retries`](https://modal.com/docs/reference/modal.Retries) configured. | ||
| # This example is about customizing the latter to only retry on certain exception types. | ||
| # For example, you may only want to retry on certain expected errors (e.g. timeouts, or | ||
| # transient network errors) and crash immediately on others (e.g. OOM, bad input). | ||
|
|
||
| # The trick is to: | ||
| # 1. Raise retryable errors in the usual way to trigger `modal.Retries` | ||
| # 2. Catch and `return` non-retryable errors. | ||
| # For #2, Modal will see a successful function call execution and return the exception | ||
| # to your client/server to handle as desired. | ||
|
|
||
| import modal | ||
|
|
||
| app = modal.App("example-custom-retries") | ||
|
|
||
| # ## Define retryable vs. crashable exceptions | ||
|
|
||
| retry_exceptions = ( | ||
| TimeoutError, | ||
| ConnectionError, | ||
| # transient CUDA errors, network blips, etc. | ||
| ) | ||
|
|
||
| crashable_exceptions = ( | ||
| MemoryError, | ||
| ValueError, | ||
| # OOM, bad input — retrying won't help | ||
| ) | ||
|
|
||
| # ## Use a Dict to track call count across retries | ||
| # | ||
| # Each retry runs in a new container invocation, so we use a | ||
| # [`modal.Dict`](https://modal.com/docs/reference/modal.Dict) to share | ||
| # state and make the demo deterministic. | ||
|
|
||
| call_counter = modal.Dict.from_name( | ||
| "custom-retries-demo-counter", create_if_missing=True | ||
| ) | ||
|
|
||
| # ## Demo App | ||
| # | ||
| # This function follows a scripted sequence to demonstrate the behavior: | ||
| # | ||
| # 1. **Call 1** — raises `TimeoutError` (retryable → Modal retries) | ||
| # 2. **Call 2** — raises `ConnectionError` (retryable → Modal retries) | ||
| # 3. **Call 3** — raises `MemoryError` (crashable → returned, no more retries) | ||
| # | ||
| # So you'll see two retries, then a clean stop on the third attempt. | ||
|
|
||
|
|
||
| @app.function(retries=modal.Retries(max_retries=5, initial_delay=1.0)) | ||
| def flaky_task(): | ||
| call_count = call_counter.get("calls", 0) + 1 | ||
| call_counter["calls"] = call_count | ||
| print(f"Attempt {call_count}") | ||
|
|
||
| # Scripted error sequence | ||
| errors = [ | ||
| TimeoutError("GPU timed out"), # attempt 1: retryable | ||
| ConnectionError("lost connection to data server"), # attempt 2: retryable | ||
| MemoryError("CUDA out of memory"), # attempt 3: crashable | ||
| ] | ||
| error = errors[min(call_count, len(errors)) - 1] | ||
|
|
||
| print(f" Hit: {error!r}") | ||
|
|
||
| if isinstance(error, retry_exceptions): | ||
| print(" -> retryable, re-raising so Modal retries") | ||
| raise error | ||
|
|
||
| # Return instead of raise — Modal sees success, stops retrying | ||
| print(" -> non-retryable, returning error to stop retries") | ||
| return error | ||
|
|
||
|
|
||
| # ## Entrypoint | ||
| # | ||
| # The caller checks whether the return value is an exception. | ||
|
|
||
|
|
||
| @app.local_entrypoint() | ||
| def main(): | ||
| call_counter["calls"] = 0 # reset counter | ||
| result = flaky_task.remote() | ||
| if isinstance(result, Exception): | ||
| print(f"Stopped with non-retryable error: {result!r}") | ||
| else: | ||
| print(f"Result: {result}") | ||
Oops, something went wrong.
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.
Do we have a list of exceptions that we'll retry? Do users want to configure their function so they can control when their retries?
I'm thinking of:
Uh oh!
There was an error while loading. Please reload this page.
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.
I have only gotten this request twice in the last year, so possibly not often enough to dedicate engineering...
Right now, retries are configured based on task status, and there is no exception filtering.