Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Stop runtimes rather than delete them #6403

Merged
merged 10 commits into from
Jan 23, 2025
Merged

Feat: Stop runtimes rather than delete them #6403

merged 10 commits into from
Jan 23, 2025

Conversation

tofarr
Copy link
Collaborator

@tofarr tofarr commented Jan 21, 2025

End-user friendly description of the problem this fixes or functionality that this introduces

Improved container management: Runtime containers are now stopped instead of deleted when not in use, allowing for faster reuse and better resource management. Containers are only deleted when a conversation is explicitly deleted.

  • Include this change in the Release Notes. If checked, you must provide an end-user friendly description for your change below

Give a summary of what the PR does, explaining any non-trivial design decisions

This change improves how OpenHands handles runtime containers by stopping them instead of deleting them when they are no longer needed. This allows for faster container reuse and better resource management.

This PR changes how runtime containers are handled in OpenHands:

  1. Changed default behavior to not keep runtimes alive by default (keep_runtime_alive=False)
  2. Modified container cleanup to stop containers instead of removing them
  3. Added proper container deletion when a conversation is deleted through the API
  4. Improved container port handling and reattachment logic
  5. Added new delete() class method to Runtime base class for explicit container cleanup

Design decisions:

  • Stop instead of remove: This allows for faster container reuse since we don't need to recreate and pull images
  • Default to not keeping runtimes alive: Better resource management by default
  • Explicit deletion: Only delete containers when a conversation is explicitly deleted, giving more control to users

Screenshots

2 Stopped Conversations
image
image

Restart a conversation
image
image

Link of any specific issues this addresses

N/A


To run this PR locally, use the following command:

docker run -it --rm   -p 3000:3000   -v /var/run/docker.sock:/var/run/docker.sock   --add-host host.docker.internal:host-gateway   -e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:9172ca8-nikolaik   --name openhands-app-9172ca8   docker.all-hands.dev/all-hands-ai/openhands:9172ca8

@tofarr tofarr marked this pull request as ready for review January 21, 2025 22:48
for exposed_port in config['ExposedPorts'].keys():
exposed_port = int(exposed_port.split('/tcp')[0])
if exposed_port != self._host_port and exposed_port != self._vscode_port:
self._app_ports.append(exposed_port)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change this logic because the existing logic did not work for stopped containers.

@@ -227,6 +228,8 @@ async def delete_conversation(
is_running = await session_manager.is_agent_loop_running(conversation_id)
if is_running:
await session_manager.close_session(conversation_id)
runtime_cls = get_runtime_cls(config.runtime)
await runtime_cls.delete(conversation_id)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly allow the runtime to run delete logic here.

Copy link
Collaborator

@rbren rbren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues I'm noticing:

  • When I start a fourth conversation, the newest docker instance goes down, instead of the oldest
  • When I resume an old conversation, it doesn't seem to respect the max of 3 running docker instances

@tofarr tofarr requested review from rbren January 22, 2025 18:53
@tofarr
Copy link
Collaborator Author

tofarr commented Jan 23, 2025

Two issues I'm noticing:

  • When I start a fourth conversation, the newest docker instance goes down, instead of the oldest
  • When I resume an old conversation, it doesn't seem to respect the max of 3 running docker instances

As noted on the call yesterday, the oldest conversation should now be the one that gets stopped, and the limitation of 3 instances is in place, but the conversation panel does not update in real time.

@tofarr tofarr merged commit 21d4ba0 into main Jan 23, 2025
14 checks passed
@tofarr tofarr deleted the feat-stop-runtimes branch January 23, 2025 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants