Skip to content

test: avoid module reload state leakage - #6129

Open
daleselaji-dev wants to merge 1 commit into
browser-use:mainfrom
daleselaji-dev:fix/test-action-timeout-reload-5425
Open

daleselaji-dev wants to merge 1 commit into
browser-use:mainfrom
daleselaji-dev:fix/test-action-timeout-reload-5425

Conversation

@daleselaji-dev

@daleselaji-dev daleselaji-dev commented Oct 18, 2026 •

Copy link
Copy Markdown

Problem

tests/ci/test_action_timeout.py::test_malformed_env_timeout_does_not_break_import reloads browser_use.tools.service repeatedly. Later tests that compare the shared Tools class by identity can then fail even though the class names are identical.

Root Cause

importlib.reload() creates a new Tools class object. Modules that imported Tools by value before the reload retain the original object, so is and isinstance checks become order-dependent for the remainder of the pytest process.

Solution

Exercise the existing pure _parse_env_action_timeout() helper directly instead of reloading the service module. This preserves coverage of invalid and valid timeout values without mutating module/class identity.

Changes

  • Removed the reload/teardown fixture from tests/ci/test_action_timeout.py.
  • Added direct assertions for invalid fallback values and a valid 45 second value.
  • Product code is unchanged.

Testing

  • Baseline: uv run pytest tests/ci/test_action_timeout.py tests/ci/test_beta_agent.py -q reproduced the issue: test_beta_agent_constructor_type_hints_match_browser_use_core_params failed with two distinct Tools class objects.
  • Focused: uv run pytest tests/ci/test_action_timeout.py -q — 5 passed.
  • Regression: uv run pytest tests/ci/test_beta_agent.py::test_beta_agent_constructor_type_hints_match_browser_use_core_params -q — 1 passed.
  • uv run ruff check tests/ci/test_action_timeout.py tests/ci/test_beta_agent.py — passed.
  • uv run ruff format --check tests/ci/test_action_timeout.py tests/ci/test_beta_agent.py — passed.
  • uv run pyright tests/ci/test_action_timeout.py tests/ci/test_beta_agent.py — 0 errors, 0 warnings.
  • uv run pre-commit run --files tests/ci/test_action_timeout.py — passed.
  • git diff --check — passed.
  • Risk-matched uv run pytest tests/ci -q with PYTHONUTF8=1 PYTHONIOENCODING=utf-8: 87 passed, 3 skipped, then stopped at the unrelated existing Windows path assertion in tests/ci/infrastructure/test_config.py::TestLazyConfig::test_path_configuration (C:\Users\ACE\.cache vs expected /.cache). Without UTF-8, the same suite first stopped after 15 passes on an unrelated GBK console encoding error in tests/ci/browser/test_cross_origin_click.py.

Compatibility/Risk

This is test-only. It removes process-wide module mutation and keeps the timeout parser assertions unchanged in intent. No runtime behavior or public API changes.

Notes for Reviewer

The targeted baseline failure is order-dependent: running the timeout test before the beta type-hint test reproduces the class identity mismatch; the focused regression passes after this change. The full CI suite remains locally unverified beyond the unrelated Windows/environment failures documented above.

Linked Issue

Fixes #5425

AI Disclosure

AI assistance was used for diagnosis, implementation, and local validation; the change is limited to the test harness and the results above are from this checkout.


Summary by cubic

Fixes test pollution caused by reloading browser_use.tools.service, which recreates the Tools class and makes later identity-based tests order-dependent.

  • Replaces module reloading in test_action_timeout.py with direct calls to the existing _parse_env_action_timeout() helper.
  • Keeps the same coverage for invalid fallback values and the valid 45-second value.
  • Test-only change; no runtime behavior or public API changes.

Written for commit 03969ad. Summary will update on new commits.

View guided diff

@daleselaji-dev
daleselaji-dev marked this pull request as ready for review October 18, 2026 12:37

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/ci/test_action_timeout.py">

<violation number="1" location="tests/ci/test_action_timeout.py:147">
P3: This test now covers only the pure parser, so nothing checks that `BROWSER_USE_ACTION_TIMEOUT_S` actually feeds `_DEFAULT_ACTION_TIMEOUT_S` at import. A regression in the `os.getenv` call at module level would go unnoticed. Consider a subprocess-based check that imports the module with the env var set, which avoids the in-process reload that caused the state leak.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Reloading recreates the module's ``Tools`` class while other modules retain the
original class object, making later type-identity tests depend on test order.
"""
from browser_use.tools.service import _parse_env_action_timeout

@cubic-dev-ai cubic-dev-ai Bot Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This test now covers only the pure parser, so nothing checks that BROWSER_USE_ACTION_TIMEOUT_S actually feeds _DEFAULT_ACTION_TIMEOUT_S at import. A regression in the os.getenv call at module level would go unnoticed. Consider a subprocess-based check that imports the module with the env var set, which avoids the in-process reload that caused the state leak.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/ci/test_action_timeout.py, line 147:

<comment>This test now covers only the pure parser, so nothing checks that `BROWSER_USE_ACTION_TIMEOUT_S` actually feeds `_DEFAULT_ACTION_TIMEOUT_S` at import. A regression in the `os.getenv` call at module level would go unnoticed. Consider a subprocess-based check that imports the module with the env var set, which avoids the in-process reload that caused the state leak.</comment>

<file context>
@@ -132,45 +132,23 @@ def test_default_action_timeout_accommodates_extract_action():
+	Reloading recreates the module's ``Tools`` class while other modules retain the
+	original class object, making later type-identity tests depend on test order.
+	"""
+	from browser_use.tools.service import _parse_env_action_timeout
 
 	bad_values = ('', 'not-a-number', 'abc', 'nan', 'NaN', 'inf', '-inf', '0', '-5')
</file context>
Fix with cubic

sun04222 added a commit to sun04222/browser-use that referenced this pull request Oct 23, 2026

This branch has not been deployed

No deployments
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.

Test-order-dependent failure: importlib.reload() in test_action_timeout.py corrupts Tools class identity for the rest of the pytest process

1 participant