Skip to content

Unrevert "Add username parameter to AsyncBashSession" #8771

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion openhands/runtime/action_execution_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ async def run(
try:
if action.is_static:
path = action.cwd or self._initial_cwd
result = await AsyncBashSession.execute(action.command, path)
result = await AsyncBashSession.execute(
action.command, path, self.username
)
obs = CmdOutputObservation(
content=result.content,
exit_code=result.exit_code,
Expand Down
2 changes: 1 addition & 1 deletion openhands/runtime/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ async def clone_or_init_repo(
'No repository selected. Initializing a new git repository in the workspace.'
)
action = CmdRunAction(
command='git init',
command=f'git init && git config --global --add safe.directory {self.workspace_root}'
)
self.run_action(action)
else:
Expand Down
34 changes: 29 additions & 5 deletions openhands/runtime/utils/async_bash.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import asyncio
import os
import pwd
import sys
from typing import Any, Optional

from openhands.runtime.base import CommandResult


class AsyncBashSession:
@staticmethod
async def execute(command: str, work_dir: str) -> CommandResult:
async def execute(
command: str, work_dir: str, username: Optional[str] = None
) -> CommandResult:
"""Execute a command in the bash session asynchronously."""
work_dir = os.path.abspath(work_dir)

Expand All @@ -17,12 +22,31 @@ async def execute(command: str, work_dir: str) -> CommandResult:
if not command:
return CommandResult(content='', exit_code=0)

# Create subprocess arguments
subprocess_kwargs: dict[str, Any] = {
'stdout': asyncio.subprocess.PIPE,
'stderr': asyncio.subprocess.PIPE,
'cwd': work_dir,
}

# Only apply user-specific settings on non-Windows platforms
if username and sys.platform != 'win32':
try:
user_info = pwd.getpwnam(username)
env: dict[str, str] = {
'HOME': user_info.pw_dir,
'USER': username,
'LOGNAME': username,
}
subprocess_kwargs['env'] = env
subprocess_kwargs['user'] = username
except KeyError:
raise ValueError(f'User {username} does not exist.')

# Prepare to run the command
try:
process = await asyncio.subprocess.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=work_dir,
command, **subprocess_kwargs
)

try:
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/test_runtime_git_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ async def test_clone_or_init_repo_no_repo_with_user_id(temp_dir):
# Verify that git init was called
assert len(runtime.run_action_calls) == 1
assert isinstance(runtime.run_action_calls[0], CmdRunAction)
assert runtime.run_action_calls[0].command == 'git init'
assert (
runtime.run_action_calls[0].command
== f'git init && git config --global --add safe.directory {runtime.workspace_root}'
)
assert result == ''


Expand All @@ -255,7 +258,10 @@ async def test_clone_or_init_repo_no_repo_no_user_id_no_workspace_base(temp_dir)
# Verify that git init was called
assert len(runtime.run_action_calls) == 1
assert isinstance(runtime.run_action_calls[0], CmdRunAction)
assert runtime.run_action_calls[0].command == 'git init'
assert (
runtime.run_action_calls[0].command
== f'git init && git config --global --add safe.directory {runtime.workspace_root}'
)
assert result == ''


Expand Down
Loading