Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browser not stop during aborts (e.g. SIGTERM) #1420

Merged
merged 19 commits into from
Sep 12, 2024

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Sep 10, 2024

What?

This change fixes an issue whereby the browser module could block a test abort if the browser module was in the middle of waiting on something e.g. working with waitFor* apis.

The fix is to use the VU context for the iteration, but keep to the background context for running the chromium subprocess and connections. Now when the test is aborted, the vu context is closed by k6, which in turn will cancel all running browser API calls, and finally allow k6 to shutdown the test.

Why?

It was blocking the test from aborting. The reason was due to the use of the background context for the iteration as well as for running the browser subprocess and connection. k6 signals a test abort by cancelling the VU context, but since the browser module wasn't relying on that, the browser module would first complete the exiting API call/iteration before realising that the test was aborted.

We need to work with both the VU context to control the iteration lifecycle, and the background context so that we allow the k6 event system to control the chromium subprocess/connection lifecycle. They need to be separate to prevent race conditions when working with the chromium subprocess/connection.

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

Updates: #1410

This helps us by clarifying that this is a background context, and not
controlled by k6 (i.e. the VU context). The background context is to
only be used to control the lifecycle of chromium/connection. In later
commits vu context will be reintroduced to control the iteration
lifecycle so that k6 can abort the iteration and the browser module
doesn't block that from happening.
The background context will be used for the chromium subprocess,
connection and things that need to be controlled by the iterStart and
iterEnd (k6 event system) events. The k6Ctx is the context that is
controlled by the vu context. This will allow the iteration to abort
due to a SIGTERM while also allowing chromium to exit with endIter/exit
events.

At the moment this only introduces the k6Ctx, but it is still the
background context.
vuCtx is a clearer name which represents the VU context.
Now that Browser is setup to work with the different contexts, let's
actually set them up correctly.
This rename makes it clearer which context will be cancelled if the
function is called.
The background context isn't needed here. We're removing it to make
things clearer later on when we get to creating the Browser type, which
uses the cu context.
It didn't feel right having a goroutine where there was no way of
shutting it down. Now when the browser.Close() method is called it will
shut the init goroutine down too.
Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

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

Looks promising! 🎉

browser/registry_test.go Show resolved Hide resolved
browser/registry_test.go Outdated Show resolved Hide resolved
chromium/browser_type.go Outdated Show resolved Hide resolved
common/browser.go Show resolved Hide resolved
common/browser.go Outdated Show resolved Hide resolved
common/browser.go Outdated Show resolved Hide resolved
This is to ensure that we remember why we're using context.Background
for launching or connecting to chromium.
Encapsulate the retrieval of the browser count from the registry.
Encapsulate the retrieval of the iteration trace count from the
registry.
@ankur22 ankur22 merged commit 173c437 into main Sep 12, 2024
23 checks passed
@ankur22 ankur22 deleted the fix/browser-not-stop-sigterm branch September 12, 2024 13:48
This was referenced Sep 19, 2024
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.

2 participants