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 races around touching state.Group outside of the event loop #589

Merged
merged 1 commit into from
Oct 17, 2022

Conversation

mstoykov
Copy link
Contributor

@mstoykov mstoykov commented Oct 13, 2022

There is no locking around the state.Group and it is modified by calls to group() while on the event loop. So for the time being to modify it - it is needed to be on the event loop.

Also the adding of the tag isn't needed as that is done automatically by group()

There is no locking around the `state.Group` and it is modified by calls
to `group()` while on the event loop. So for the time being to modify it
- it is needed to be on the event loop.

Also the adding of the tag isn't needed as that is done automatically by
`group()`
@mstoykov mstoykov added this to the v0.6.0 milestone Oct 13, 2022
@mstoykov
Copy link
Contributor Author

found while testing for #542

common/frame_session.go Show resolved Hide resolved
common/frame_session.go Show resolved Hide resolved
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.

LGTM, but I believe we need a new issue to add Group support back in the xk6-browser.

@mstoykov mstoykov merged commit a94a165 into main Oct 17, 2022
@mstoykov mstoykov deleted the fixRacesAroundGroup branch October 17, 2022 14:03
@mstoykov
Copy link
Contributor Author

a new issue to add Group support back in the xk6-browser.

what do you mean?

the tag group is still added - the code that is removed here didn't need to exist.

Do you want an issue to make the logging of group to be brought back for the two cases where it was removed?

@inancgumus
Copy link
Member

inancgumus commented Oct 17, 2022

Do you want an issue to make the logging of group to be brought back for the two cases where it was removed?

Never mind, I confused it with something else. The PR is good as it is 👍

@imiric
Copy link
Contributor

imiric commented Oct 17, 2022

Hey, I'm just getting around to reviewing this, apologies for the delay.

I'm confused about this:

Also the adding of the tag isn't needed as that is done automatically by group()

group() only adds the tag for the group_duration metric, but not for the metrics we're emitting here (data_sent), nor for the logger fields we're adding.

I see that you created grafana/k6#2728, which is probably why I wasn't able to make it work just now testing on the main commit before this was merged (a23053d), but it brings up the question of how can we make this work for custom metrics, like browser_dom_content_loaded, which didn't even access state.Group. We should figure out a way to make this work for all cases, so 👍 on adding an issue for it.

It seems unlikely that group() will be deprecated anytime soon, and there are some non-trivial API changes needed to support these async scripts, as that issue mentions. 😅

@mstoykov
Copy link
Contributor Author

mstoykov commented Oct 17, 2022

group() only adds the tag for the group_duration metric

https://github.com/grafana/k6/blob/0568f273b157829d94b7869cd495025eadd9f2bb/js/modules/k6/k6.go#L108-L111

It does add them to the state.Tags from which you fork the tags and then emit the metric. So if you are within a group and you are not hitting 2728 - you don't need the code I have removed. If you are hitting 2728 - the code I removed again doesn't help you ;)

So the answer to what should xk6-browser team do is - nothing ;)

It seems unlikely that group() will be deprecated anytime soon, and there are some non-trivial API changes needed to support these async scripts, as that issue mentions. sweat_smile

I mean ... "unlikely" yes, but depending on what the fixes for 2728 turn out to be, it might turn out that it makes more sense than anything else.

In general I would argue that when emitting metrics asyncrhonously you should note/save the tags from the lib.State when you start the asynchrnous call while still on the event loop. And then use that. This also will work with single level group ... still won't work if you have a then afterwards.

group("some group", () => {
  someAsyncCall(). // if before someAsyncCall returns a promise it gets the `state.Tags` - it will be fine
   then(() => {
    // anything here will not run in the "context" of the group
    // this is what 2728 is about.
   })
});

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.

4 participants