grafana / xk6-browser

k6 extension that adds support for browser automation and end-to-end web testing via the Chrome Devtools Protocol
https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/browser/
GNU Affero General Public License v3.0
337 stars 42 forks source link

runtime error: invalid memory address or nil pointer dereference #119

Closed LaserPhaser closed 2 years ago

LaserPhaser commented 2 years ago

runtime error: invalid memory address or nil pointer dereference has happened looks like its because of UserAgent:

userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/16B92 UCBrowser/12.1.7.1109 Mobile AliApp(TUnionSDK/0.1.20.3)'

Reproducer:

import launcher from "k6/x/browser";

export let options = {
    scenarios: {
        constant_request_rate: {
            executor: 'constant-arrival-rate',
            rate: 1,
            timeUnit: '1000m', // 1000 iterations per second, i.e. 1000 RPS
            duration: '1000m',
            preAllocatedVUs: 500, // how large the initial pool of VUs would be
            maxVUs: 5000, // if the preAllocatedVUs are not enough, we can initialize more

        },
    },
};

export default function () {
    const browser = launcher.launch('chromium', {headless: false, devtools: true,});
    const context = browser.newContext({
        userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/16B92 UCBrowser/12.1.7.1109 Mobile AliApp(TUnionSDK/0.1.20.3)',
        isMobile: true,
        viewport: {width: 600, height: 800}
    });
    const page = context.newPage();
    page.goto('http://google.com/', {waitUntil: 'load'});
    page.screenshot({path: `example-chromium.png`});
    page.close();
    browser.close();
}

Exception:

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

goroutine 1111 [running]:
github.com/grafana/xk6-browser/common.(*Frame).clearLifecycle(0xc008d97380)
    github.com/grafana/xk6-browser@v0.1.0/common/frame.go:202 +0x17f
github.com/grafana/xk6-browser/common.(*FrameManager).frameNavigated(0xc0082c9830, {0xc00176abe0, 0x0}, {0x0, 0x0}, {0xc00176ac20, 0x20}, {0x0, 0x0}, {0xc00176ac40, ...}, ...)
    github.com/grafana/xk6-browser@v0.1.0/common/frame_manager.go:227 +0x588
github.com/grafana/xk6-browser/common.(*FrameSession).onFrameNavigated(0xc0082f1a00, 0xc0086d6360, 0x20)
    github.com/grafana/xk6-browser@v0.1.0/common/frame_session.go:513 +0x165
github.com/grafana/xk6-browser/common.(*FrameSession).initEvents.func1()
    github.com/grafana/xk6-browser@v0.1.0/common/frame_session.go:196 +0x3ec
created by github.com/grafana/xk6-browser/common.(*FrameSession).initEvents
    github.com/grafana/xk6-browser@v0.1.0/common/frame_session.go:181 +0xe7
imiric commented 2 years ago

Hi, thanks for reporting this!

Unfortunately I'm not able to reproduce it on current main (baaf58c). Running a slight variation of your script:

import launcher from 'k6/x/browser';

export let options = {
  scenarios: {
    constant_request_rate: {
      executor: 'constant-arrival-rate',
      rate: 1,
      timeUnit: '1000m', // 1000 iterations per second, i.e. 1000 RPS
      duration: '1000m',
      preAllocatedVUs: 500, // how large the initial pool of VUs would be
      maxVUs: 5000, // if the preAllocatedVUs are not enough, we can initialize more
    },
  },
};

export default function () {
  const browser = launcher.launch('chromium', {headless: false, devtools: true,});
  const context = browser.newContext({
    userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/16B92 UCBrowser/12.1.7.1109 Mobile AliApp(TUnionSDK/0.1.20.3)',
    isMobile: true,
    viewport: {width: 600, height: 800}
  });
  const page = context.newPage();
  const resp = page.goto('https://httpbin.test.k6.io/get', {waitUntil: 'load'});
  console.log(resp.json()['headers']['User-Agent']);
  page.close();
  browser.close();
}

Outputs:

INFO[0001] Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/16B92 UCBrowser/12.1.7.1109 Mobile AliApp(TUnionSDK/0.1.20.3)  source=console

As expected. It also doesn't happen for google.com, I just changed the URL so that I could output the User Agent.

Can you reproduce it consistently? What OS are you using? And which xk6-browser version (compiled by yourself or the v0.1.0 binary release)?

