소스 검색

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

tofarr 1 년 전
부모
커밋
5ccee7c8a7
2개의 변경된 파일11개의 추가작업 그리고 2개의 파일을 삭제
  1. 8 2
      openhands/runtime/client/client.py
  2. 3 0
      openhands/server/session/session.py

+ 8 - 2
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')

+ 3 - 0
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: