Skip to content

Add timeout mechanism for code execution #1318

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

Merged

Conversation

albertvillanova
Copy link
Member

This PR implements a timeout mechanism for code execution to prevent code from running indefinitely or consuming excessive resources.

As privately discussed with @akseljoonas, certain computations (such as extremely large exponentiations) can lead to unresponsive behavior or exhaust available memory. For example:

log_5_result = 3 ** 2048
log_7_n_result = 5 ** log_5_result

To mitigate such risks, this update enforces a configurable timeout limit, ensuring that execution is safely interrupted if it exceeds the allowed duration.

@albertvillanova albertvillanova merged commit 6b4ad14 into huggingface:main May 16, 2025
3 checks passed
@albertvillanova albertvillanova deleted the timeout-local-executor branch May 16, 2025 05:05
@enzomich
Copy link

It doesn't work under Windows, which has no SIGALRM:

 ─ Executing parsed code: ────────────────────────
  final_answer("The Dirac equation is given by $
  (i\gamma^\mu \partial_\mu - m)\psi = 0 $.")
 ─────────────────────────────────────────────────
module 'signal' has no attribute 'SIGALRM'
[Step 2: Duration 54.10 seconds| Input tokens:
6,534 | Output tokens: 853]
━━━━━━━━━━━━━━━━━━━━━ Step 3 ━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ────────────────────────
  final_answer("The Dirac equation is: $
  (i\\gamma^\\mu \\partial_\\mu - m)\\psi = 0 $")
 ─────────────────────────────────────────────────
module 'signal' has no attribute 'SIGALRM'
[Step 3: Duration 37.12 seconds| Input tokens:
10,838 | Output tokens: 1,095]
[...]```

albertvillanova added a commit to albertvillanova/smolagents that referenced this pull request May 16, 2025
@albertvillanova
Copy link
Member Author

albertvillanova commented May 16, 2025

Thanks for reporting, @enzomich.

We are reverting the implementation and thinking about a better one:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants