Skip to content

Race condition on emitting metrics #4203

@ankur22

Description

@ankur22

Brief summary

In very rare cases, the browser module can be emitting a metric when in fact k6 is shutting down. This is mostly reproducible in tests (#4082, and the test in grafana/xk6-browser#1552).

go test -race -count 20 -run ^TestSelectOption$ github.com/grafana/xk6-browser/tests
==================
WARNING: DATA RACE
Write at 0x00c000352470 by goroutine 3934:
  runtime.closechan()
      /home/user/.go/src/runtime/chan.go:397 +0x0
  go.k6.io/k6/cmd.(*cmdRun).run.func11()
      /home/user/go/pkg/mod/go.k6.io/[email protected]/cmd/run.go:274 +0xc5
  runtime.deferreturn()
      /home/user/.go/src/runtime/panic.go:605 +0x5d
  go.k6.io/k6/cmd.(*cmdRun).run-fm()
      <autogenerated>:1 +0x64
  github.com/spf13/cobra.(*Command).execute()
      /home/user/go/pkg/mod/github.com/spf13/[email protected]/command.go:856 +0xbbd
  github.com/spf13/cobra.(*Command).ExecuteC()
      /home/user/go/pkg/mod/github.com/spf13/[email protected]/command.go:974 +0x617
  github.com/spf13/cobra.(*Command).Execute()
      /home/user/go/pkg/mod/github.com/spf13/[email protected]/command.go:902 +0x24c
  go.k6.io/k6/cmd.(*rootCommand).execute()
      /home/user/go/pkg/mod/go.k6.io/[email protected]/cmd/root.go:108 +0x228
  go.k6.io/k6/cmd.ExecuteWithGlobalState()
      /home/user/go/pkg/mod/go.k6.io/[email protected]/cmd/root.go:137 +0x2b
  github.com/grafana/xk6-browser/tests.TestSelectOption()
      /home/user/projects/grafana/k6/xk6-browser/tests/locator_test.go:757 +0x3b1
  testing.tRunner()
      /home/user/.go/src/testing/testing.go:1690 +0x226
  testing.(*T).Run.gowrap1()
      /home/user/.go/src/testing/testing.go:1743 +0x44

Previous read at 0x00c000352470 by goroutine 4009:
  runtime.chansend()
      /home/user/.go/src/runtime/chan.go:171 +0x0
  go.k6.io/k6/metrics.PushIfNotDone()
      /home/user/go/pkg/mod/go.k6.io/[email protected]/metrics/sample.go:135 +0x1106
  github.com/grafana/xk6-browser/common.(*NetworkManager).emitResponseMetrics()
      /home/user/projects/grafana/k6/xk6-browser/common/network_manager.go:266 +0xe48
  github.com/grafana/xk6-browser/common.(*NetworkManager).onLoadingFinished.func1()
      /home/user/projects/grafana/k6/xk6-browser/common/network_manager.go:408 +0x68
  github.com/grafana/xk6-browser/common.(*NetworkManager).onLoadingFinished()
      /home/user/projects/grafana/k6/xk6-browser/common/network_manager.go:412 +0x521
  github.com/grafana/xk6-browser/common.(*NetworkManager).handleEvents()
      /home/user/projects/grafana/k6/xk6-browser/common/network_manager.go:362 +0x184
  github.com/grafana/xk6-browser/common.(*NetworkManager).initEvents.func1()
      /home/user/projects/grafana/k6/xk6-browser/common/network_manager.go:343 +0x38

Goroutine 3934 (running) created at:
  testing.(*T).Run()
      /home/user/.go/src/testing/testing.go:1743 +0x825
  testing.runTests.func1()
      /home/user/.go/src/testing/testing.go:2168 +0x85
  testing.tRunner()
      /home/user/.go/src/testing/testing.go:1690 +0x226
  testing.runTests()
      /home/user/.go/src/testing/testing.go:2166 +0x8be
  testing.(*M).Run()
      /home/user/.go/src/testing/testing.go:2034 +0xf17
  main.main()
      _testmain.go:307 +0x164

Goroutine 4009 (finished) created at:
  github.com/grafana/xk6-browser/common.(*NetworkManager).initEvents()
      /home/user/projects/grafana/k6/xk6-browser/common/network_manager.go:342 +0x2e6
  github.com/grafana/xk6-browser/common.NewNetworkManager()
      /home/user/projects/grafana/k6/xk6-browser/common/network_manager.go:115 +0x597
  github.com/grafana/xk6-browser/common.NewFrameSession()
      /home/user/projects/grafana/k6/xk6-browser/common/frame_session.go:125 +0x5c6
  github.com/grafana/xk6-browser/common.NewPage()
      /home/user/projects/grafana/k6/xk6-browser/common/page.go:284 +0xcaa
  github.com/grafana/xk6-browser/common.(*Browser).onAttachedToTarget()
      /home/user/projects/grafana/k6/xk6-browser/common/browser.go:317 +0x5c8
  github.com/grafana/xk6-browser/common.(*Browser).initEvents.func1()
      /home/user/projects/grafana/k6/xk6-browser/common/browser.go:234 +0x464
==================
--- FAIL: TestSelectOption (0.45s)
    testing.go:1399: race detected during execution of test
FAIL
FAIL    github.com/grafana/xk6-browser/tests    9.212s
FAIL

xk6-browser version

v1.9.1

OS

NA

Chrome version

NA

Docker version and image (if applicable)

No response

Steps to reproduce the problem

Run the test in grafana/xk6-browser#1552 with the -race flag.

Expected behaviour

Test passes with no errors.

Actual behaviour

A race condition was found.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions