Skip to content

πŸ™Œ OpenHands: AI-Driven Development

feat(runtime): Set server process to run with highest system priority (#5206)

Xingyao Wang7db0a35305cd
diff --git a/openhands/runtime/impl/modal/modal_runtime.py b/openhands/runtime/impl/modal/modal_runtime.py
index 40014f8aa3..fb036a6d72 100644
--- a/openhands/runtime/impl/modal/modal_runtime.py
+++ b/openhands/runtime/impl/modal/modal_runtime.py
@@ -252,6 +252,7 @@ echo 'export INPUTRC=/etc/inputrc' >> /etc/bash.bashrc
self.config.sandbox.user_id,
plugin_args,
browsergym_args,
+ is_root=not self.config.run_as_openhands, # is_root=True when running as root
)
self.log('debug', f'Starting container with command: {sandbox_start_cmd}')
self.sandbox = modal.Sandbox.create(
diff --git a/openhands/runtime/impl/remote/remote_runtime.py b/openhands/runtime/impl/remote/remote_runtime.py
index d996441b66..cca97392f5 100644
--- a/openhands/runtime/impl/remote/remote_runtime.py
+++ b/openhands/runtime/impl/remote/remote_runtime.py
@@ -235,6 +235,7 @@ class RemoteRuntime(Runtime):
self.config.sandbox.user_id,
plugin_args,
browsergym_args,
+ is_root=not self.config.run_as_openhands, # is_root=True when running as root
)
start_request = {
'image': self.container_image,
diff --git a/openhands/runtime/impl/runloop/runloop_runtime.py b/openhands/runtime/impl/runloop/runloop_runtime.py
index 36ad4590b7..0b9310cc30 100644
--- a/openhands/runtime/impl/runloop/runloop_runtime.py
+++ b/openhands/runtime/impl/runloop/runloop_runtime.py
@@ -171,6 +171,7 @@ class RunloopRuntime(EventStreamRuntime):
self.config.sandbox.user_id,
plugin_args,
browsergym_args,
+ is_root=not self.config.run_as_openhands, # is_root=True when running as root
)
# Add some additional commands based on our image
diff --git a/openhands/runtime/utils/command.py b/openhands/runtime/utils/command.py
index 1617ec20f3..35a1252336 100644
--- a/openhands/runtime/utils/command.py
+++ b/openhands/runtime/utils/command.py
@@ -5,8 +5,9 @@ def get_remote_startup_command(
user_id: int,
plugin_args: list[str],
browsergym_args: list[str],
+ is_root: bool = False,
):
- return [
+ base_cmd = [
'/openhands/micromamba/bin/micromamba',
'run',
'-n',
@@ -27,3 +28,18 @@ def get_remote_startup_command(
str(user_id),
*browsergym_args,
]
+
+ if is_root:
+ # If running as root, set highest priority and lowest OOM score
+ cmd_str = ' '.join(base_cmd)
+ return [
+ 'nice',
+ '-n',
+ '-20', # Highest priority
+ 'sh',
+ '-c',
+ f'echo -1000 > /proc/self/oom_score_adj && exec {cmd_str}'
+ ]
+ else:
+ # If not root, run with normal priority
+ return base_cmd