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
syscall/js: deadlock occurred with http.Get #25902
Comments
I got some more error at the bottom while trying to reproduce this -
/cc @neelance , @johanbrandhorst |
This is a nonsensical example, no? Add a waitgroup or something to monitor the progress of the callback before exiting. |
I've investigated this a bit more, and indeed, I think there is a problem. Modifying the code like so still exhibits the same problem: package main
import (
"fmt"
"net/http"
"syscall/js"
)
func main() {
var cb js.Callback
done := make(chan struct{})
cb = js.NewCallback(func(args []js.Value) {
fmt.Println("Clicked")
resp, err := http.Get("/")
fmt.Println(resp, err)
close(done)
})
js.Global.Get("document").Call("getElementById", "btn").Call("addEventListener", "click", cb)
<-done
} The problem is that, indeed, all goroutines are asleep (while waiting for Fetch I/O). I'm not sure how to solve this so will defer to @neelance. |
The problem is that you are blocking the callback loop. New calls to callbacks (like the HTTP response) can not be processed while some other callback handler (like the click handler) have not returned. You need to explicitly start a new goroutine: package main
import (
"fmt"
"net/http"
"syscall/js"
)
func main() {
var cb js.Callback
done := make(chan struct{})
cb = js.NewCallback(func(args []js.Value) {
go func() {
fmt.Println("Clicked")
resp, err := http.Get("/")
fmt.Println(resp, err)
close(done)
}()
})
js.Global.Get("document").Call("getElementById", "btn").Call("addEventListener", "click", cb)
<-done
} |
Ah I see. Makes sense now. It was not immediately apparent that the callback loop is being blocked. I should have read the documentation first - https://tip.golang.org/pkg/syscall/js/?GOOS=js&GOARCH=wasm#NewCallback
@neelance - Feel free to close the bug if you don't think there is anything else to be done. |
Can't close the bug since it is not assigned to me. |
Thank you for comments. |
I understand. Unfortunately this is working as intended and documented explicitly. So I don't think much can be done except educate the users. Maybe in future, this behavior can change. But until then, it is unlikely we will be able to do anything. |
The only idea that I have to improve this is to rename |
Kinda goes against the set of callbacks we have - |
I was wondering how the order of callbacks matters. |
There are several JS APIs that depend on it. Usually JS devs (me included when writing JS) don't spend much thought on this since it is "simply" a property of the execution model, so generally assumed (JS devs also don't think about issues due to preemption, because there is none). See https://go-review.googlesource.com/c/go/+/114197/6/src/syscall/js/callback.go#52 and https://go-review.googlesource.com/c/go/+/114197/8/src/syscall/js/callback.go#57. |
I probably not understand what is problem. Can we add function that wait all callbacks?
|
@mattn Now I don't get what you are saying... |
@neelance I mean that I wonder that Go really can't solve this. For example, I don't make sure, is it possible to make channel/goroutine in Go instead of user code? |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
It has occurred with 4eb1c84.
What operating system and processor architecture are you using (
go env
)?What did you do?
I built this code with
GOOS=js GOARCH=wasm
.And I ran it on Chrome using
misc/wasm/wasm_exec.js
andmisc/wasm/wasm_exec.html
.In addition, I added a button to
wasm_exec.html
.It occurred deadlock.
I tried to remove
select{}
but it immediately exit and it didn't kick the callback.What did you expect to see?
Any errors do not occur with a click event and http.Get.
What did you see instead?
It occurred an error.
The text was updated successfully, but these errors were encountered: