Skip to content

Commit

Permalink
Fix pr #6620: Fix memory leak in JSON encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Feb 5, 2025
1 parent 2832dba commit cf2874a
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions openhands/runtime/browser/browser_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,51 @@ def check_alive(self, timeout: float = 60):
logger.debug(f'Browser env is not alive. Response ID: {response_id}')

def close(self):
if not self.process.is_alive():
return
try:
self.agent_side.send(('SHUTDOWN', None))
self.process.join(5) # Wait for the process to terminate
# Always try to send shutdown signal if process is alive
if self.process.is_alive():
logger.error(
'Browser process did not terminate, forcefully terminating...'
)
self.process.terminate()
self.process.join(5) # Wait for the process to terminate
if self.process.is_alive():
self.process.kill()
try:
self.agent_side.send(('SHUTDOWN', None))
self.process.join(5) # Wait for the process to terminate
self.agent_side.close()
self.browser_side.close()
except Exception:
pass

# Force terminate if still alive
if self.process.is_alive():
logger.error(
'Browser process did not terminate, forcefully terminating...'
)
try:
self.process.terminate()
self.process.join(5) # Wait for the process to terminate
except Exception:
pass

# Force kill if still alive
if self.process.is_alive():
try:
self.process.kill()
self.process.join(5) # Wait for the process to terminate
except Exception:
pass

# Always try to close pipes, even if process is not alive
try:
self.agent_side.close()
except Exception:
pass
try:
self.browser_side.close()
except Exception:
pass

# Ensure process is terminated
if self.process.is_alive():
logger.error('Failed to terminate browser process')
try:
self.process.kill() # Final attempt
except Exception:
pass
except Exception as e:
logger.error(f'Encountered an error when closing browser env: {e}')

Expand Down

0 comments on commit cf2874a

Please sign in to comment.