Skip to content

Commit

Permalink
fix windows-related signal issue
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Mar 23, 2024
1 parent eb76acf commit 8d03cfe
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions human_eval/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,32 @@ def check_correctness(problem: Dict, completion: str, timeout: float,

@contextlib.contextmanager
def time_limit(seconds: float):
def signal_handler(signum, frame):
raise TimeoutException("Timed out!")
signal.setitimer(signal.ITIMER_REAL, seconds)
signal.signal(signal.SIGALRM, signal_handler)
try:
yield
finally:
signal.setitimer(signal.ITIMER_REAL, 0)
# adaped from: https://github.com/openai/human-eval/issues/18#issuecomment-1609063376
import os
if os.name == 'posix': # linux
def signal_handler(signum, frame):
raise TimeoutException("Timed out!")
signal.setitimer(signal.ITIMER_REAL, seconds)
signal.signal(signal.SIGALRM, signal_handler)
try:
yield
finally:
signal.setitimer(signal.ITIMER_REAL, 0)

elif os.name == 'nt': # windows
import threading
timer = threading.Timer(seconds, lambda: (_ for _ in ()).throw(TimeoutException("Timed out!")))
timer.start()
try:
yield
finally:
timer.cancel()
else:
raise RuntimeError("Operating system not supported")


class TimeoutException(Exception):
pass


@contextlib.contextmanager
Expand Down

0 comments on commit 8d03cfe

Please sign in to comment.