Invalid Memory Address

I am getting this Error when running K6 Browser locally any help?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=

These is my options:

export const options = {
  scenarios: {
    browser: {
      executor: 'constant-vus',
      vus: 10,
      duration: '5m',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    }
}
};

Hi @chingher90, welcome to the community forum!

Can you share the sanitized version of the test script also with us?

1 Like

@bandorko

Here is the sanitized version

import { browser } from 'k6/experimental/browser';
import { sleep, check } from 'k6';
import { SharedArray } from "k6/data";
import papaparse from "https://jslib.k6.io/papaparse/5.1.1/index.js";

export const options = {
  scenarios: {
    browser: {
      executor: 'constant-vus',
      vus: 10,
      duration: '5m',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    }
}
};

  // not using SharedArray here will mean that the code in the function call (that is what loads and
  // parses the csv) will be executed per each VU which also means that there will be a complete copy
  // per each VU
const csvData = new SharedArray("UserData", function () {
  // Load CSV file and parse it using Papa Parse
  return papaparse.parse(open("./data/randomData.csv"), { header: true }).data;
});

  // Pick a random userID test data pair
  const getCSVRandomData = csvData[Math.floor(Math.random() * csvData.length)];
  console.log("Selected UserName Data: ", JSON.stringify(getCSVRandomData));

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

  try {
    await page.goto('https://FAKE_URL.com/index.html');
    page.locator('input[name="username"]').type(getCSVRandomData.users);
    page.locator('input[name="password"]').type(getCSVRandomData.password);
    const submitButton = page.locator("LOGINBUTTON_XPATH");
    await Promise.all([page.waitForNavigation(), submitButton.click()]);
    sleep(1.0)
    //Navigate to another page after login
    page.goto ('https://FAKE_URL.com');
    sleep(1.0)
    const text = page.locator('XPATH');
    if (text.isVisible()) {
      console.log("element is visible");
    }
    
  } finally {
    const signoutButton = page.locator('SIGNOUT_XPATH');
    if (signoutButton.isVisible()) {
      await Promise.all([page.waitForNavigation(), signoutButton.click()]);
      console.log(" Test Successfully Sign out");
    }else{
      console.log("SignOut failed to locate");
    };
    page.close();
  }
}


I confirm the script will work if I just config for only one VU for one iteration but if I bump up the VUs like the above settings …it will crash.

@chingher90: Are there any stacktrace in the error message?

I have the same error, also hapenning only when I use maultiple VUs. Here is my scenario:

{
        executor: 'constant-vus',
        vus: 10,
        duration: `4.5m`,
        gracefulStop: '30s',
        options: {
            browser: {
                type: 'chromium',
            },
        }
    }

Here is the error stack:

ERRO[0160] panic: runtime error: invalid memory address or nil pointer dereference
goroutine 182 [running]:
runtime/debug.Stack()
csTimeSeruntime/debug/stack.go:24 +0x64
go.k6.io/k6/js/common.RunWithPanicCatching.func1()
csExplaigo.k6.io/k6/js/common/util.go:82 +0x190
panic({0x101dc1960?, 0x102dc8c70?})
        runtime/panic.go:914 +0x218
github.com/dop251/goja.(*Runtime).runWrapped.func1()
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/runtime.go:2442 +0x168
panic({0x101dc1960?, 0x102dc8c70?})
        runtime/panic.go:914 +0x218
github.com/dop251/goja.(*vm).handleThrow(0x14002437320, {0x101dc1960, 0x102dc8c70})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:788 +0x3e0
github.com/dop251/goja.(*vm).try.func1()
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:807 +0x48
panic({0x101dc1960?, 0x102dc8c70?})
        runtime/panic.go:914 +0x218
github.com/dop251/goja.(*vm).handleThrow(0x14002437320, {0x101dc1960, 0x102dc8c70})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:788 +0x3e0
github.com/dop251/goja.(*vm).runTryInner.func1()
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:830 +0x48
panic({0x101dc1960?, 0x102dc8c70?})
        runtime/panic.go:914 +0x218
github.com/grafana/xk6-browser/common.(*screenshotter).fullPageSize(0x140050186e0, 0x1400231e9c0)
        github.com/grafana/xk6-browser@v1.3.0/common/screenshotter.go:105 +0xfc
github.com/grafana/xk6-browser/common.(*screenshotter).screenshotPage(0x14005018738?, 0x1400231e9c0, 0x14005018738)
        github.com/grafana/xk6-browser@v1.3.0/common/screenshotter.go:324 +0x2b8
github.com/grafana/xk6-browser/common.(*Page).Screenshot(0x1400231e9c0, {0x1020289b0, 0x140023e6ea0})
        github.com/grafana/xk6-browser@v1.3.0/common/page.go:1119 +0x29c
