From e18ecf632017191b4ad7d87aed95eed6dc61d9b8 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 23 Jun 2023 21:14:08 -0700 Subject: [PATCH] aider: Added timeout of 60 seconds to coder.run call using threading module. # Context: USER: make sure the call into `coder.run` doesn't take longer than 60 seconds ASSISTANT: To ensure that the `coder.run` call doesn't take longer than 60 seconds, we can use the `threading` module to create a separate thread for the `coder.run` call and use the `join` method with a timeout of 60 seconds. If the thread is still alive after the timeout, we can raise an exception or handle it accordingly. Here are the changes needed in the `scripts/benchmark.py` file: ```python scripts/benchmark.py <<<<<<< ORIGINAL import argparse import datetime import json import os import shutil import subprocess import time from json.decoder import JSONDecodeError from pathlib import Path ======= import argparse import datetime import json import os import shutil import subprocess import time import threading from json.decoder import JSONDecodeError from pathlib import Path >>>>>>> UPDATED ``` ```python scripts/benchmark.py <<<<<<< ORIGINAL start = time.time() coder.run(with_message=instructions) dur += time.time() - start ======= def run_coder(): coder.run(with_message=instructions) start = time.time() coder_thread = threading.Thread(target=run_coder) coder_thread.start() coder_thread.join(60) # 60 seconds timeout if coder_thread.is_alive(): # Handle the case when the coder.run call takes longer than 60 seconds # You can raise an exception or handle it accordingly raise Exception("coder.run took longer than 60 seconds") dur += time.time() - start >>>>>>> UPDATED ``` These changes import the `threading` module, create a separate thread for the `coder.run` call, and set a timeout of 60 seconds. If the thread is still alive after the timeout, an exception is raised. You can modify the exception handling as needed. --- scripts/benchmark.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/benchmark.py b/scripts/benchmark.py index 0215c39b0..7cb0d7539 100644 --- a/scripts/benchmark.py +++ b/scripts/benchmark.py @@ -5,6 +5,7 @@ import os import shutil import subprocess import time +import threading from json.decoder import JSONDecodeError from pathlib import Path @@ -172,8 +173,19 @@ def run_test(testdir, model_name, edit_format, retries): dur = 0 test_outcomes = [] for i in range(retries): + def run_coder(): + coder.run(with_message=instructions) + start = time.time() - coder.run(with_message=instructions) + coder_thread = threading.Thread(target=run_coder) + coder_thread.start() + coder_thread.join(60) # 60 seconds timeout + + if coder_thread.is_alive(): + # Handle the case when the coder.run call takes longer than 60 seconds + # You can raise an exception or handle it accordingly + raise Exception("coder.run took longer than 60 seconds") + dur += time.time() - start if coder.num_control_c: