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

SharedArray does not work with async functions #3014

Open
mstoykov opened this issue Apr 6, 2023 · 2 comments
Open

SharedArray does not work with async functions #3014

mstoykov opened this issue Apr 6, 2023 · 2 comments
Assignees
Labels
evaluation needed proposal needs to be validated or tested before fully implementing it in k6 js-compat ux

Comments

@mstoykov
Copy link
Contributor

mstoykov commented Apr 6, 2023

import {SharedArray} from "k6/data";
let s = new SharedArray("cool", async() => {
  return [1, 2, 3];
})
export default function  () {
  console.log(s);
}

Will give you

ERRO[0000] GoError: only arrays can be made into SharedArray

But it likely should work.

There is currently no real reason to use async code withing SharedArray, but it might based on #2043 and specifically thsi comment.

Note: this does mean that SharedArray will also have to return a promise or have an async variant

@mstoykov mstoykov added ux js-compat evaluation needed proposal needs to be validated or tested before fully implementing it in k6 labels Apr 6, 2023
@mstoykov mstoykov self-assigned this Apr 25, 2023
@mstoykov mstoykov added this to the v0.45.0 milestone Apr 26, 2023
mstoykov added a commit that referenced this issue May 30, 2023
This is mostly to prevent users from using it for now.

updates #3014
mstoykov added a commit that referenced this issue Jun 1, 2023
This is mostly to prevent users from using it for now.

Also move isAsyncFunction to js/common in order to reuse it

updates #3014 


Co-authored-by: Ivan Mirić <[email protected]>
@mstoykov mstoykov modified the milestones: v0.45.0, TBD Jun 5, 2023
@codebien codebien removed this from the TBD milestone Sep 27, 2023
@mstoykov mstoykov removed their assignment Jan 10, 2024
@JeffBNimble
Copy link

JeffBNimble commented Jun 24, 2024

I have another use case that would make support of an async function for SharedArray useable for me. We are using the webcrypto experimental package to generate a shared array of "user" objects, each with a uuid identifier and a signed JWT token. I'm unable to generate/sign a JWT as part of the SharedArray loader function because the webcrypto subtle.sign() function is async and I cannot call this within the loader function.

If the SharedArray loader function allowed an async function, then this would be easily doable. Because of this, I cannot use a SharedArray to populate anything that the crypto.subtle.sign() function produces or call any other functions that are async.

What this leaves me having to do is to return the generated "users" as a response to the setup function and accept the generated "users" as an argument to the default function, which means that I have a fresh copy of this potentially large set of users for each VU.

@mstoykov
Copy link
Contributor Author

Hi @JeffBNimble, thanks for the input - this does seem like a good case for this functionality.

If you want you can work on this, but I will recommend making a new AsyncSharedArray - that always returns a promise.

As part of #3265 we will be getting top-level-await which is arguably currently a big UX problem for anything that uses async in the top.

Given the recent goja fork and the ongoing work on ESM - I don't think I will be able to work on it. It also happens to be vacation season, so we already have lesser capacity within the team :(. So no ETA on when we will be able to work on this.

My hacky workaround for now will be to abuse __VU as in

import { SharedArray } from "k6/data";
let data;
if (__VU == 0) { // first init context (among other things)
	(async () => {
		// generate what you need
		let generated_data = await [1, 2, 3, 4, 5]

		new SharedArray("coolname", () => { return generated_data; })
	})()
} else {
	data = new SharedArray("coolname", () => { throw "this will never be executed as shared array was already defined" })
}

export default () => {
	console.log(data[1]);
}

As you can see you need to quite a bunch of stuff in order to use async/await in the top level currently.

You might also want to look into using the old k6/crypto if that works for your use case https://gist.github.com/robingustafsson/7dd6463d85efdddbb0e4bcd3ecc706e1

@mstoykov mstoykov removed their assignment Jun 25, 2024
@mstoykov mstoykov removed the triage label Jun 25, 2024
@joanlopez joanlopez self-assigned this Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
evaluation needed proposal needs to be validated or tested before fully implementing it in k6 js-compat ux
Projects
None yet
Development

No branches or pull requests

4 participants