reflect.Value.call({0x101d62060?, 0x14000c872d0?, 0x14a3bb650?}, {0x1018afa59, 0x4}, {0x14000f1b080, 0x1, 0x0?})
        reflect/value.go:596 +0x994
reflect.Value.Call({0x101d62060?, 0x14000c872d0?, 0x140023e6ea0?}, {0x14000f1b080?, 0x140021779d0?, 0x10095f394?})
        reflect/value.go:380 +0x94
github.com/dop251/goja.(*Runtime).newWrappedFunc.(*Runtime).wrapReflectFunc.func1({{0x1020289b0, 0x140029b1080}, {0x140004949a0, 0x1, 0x1a}})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/runtime.go:1982 +0x2cc
github.com/dop251/goja.(*nativeFuncObject).vmCall(0x140003829c0, 0x14002437320, 0x1)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/func.go:563 +0x16c
github.com/dop251/goja.call.exec(0x2437320?, 0x14002437320)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:3366 +0x74
github.com/dop251/goja.(*vm).run(0x14002437320)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:582 +0x6c
github.com/dop251/goja.(*vm).runTryInner(0x14000ca73f0?)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:834 +0x58
github.com/dop251/goja.(*generator).step(0x14000c248c0)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/func.go:762 +0x30
github.com/dop251/goja.(*generator).nextThrow(0x14000c248c0, {0x101fa79e0, 0x14000b03cb0})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/func.go:815 +0x1f8
github.com/dop251/goja.(*asyncRunner).onRejected(0x14000c248c0, {{0x102028dd8, 0x102e27b00}, {0x14000969380, 0x1, 0x1}})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/func.go:697 +0xe0
github.com/dop251/goja.(*Runtime).callJobCallback(0x0?, 0x14a01c350?, {0x102028dd8?, 0x102e27b00?}, {0x14000969380?, 0x10015c800?, 0x14003efd588?})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/runtime.go:2878 +0x64
github.com/dop251/goja.(*Promise).reject.(*Runtime).triggerPromiseReactions.(*Runtime).newPromiseReactionJob.func1.1()
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/builtin_promise.go:213 +0xac
github.com/dop251/goja.(*vm).try(0x14002437320, 0x14003efd680)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/vm.go:811 +0x1e0
github.com/dop251/goja.(*Promise).reject.(*Runtime).triggerPromiseReactions.(*Runtime).newPromiseReactionJob.func1()
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/builtin_promise.go:212 +0xc0
github.com/dop251/goja.(*Runtime).leave(0x14000eaa800)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/runtime.go:2751 +0xe0
github.com/dop251/goja.(*Runtime).runWrapped(0x14000eaa800, 0x103445878?)
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/runtime.go:2451 +0xf0
github.com/dop251/goja.AssertFunction.func1({0x0?, 0x0?}, {0x14000969360?, 0x0?, 0x0?})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/runtime.go:2401 +0x78
github.com/dop251/goja.(*Runtime).wrapPromiseReaction.func1({0x101dcc800?, 0x140010deec0?})
        github.com/dop251/goja@v0.0.0-20231027120936-b396bb4c349d/builtin_promise.go:601 +0xa8
github.com/grafana/xk6-browser/k6ext.promise.func1.1()
        github.com/grafana/xk6-browser@v1.3.0/k6ext/promise.go:44 +0x4c
go.k6.io/k6/js/eventloop.(*EventLoop).Start(0x14002d66500, 0x14001084f00)
        go.k6.io/k6/js/eventloop/eventloop.go:177 +0x16c
go.k6.io/k6/js.(*VU).runFn.func2()
        go.k6.io/k6/js/runner.go:865 +0xe0
go.k6.io/k6/js/common.RunWithPanicCatching({0x10202d008?, 0x14000352800?}, 0x101d368a0?, 0x140027898c0?)
        go.k6.io/k6/js/common/util.go:86 +0x74
go.k6.io/k6/js.(*VU).runFn(0x14001764aa0, {0x102019c68, 0x140026c5a90}, 0x60?, 0x14001d82798, 0x14002218270, {0x140003379c0, 0x1, 0x1})
        go.k6.io/k6/js/runner.go:864 +0x1d8
go.k6.io/k6/js.(*ActiveVU).RunOnce(0x14000b5a000)
        go.k6.io/k6/js/runner.go:797 +0x3d4
go.k6.io/k6/lib/executor.ConstantVUs.Run.getIterationRunner.func7({0x102019c30, 0x14000f16180}, {0x102007818?, 0x14000b5a000?})
        go.k6.io/k6/lib/executor/helpers.go:81 +0x44
go.k6.io/k6/lib/executor.ConstantVUs.Run.func5({0x102013098, 0x14001764aa0})
        go.k6.io/k6/lib/executor/constant_vus.go:193 +0x284
