-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
runtime: wasm: fatal error: all goroutines are asleep - deadlock! #41310
Comments
This is documented here: https://golang.org/pkg/syscall/js/#FuncOf
You would need to perform the |
Thanks @agnivade for clarification. |
@agnivade I am doing http.Get in seperate routine but still getting error. I am creating a channel before invoking go routine to perform http.Get and writing on it from inside the go routine and reading from channel later down the function. Is this also a limitation? |
Yes, that does not change anything if you read from the channel in the same function. It will still cause the deadlock. You have to create a separate event handler loop or some other mechanism to make http requests and read responses. |
I'm new to Go so would appreciate any thoughts on my scenario. I'm not doing the GET directly, but have compiled go-git to wasm and am calling its So my question is about ways to solve this when not doing the fetches directly, but when they are happening in another package (which would not expect to be used in the browser). Can I solve this without re-writing the relevant parts of go-git? If not I guess I'll look into modifying go-git to use my own "fetch" package ("in another event loop" whatever that means 😄), but am wondering if you can offer any advice to someone new to Go. Thanks. |
You can try wrapping repo.Clone() call in a seperate go routine. |
Thanks. I'm trying the following, but maybe it can only work if This is in a funciton called via the wasm go bridge so I can't put it in a playground:
Or might the problem remain because this is (I think) a nested goroutine? |
I think I have solved this, the issue remained because I was waiting on the result so if I remove the WaitGroup altogether and just create the goroutine as it, it doesn't exit as in:
It doesn't clone the repo, but that's a different issue. Thanks for your help. |
You're cloning in memory but not using it's return value so that might be issue. Try doing
And this repo variable will contain cloned repo. And again you won't be able to wait for go routine func Clone() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
// Handler for the Promise: this is a JS function
// It receives two arguments, which are JS functions themselves: resolve and reject
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
// Commented out because this Promise never fails
//reject := args[1]
// Now that we have a way to return the response to JS, spawn a goroutine
// This way, we don't block the event loop and avoid a deadlock
go func() {
r, err := repo.Clone()
// Resolve the Promise, passing anything back to JavaScript
// This is done by invoking the "resolve" function passed to the handler
resolve.Invoke(r)
}()
// The handler of a Promise doesn't return any value
return nil
})
// Create and return the Promise object
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
})
} To invoke from JavaScript async function MyFunc() {
// Get the Promise from Go
const p = Clone()
// Await for the Promise to resolve
const repo = await p
// Use repo response
} |
I'm pretty sure the remaining issue is because I'm doing this in the browser, so the server issues a 404 (after initially responding as desired), so it isn't a concern for this thread. It's also not vital for my use case, more for testing at least for now so I'm happy! And thanks for your help. Go has a great community and I've had excellent help whenever I get stuck. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Call Go function from js(from chrome dev tools console)
What did you expect to see?
Function running succesfully
What did you see instead?
The text was updated successfully, but these errors were encountered: