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

VU context should control the iteration #1410

Open
2 tasks done
ankur22 opened this issue Aug 30, 2024 · 1 comment
Open
2 tasks done

VU context should control the iteration #1410

ankur22 opened this issue Aug 30, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@ankur22
Copy link
Collaborator

ankur22 commented Aug 30, 2024

Brief summary

Currently the browser module relies on the the events that it is subscribed to control its lifecycle. The reasons we have this is to allow the browser module to control the number of chromium instances that are used per test run.

An unexpected consequence of working with the event system is that we were unable to easily work with the vu context to allow k6 to abort tests early. If a vu context was used to control the chromium subprocess/connection then there would be a race condition between chromium unexpectedly shutting down when the iteration ends and the current API calls being made via CDP.

The work around for this is to use a background context that is created when the module is created. This means the module relies on the events system to correctly shut down iterations.

When this doesn't work is when a gracefulStop occurs (SIGTERM for example) and the iteration's vu context is closed. At this point k6 expects the current API call to cancel itself since the context is closed and allow it to clean itself up and gracefully shutdown.

Since the browser module isn't working with the vu context, it fails to shut it self down promptly, which can result in various other timeouts elsewhere. We should look at using the vu context to control the API calls, but rely on the events to control the chromium browser lifecycle -- IterEnd and exit events are called once the iteration's vu context is closed and the browser module hands over the main thread back to k6.

xk6-browser version

v0.53.0

OS

NA

Chrome version

NA

Docker version and image (if applicable)

No response

Steps to reproduce the problem

Run the following script, which will block the iteration and then send a SIGTERM signal (kill -15 {pid}) to the k6 process—it won't quit the process (until a default of 30 seconds or a user-specified timeout elapses):

import { browser } from 'k6/browser';

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
            type: 'chromium',
        },
      },
    },
  },
}

export default async function() {
  const context = await browser.newContext();
  const page = await context.newPage();

  try {
    await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
    await Promise.all([
      page.waitForNavigation(),
      page.locator('a[href="/my_messages.php"]').click(),
    ]);
    // indefinitely block here
    await page.locator("NOT-EXISTS").textContent();
  } finally {
    await page.close();
  }
}

Expected behaviour

The test exits promptly and doesn't wait for the timeout to occur.

Actual behaviour

The browser module waits for the timeout to occur.

Tasks

@ankur22 ankur22 added the bug Something isn't working label Aug 30, 2024
@ankur22 ankur22 self-assigned this Aug 30, 2024
@inancgumus
Copy link
Member

This issue might be related to #1253 as well.

ankur22 added a commit to grafana/k6 that referenced this issue Sep 2, 2024
This is a temporary solution to a problem the browser module is facing
when a signal (e.g. SIGTERM) is handled by k6, but is not correctly
handled by the browser module. The reason is due to it not working with
the vu context. More details in the open issue:
grafana/xk6-browser#1410.

The introduction of a temporary abort event will be fired, which the
browser module can intercept and action on immediately.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants