Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix Bash commands now do not block and actually respect the timeout (#4058)

tofarr5ccee7c8a7d8
diff --git a/openhands/runtime/client/client.py b/openhands/runtime/client/client.py
index f9098765eb..43bc7cd15a 100644
--- a/openhands/runtime/client/client.py
+++ b/openhands/runtime/client/client.py
@@ -323,7 +323,13 @@ class RuntimeClient:
logger.debug('Requesting exit code...')
self.shell.expect(self.__bash_expect_regex, timeout=timeout)
_exit_code_output = self.shell.before
- exit_code = int(_exit_code_output.strip().split()[0])
+ try:
+ exit_code = int(_exit_code_output.strip().split()[0])
+ except:
+ logger.error('Error getting exit code from bash script')
+ # If we try to run an invalid shell script the output sometimes includes error text
+ # rather than the error code - we assume this is an error
+ exit_code = 2
except pexpect.TIMEOUT as e:
if kill_on_timeout:
@@ -622,7 +628,7 @@ if __name__ == '__main__':
observation = await client.run_action(action)
return event_to_dict(observation)
except Exception as e:
- logger.error(f'Error processing command: {str(e)}')
+ logger.error(f'Error processing command: {str(e)}', exc_info=True, stack_info=True)
raise HTTPException(status_code=500, detail=str(e))
@app.post('/upload_file')
diff --git a/openhands/server/session/session.py b/openhands/server/session/session.py
index 4afd3c1ee8..3254cf1457 100644
--- a/openhands/server/session/session.py
+++ b/openhands/server/session/session.py
@@ -162,6 +162,9 @@ class Session:
'Model does not support image upload, change to a different model or try without an image.'
)
return
+ asyncio.run_coroutine_threadsafe(self._add_event(event, EventSource.USER), self.agent_session.loop) # type: ignore
+
+ async def _add_event(self, event, event_source):
self.agent_session.event_stream.add_event(event, EventSource.USER)
async def send(self, data: dict[str, object]) -> bool: