Skip to content

Commit 6e6ff1b

Browse files
Fix Windows compatibility for AsyncBashSession by conditionally applying user settings
1 parent 4c99ec9 commit 6e6ff1b

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

openhands/runtime/utils/async_bash.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import asyncio
22
import os
33
import pwd
4+
import sys
5+
from typing import Any, Optional
46

57
from openhands.runtime.base import CommandResult
68

79

810
class AsyncBashSession:
911
@staticmethod
1012
async def execute(
11-
command: str, work_dir: str, username: str | None = None
13+
command: str, work_dir: str, username: Optional[str] = None
1214
) -> CommandResult:
1315
"""Execute a command in the bash session asynchronously."""
1416
work_dir = os.path.abspath(work_dir)
@@ -20,24 +22,31 @@ async def execute(
2022
if not command:
2123
return CommandResult(content='', exit_code=0)
2224

23-
env = {}
24-
if username:
25+
# Create subprocess arguments
26+
subprocess_kwargs: dict[str, Any] = {
27+
'stdout': asyncio.subprocess.PIPE,
28+
'stderr': asyncio.subprocess.PIPE,
29+
'cwd': work_dir,
30+
}
31+
32+
# Only apply user-specific settings on non-Windows platforms
33+
if username and sys.platform != 'win32':
2534
try:
2635
user_info = pwd.getpwnam(username)
27-
env['HOME'] = user_info.pw_dir
28-
env['USER'] = username
29-
env['LOGNAME'] = username
36+
env: dict[str, str] = {
37+
'HOME': user_info.pw_dir,
38+
'USER': username,
39+
'LOGNAME': username,
40+
}
41+
subprocess_kwargs['env'] = env
42+
subprocess_kwargs['user'] = username
3043
except KeyError:
3144
raise ValueError(f'User {username} does not exist.')
45+
3246
# Prepare to run the command
3347
try:
3448
process = await asyncio.subprocess.create_subprocess_shell(
35-
command,
36-
stdout=asyncio.subprocess.PIPE,
37-
stderr=asyncio.subprocess.PIPE,
38-
cwd=work_dir,
39-
user=username,
40-
env=env,
49+
command, **subprocess_kwargs
4150
)
4251

4352
try:

0 commit comments

Comments
 (0)