created by go.k6.io/k6/lib/executor.ConstantVUs.Run in goroutine 165
        go.k6.io/k6/lib/executor/constant_vus.go:204 +0x708

Goja stack:

I’m using MacOS Ventura 13.3.1 Apple Mi Pro Chip 16GM Memory k6 v0.49.0 (go1.21.6, darwin/arm64)

Hi @armaaar , welcome to the community forum!

I think your error is definitely not the same, because your error happens in the page.Screenshot function, and the previous script doesn’t even call screenshot. I assume your script calls screenshot(). Can you share the script and the html page with us?

Thanks.

You are absolutely correct! I didn’t notice the screenshot part. I’ve removed it and I’m running the tests again. Hopefully they would be fixed now.

Here is a sanetized version of the script I was running when I got the error:

export function setup() {
    return getAccessToken()
}

export default async function (accessToken) {
    const tags = exec.vu.metrics.tags

    const browserContext = browser.newContext()
    browserContext.addCookies([{ name: 'access_token', value: accessToken, tags.url }])
    const page = browserContext.newPage()

    try {
        // Send request to server
        const response = await page.goto(tags.url)

        // Wait for page to load
        await page.waitForNavigation({waitUntil: 'load'})

        // Wait for all loaders to disappear
        page.waitForSelector('.loader', {state: 'detached', strict: false})

    } finally {
        check(page, { 'No Loaders': p => p.isHidden('.loader', {strict: false}) })

        page.screenshot({ path: `./screenshots/${tags.page}.png`, fullPage: true});
        page.close()
        browserContext.close()
    }
}

Hi,
I ended with panic: runtime error: invalid memory address or nil pointer dereference while trying to click on the element which is available in second tab.

able to get the innerHTML value of the element, but when try to click the element I encounter panic: runtime error: invalid memory address or nil pointer dereference

sample code:

console.log(page.innerHTML(xpathCreateNewTemplate));
page.click(xpathCreateNewTemplate);

k6 version: k6.exe v0.50.0 (commit/f18209a5e3, go1.21.8, windows/amd64)

Hi @mkumar5,

Can we help us reproduce this issue with a sample HTML and script?

Thanks.

Below is a sample Code

import {browser} from 'k6/experimental/browser';
import {sleep, check} from 'k6';

export const options = {
    scenarios: {
        browser: {
            executor: 'shared-iterations',
            vus:1,
            iterations: 1,
            maxDuration: '50s',
            options: {
            browser: {
                    type: 'chromium',
            },
            },
        },
    },
    thresholds: {
       
    }
  }

export default async function () {
    const baseURl ="https://alloy/accounts/sign-in";
    const idPassword = "#mat-input-1";
    const idUserName ="#mat-input-0";
    const passwordValue = "password";
    const userNameValue ="userName";
    const xpathCreateNewTemplate ="//button[@mattooltip='Create New']";
    const xpathPasswordContinue ="//div[@id='cdk-step-content-0-1']//button[contains(text(),'Continue')]";
    const xpathTemplateManager = '//mat-icon[normalize-space()="alternate_email"]';
    const xpathUserNameContinue ="//div[@id='cdk-step-content-0-0']//button[contains(text(),'Continue')]";

    const page = browser.newPage();

  try
    {
      await page.goto(baseURl);
      await signIn(page);
      var Page2 = clickTemplateManagerTile(page);
      clickCreateTemplate(Page2);
    }
    finally
    {
      page.close();
    }
        
    async function signIn(page) {
      const userNameElement = page.locator(idUserName);
      userNameElement.type(userNameValue);   
      page.click(xpathUserNameContinue);
    
      page.waitForNavigation();
      const passwordElement = page.locator(idPassword);
      passwordElement.type(passwordValue);  
      page.click(xpathPasswordContinue);
      await page.waitForNavigation();
      await page.waitForSelector(xpathTemplateManager,8000);

      check(page, { 
            'waitForFunction successfully resolved': page.locator(xpathTemplateManager).innerHTML() == 'alternate_email'
        });
    }

    function clickTemplateManagerTile(page) {
      page.click(xpathTemplateManager);
      sleep(2);
      page.waitForNavigation();    
      const allpages = browser.context().pages();
      var Page2 = allpages[1];
      check(Page2, { 
            'Check Page2 URL signed in': p => p.url().includes('template-manager'),
            'Check Page2 title': p => p.title().includes('Template Manager'),
        });
      return Page2;
    }
      
    async function clickCreateTemplate(page) {
      page.click(xpathCreateNewTemplate);
      // above click causes Invalid Memory Address and the above element is avaliable in second Tab
      }
}

Hi @mkumar5,

