Spaces:
Running
Running
File size: 3,737 Bytes
8b9b083 224cae0 2545b33 224cae0 2545b33 224cae0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | # SPDX-License-Identifier: BSD-3-Clause
"""Custom exceptions for environment server operations."""
from typing import Optional
class OpenEnvError(Exception):
"""Base exception for all OpenEnv errors."""
pass
class ConcurrencyConfigurationError(OpenEnvError):
"""
Raised when an environment is misconfigured for concurrent sessions.
This error is raised during server startup when max_concurrent_envs > 1
is specified for an environment that is not marked as SUPPORTS_CONCURRENT_SESSIONS.
"""
def __init__(
self,
environment_name: str,
max_concurrent_envs: int,
message: Optional[str] = None,
):
self.environment_name = environment_name
self.max_concurrent_envs = max_concurrent_envs
if message is None:
message = (
f"Environment '{environment_name}' is not marked as SUPPORTS_CONCURRENT_SESSIONS. "
f"Cannot run with max_concurrent_envs={max_concurrent_envs}. "
f"Either set max_concurrent_envs=1 or ensure the environment "
f"properly isolates session state and set SUPPORTS_CONCURRENT_SESSIONS=True."
)
super().__init__(message)
class SessionCapacityError(OpenEnvError):
"""
Raised when the server cannot accept new sessions due to capacity limits.
This error is raised when a new WebSocket connection is attempted but
the server has already reached max_concurrent_envs active sessions. The
default message tells the caller which server setting limits the number of
sessions, since the client (e.g. a trainer opening one session per
concurrent episode) usually cannot see the server configuration.
"""
def __init__(
self,
active_sessions: int,
max_sessions: int,
message: Optional[str] = None,
):
self.active_sessions = active_sessions
self.max_sessions = max_sessions
if message is None:
message = (
f"Server at capacity: {active_sessions}/{max_sessions} sessions active. "
f"Cannot accept new connections. To serve more concurrent sessions, "
f"start the server with a higher max_concurrent_envs (most "
f"environment servers read it from the MAX_CONCURRENT_ENVS "
f"environment variable). Values above 1 also require an environment "
f"that isolates per-session state and declares "
f"SUPPORTS_CONCURRENT_SESSIONS=True. Otherwise, open fewer "
f"sessions at once or close idle ones."
)
super().__init__(message)
class SessionNotFoundError(OpenEnvError):
"""Raised when attempting to access a session that does not exist."""
def __init__(self, session_id: str, message: Optional[str] = None):
self.session_id = session_id
if message is None:
message = f"Session '{session_id}' not found."
super().__init__(message)
class SessionCreationError(OpenEnvError):
"""Raised when a session cannot be created."""
def __init__(self, reason: str, message: Optional[str] = None):
self.reason = reason
if message is None:
message = f"Failed to create session: {reason}"
super().__init__(message)
class EnvironmentFactoryError(OpenEnvError):
"""Raised when the environment factory fails to create an instance."""
def __init__(self, factory_name: str, message: Optional[str] = None):
self.factory_name = factory_name
if message is None:
message = f"Environment factory '{factory_name}' failed to create instance."
super().__init__(message)
|