All Modal Function executions have a default execution timeout of 300 seconds (5 minutes), but users may specify timeout durations between 30 seconds and 3 hours.

import time

def f():
    time.sleep(599)  # Timeout!

def f():
    print("*Just* made it!")

The timeout duration is a measure of a Function’s execution time. It does not include scheduling time or any other period besides the time your code is executing in Modal. This duration is also per execution attempt, meaning Functions configured with modal.Retries will start new execution timeouts on each retry. For example, an infinite-looping Function with a 100 second timeout and 3 allowed retries will run for least 400 seconds within Modal.

Handling timeouts

After exhausting any specified retries, a timeout in a Function will produce a modal.exception.TimeoutError which you may catch in your code.

import modal.exception

def f():
    time.sleep(200)  # Timeout!

with stub.run():
    except modal.exception.TimeoutError:
        # Handle the timeout.

Timeout accuracy

Functions will run for at least as long as their timeout allows, but they may run a handful of seconds longer. If you require accurate and precise timeout durations on your Function executions, it is recommended that you implement timeout logic in your user code.