Skip to content

Commit

Permalink
Enforce match timeouts when calling agents (#315)
Browse files Browse the repository at this point in the history
Before we would wait forever for a agent to return, THEN check and enforce the timeouts. If the agent died we would hang forever waiting for it.
  • Loading branch information
bovard authored Nov 18, 2024
1 parent a91a98f commit 1386e27
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions kaggle_environments/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import traceback
from contextlib import redirect_stderr, redirect_stdout
from io import StringIO
from requests.exceptions import Timeout
from time import perf_counter
from urllib.parse import urlparse
from .errors import DeadlineExceeded, InvalidArgument
Expand Down Expand Up @@ -78,16 +79,25 @@ def __call__(self, observation, configuration):
"observation": observation,
},
}
response = requests.post(url=self.raw, data=json.dumps(data))
response_json = response.json()
action = response_json["action"]
if action == "DeadlineExceeded":
action = DeadlineExceeded()
elif isinstance(action, str) and action.startswith("BaseException::"):
# Deserialize the exception message
parts = action.split("::", 1)
action = BaseException(parts[1])
return action
timeout = float(observation.remainingOverageTime) + float(configuration.actTimeout) + 10
try:
response = requests.post(url=self.raw, data=json.dumps(data), timeout=timeout)
response.raise_for_status()
response_json = response.json()
action = response_json["action"]
if action == "DeadlineExceeded":
action = DeadlineExceeded()
elif isinstance(action, str) and action.startswith("BaseException::"):
# Deserialize the exception message
parts = action.split("::", 1)
action = BaseException(parts[1])
return action
except Timeout:
print(f"Request timed out after {timeout} seconds")
return DeadlineExceeded()
except requests.exceptions.RequestException as e:
print(f"Request error: {e}")
return None


def build_agent(raw, builtin_agents, environment_name):
Expand Down

0 comments on commit 1386e27

Please sign in to comment.