Skip to content

Conversation

@LBaronceli
Copy link
Contributor

@LBaronceli LBaronceli commented Dec 29, 2025

What?

Fixes a race during browser shutdown where Browser.onAttachedToTarget could attempt to create new pages or sessions after the browser context had already been canceled.

The change ensures that context cancellation during teardown is handled gracefully and does not surface as a fatal error or panic.

Why?

Under high concurrency (e.g. repeated test runs or low GOMAXPROCS), Target.attachedToTarget events can still be delivered while the browser is shutting down.

In this situation, attempting to create new pages leads to panics or test failures, even though shutdown is expected behavior.

This change aligns Browser.onAttachedToTarget with existing defensive handling elsewhere in the browser codebase by treating context cancellation as a non-fatal condition.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • [] I have run linter and tests locally (make check) and all pass.
  • Note: make check reports existing lint issues in unrelated packages that are already present on master. This PR intentionally does not address them to keep the fix narrowly scoped.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

Related PR(s)/Issue(s)

Closes #5504

@LBaronceli LBaronceli requested a review from a team as a code owner December 29, 2025 00:11
@LBaronceli LBaronceli requested review from codebien and oleiade and removed request for a team December 29, 2025 00:11
@CLAassistant
Copy link

CLAassistant commented Dec 29, 2025

CLA assistant check
All committers have signed the CLA.

@ankur22 ankur22 self-requested a review January 5, 2026 11:06
@LBaronceli LBaronceli temporarily deployed to azure-trusted-signing January 5, 2026 11:21 — with GitHub Actions Inactive
@LBaronceli LBaronceli temporarily deployed to azure-trusted-signing January 5, 2026 11:23 — with GitHub Actions Inactive
ankur22 added a commit that referenced this pull request Jan 6, 2026
ankur22 added a commit that referenced this pull request Jan 6, 2026
Copy link
Contributor

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

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

@LBaronceli we've had some discussions and I've played around with your change, and indeed it helps us avoid a panic which can cause various other issues in the integration tests (data races) as well as when running k6 normally (panic causing the test to fail unnecessarily).

Could you take a look at the error that the linter is pointing to and resolve it? I believe we should be able to work with something like:

err := ...
if err is context canceled {
    log
    continue
}
if err not nil {
    panic
}

WDYT?

@LBaronceli
Copy link
Contributor Author

Hey Ankur,

Sure! made sure the if statements aren't nested, and added a debug log in case the continue is triggered by the Canceled or DealineExceeded errors.

Thank you for you patience and help with this one!

Copy link
Contributor

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

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

Brilliant! Thanks for digging into this @LBaronceli and helping us with the fix 🙇 🚀

@LBaronceli LBaronceli temporarily deployed to azure-trusted-signing January 8, 2026 09:52 — with GitHub Actions Inactive
@LBaronceli LBaronceli temporarily deployed to azure-trusted-signing January 8, 2026 09:54 — with GitHub Actions Inactive
@ankur22 ankur22 requested a review from AgnesToulet January 8, 2026 11:24
@ankur22 ankur22 merged commit a7a0275 into grafana:master Jan 8, 2026
39 of 41 checks passed
@ankur22 ankur22 added this to the v1.6.0 milestone Jan 8, 2026
@LBaronceli LBaronceli deleted the fix-browser-context-cancellation branch January 11, 2026 19:16
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.

Context canceling causing browser integration tests to fail very often

4 participants