Skip to content

Conversation

@bennorwood
Copy link

Description

This PR adds session lifecycle management for AgentCoreBrowser and fixes a bug preventing session reinitialization after close.

Changes

Session Tracking and Cleanup

  • Add _client_dict to track active browser sessions by session_id
  • Populate _client_dict in create_browser_session() for proper session tracking
  • Register atexit handler in __init__() to ensure cleanup on process exit
  • Ensures browser sessions are properly closed when process terminates

Bug Fix: Session Reinitialization

  • Fix issue where creating a new session after close would fail with "Playwright not initialized"
  • Set _started = False in _async_cleanup() to allow playwright to reinitialize
  • Previously, _started remained True after cleanup, preventing _start() from being called again

Testing

  • test_bedrock_browser_create_browser_session_hydrates_client_dict: Verifies _client_dict is populated when sessions are created
  • test_bedrock_browser_registers_atexit_handler: Confirms cleanup handler is registered at initialization
  • test_bedrock_browser_reinitializes_playwright_after_close: Verifies sessions can be created, closed, and recreated successfully

All tests pass locally with hatch test tests/browser/test_agent_core_browser.py

Checklist

  • Code follows the project's style guidelines (ran hatch fmt --formatter)
  • Linting checks pass (ran hatch fmt --linter)
  • Tests pass locally (ran hatch test)
  • Commits follow Conventional Commits specification
  • Added tests for new functionality

Related Issues

Closes #286
Closes #205

@bennorwood bennorwood requested a review from a team as a code owner November 22, 2025 11:56
Implement proper session lifecycle management for browser instances:

Code changes:
- Add _client_dict to track active browser sessions by session_id
- Populate _client_dict in create_browser_session() for session tracking
- Register atexit handler in __init__() to ensure cleanup on process exit
- Ensures browser sessions are properly closed when process terminates

Tests:
- test_bedrock_browser_create_browser_session_hydrates_client_dict:
  Verifies _client_dict is populated when sessions are created

- test_bedrock_browser_registers_atexit_handler:
  Confirms cleanup handler is registered at initialization

This prevents resource leaks from orphaned browser sessions and ensures
all active sessions are tracked and cleaned up properly.
Set _started = False in _async_cleanup() to allow playwright to reinitialize
when creating new sessions after close. Previously, _started remained True
after cleanup, preventing _start() from being called and leaving _playwright
as None, which caused "Playwright not initialized" errors.

Add test to verify sessions can be created successfully after close.
@bennorwood bennorwood force-pushed the bennorw/agentcore-browser/bugfixes/client-session-handling branch from 61caf4a to 2437bc8 Compare November 22, 2025 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] browser tool cannot initialize new session after close action [BUG] AgentCoreBrowser tool doesn't close the browser session automatically

1 participant