Thanks for the script. However, we can’t reproduce the issue without a target website or an HTML snippet. Please provide them if it’s possible.

Other than that, there are some problems within your script. As explained here and here, waitForNavigation and click return a Promise. So, you should use await or Promise.all for all of them.

Check out this example for the correct usage.

Hope this helps.

I’m running into this error too.
To reproduce: I’m simply following the steps from Running distributed tests | Grafana k6 documentation

➜  k6-operator git:(main) make deploy
/Users/michael/go/bin/controller-gen "crd:maxDescLen=0" rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xfe2438f]

goroutine 93 [running]:
go/types.(*Checker).handleBailout(0xc000b4cc00, 0xc000b41d40)
	/usr/local/go/src/go/types/check.go:367 +0x88
panic({0x1011cc40?, 0x106cfa70?})
	/usr/local/go/src/runtime/panic.go:770 +0x132
go/types.(*StdSizes).Sizeof(0x0, {0x101e7c50, 0x106d8220})
	/usr/local/go/src/go/types/sizes.go:228 +0x30f
go/types.(*Config).sizeof(...)
	/usr/local/go/src/go/types/sizes.go:333
go/types.representableConst.func1({0x101e7c50?, 0x106d8220?})
	/usr/local/go/src/go/types/const.go:76 +0x9e
go/types.representableConst({0x101edef0, 0x106a31d0}, 0xc000b4cc00, 0x106d8220, 0x0)
	/usr/local/go/src/go/types/const.go:92 +0x192
go/types.(*Checker).arrayLength(0xc000b4cc00, {0x101ec238, 0xc00132a160?})
	/usr/local/go/src/go/types/typexpr.go:510 +0x2d3
go/types.(*Checker).typInternal(0xc000b4cc00, {0x101ea858, 0xc0013274d0}, 0xc0013291d0)
	/usr/local/go/src/go/types/typexpr.go:299 +0x49d
go/types.(*Checker).definedType(0xc000b4cc00, {0x101ea858, 0xc0013274d0}, 0x10?)
	/usr/local/go/src/go/types/typexpr.go:180 +0x37
go/types.(*Checker).typeDecl(0xc000b4cc00, 0xc0013291d0, 0xc000812d00, 0x0)
	/usr/local/go/src/go/types/decl.go:615 +0x44d
go/types.(*Checker).objDecl(0xc000b4cc00, {0x101f2e00, 0xc0013291d0}, 0x0)
	/usr/local/go/src/go/types/decl.go:197 +0xa7f
go/types.(*Checker).packageObjects(0xc000b4cc00)
	/usr/local/go/src/go/types/resolver.go:681 +0x425
go/types.(*Checker).checkFiles(0xc000b4cc00, {0xc000f02560, 0x1, 0x1})
	/usr/local/go/src/go/types/check.go:408 +0x1a5
go/types.(*Checker).Files(...)
	/usr/local/go/src/go/types/check.go:372
sigs.k8s.io/controller-tools/pkg/loader.(*loader).typeCheck(0xc0002fb470, 0xc0003281a0)
	/Users/michael/go/pkg/mod/sigs.k8s.io/controller-tools@v0.13.0/pkg/loader/loader.go:286 +0x36a
sigs.k8s.io/controller-tools/pkg/loader.(*Package).NeedTypesInfo(0xc0003281a0)
	/Users/michael/go/pkg/mod/sigs.k8s.io/controller-tools@v0.13.0/pkg/loader/loader.go:99 +0x39
sigs.k8s.io/controller-tools/pkg/loader.(*TypeChecker).check(0xc000d71650, 0xc0003281a0)
	/Users/michael/go/pkg/mod/sigs.k8s.io/controller-tools@v0.13.0/pkg/loader/refs.go:268 +0x2b7
sigs.k8s.io/controller-tools/pkg/loader.(*TypeChecker).check.func1(0x0?)
	/Users/michael/go/pkg/mod/sigs.k8s.io/controller-tools@v0.13.0/pkg/loader/refs.go:262 +0x53
created by sigs.k8s.io/controller-tools/pkg/loader.(*TypeChecker).check in goroutine 71
	/Users/michael/go/pkg/mod/sigs.k8s.io/controller-tools@v0.13.0/pkg/loader/refs.go:260 +0x1c5
make: *** [manifests] Error 2
➜  k6-operator git:(main) kubectl version
Client Version: v1.29.1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.28.9-eks-036c24b
➜  k6-operator git:(main) git version
git version 2.23.0
➜  k6-operator git:(main) go version
go version go1.22.3 darwin/amd64
➜  k6-operator git:(main) make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0

Under the last few k6 versions which had the browser support merged into the main executable, I was getting the panic error with my single-page application that uses iFrames, but I have not been getting the error with the recently released version 51.