I don't think we've seen that stack trace before, so it's definitely an issue we'll look into. It would just help if we could reproduce it consistently.

Thanks!

LaserPhaser commented 2 years ago

@imiric sorry i'm unable to check it on @main cause the problem with build [#121]

LaserPhaser commented 2 years ago

Yep it's stable reproducible OS

sw_vers
ProductName:    macOS
ProductVersion: 11.5.2
BuildVersion:   20G95
xk6-browser@main 
Commit baaf58caef73
xk6 v0.34.1

Check it When I use https it's working fine When I switch to http it cause an error

import launcher from 'k6/x/browser';

export let options = {
  scenarios: {
    constant_request_rate: {
      executor: 'constant-arrival-rate',
      rate: 1,
      timeUnit: '1000m', // 1000 iterations per second, i.e. 1000 RPS
      duration: '1000m',
      preAllocatedVUs: 500, // how large the initial pool of VUs would be
      maxVUs: 5000, // if the preAllocatedVUs are not enough, we can initialize more
    },
  },
};

export default function () {
  const browser = launcher.launch('chromium', {headless: false, devtools: true,});
  const context = browser.newContext({
    userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/16B92 UCBrowser/12.1.7.1109 Mobile AliApp(TUnionSDK/0.1.20.3)',
    isMobile: true,
    viewport: {width: 600, height: 800}
  });
  const page = context.newPage();
  const resp = page.goto('http://google.com', {waitUntil: 'load'});
  console.log(resp.json()['headers']['User-Agent']);
  page.close();
  browser.close();
}
LaserPhaser commented 2 years ago

Oh lol There is a comment in code already) https://github.com/grafana/xk6-browser/blob/main/common/frame_session.go#L416

Working just fine without throwing error ))

imiric commented 2 years ago

Yup, but that's a different issue from yours :) (#120)

I think this one is the same as #126, for which we have a PR in review: #127. If you can build a binary from the fix/126-invalid-memory-frame-currentDocument-request branch and confirm if it fixes your issue, we'd appreciate it.

LaserPhaser commented 2 years ago

I've tried to build the K6 binary: xk6 build --output xk6-browser --with github.com/grafana/xk6-browser@bdda1c92e95b4d6657cd202f038b0516b48e1cb5

As well I've tried to build plugin locally from the branch xk6 build --output k6 --with github.com/grafana/xk6-browser='/Users/untitled/work/grafana/xk6-browser'

But the error still there in both cases:

panic: unable to parse remote object value: cannot extract value when remote object ID is given

goroutine 78 [running]:
go.k6.io/k6/js/common.Throw(0x1d18b80, {0x217d820, 0xc008484fa0})
    go.k6.io/k6@v0.35.0/js/common/util.go:36 +0x55
github.com/grafana/xk6-browser/common.k6Throw({0x219d588, 0xc0082f7380}, {0x1f2c241, 0x27}, {0xc005053cb0, 0x1, 0x1})
    github.com/grafana/xk6-browser@v0.1.1-0.20211118144950-bdda1c92e95b/common/helpers.go:338 +0x1ae
github.com/grafana/xk6-browser/common.(*FrameSession).onConsoleAPICalled(0xc0086b4600, 0xc000cb4730)
    github.com/grafana/xk6-browser@v0.1.1-0.20211118144950-bdda1c92e95b/common/frame_session.go:417 +0x357
github.com/grafana/xk6-browser/common.(*FrameSession).initEvents.func1()
    github.com/grafana/xk6-browser@v0.1.1-0.20211118144950-bdda1c92e95b/common/frame_session.go:207 +0x357
created by github.com/grafana/xk6-browser/common.(*FrameSession).initEvents
    github.com/grafana/xk6-browser@v0.1.1-0.20211118144950-bdda1c92e95b/common/frame_session.go:180 +0xe7
imiric commented 2 years ago

I'm confused... That error is the one from #120, which we're working on fixing. The original stack trace from this issue is a different error, which #127 should resolve. Let's not mix both things.

Can you reproduce this current issue on commit bdda1c92e?

LaserPhaser commented 2 years ago

Oh sorry (looks like i'm blind ) ), it's my fault, no I can't reproduce the original issue. I see only the new one from #120

So seems #127 has been resolved.

imiric commented 2 years ago

127 was merged now, so I'll close this, since it should be fixed with that change as well. Thanks for the feedback!