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

runtime: deadlock on blocking call #39928

Closed
pigfox opened this issue Jun 29, 2020 · 4 comments
Closed

runtime: deadlock on blocking call #39928

pigfox opened this issue Jun 29, 2020 · 4 comments

Comments

@pigfox
Copy link

@pigfox pigfox commented Jun 29, 2020

This happens when compiling Go code into wasm with:
GOOS=js GOARCH=wasm go build -o ./Go/assets/main.wasm wasm.go

What version of Go are you using (go version)?

$ go version
go1.14.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/peter/.cache/go-build"
GOENV="/home/peter/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/usr/local/go:/home/peter/work"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/5830"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/5830/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build651075817=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Attempted to make a request from compiled Wasm code.

func query(this js.Value, args []js.Value) interface{} {
	var rv interface{}

	c1 := make(chan string)
	go func() {
		c1 <- makeRequest()
	}()

	target:= fmt.Sprintf("%s", args[0]) + "Result"

	select {
	case color := <-c1:
		js.Global().Get("document").Call("getElementById", target).Set("innerHTML", color)
	}
	return rv
}

What did you expect to see?

A string in a HTML element.

What did you see instead?

fatal error: all goroutines are asleep - deadlock!
wasm_exec.js:47
wasm_exec.js:47 goroutine 1 [select (no cases)]:

@dmitshur dmitshur added this to the Backlog milestone Jun 30, 2020
@dmitshur dmitshur changed the title fatal error: all goroutines are asleep - deadlock! runtime: deadlock on blocking call Jun 30, 2020
@dmitshur
Copy link
Member

@dmitshur dmitshur commented Jun 30, 2020

Thanks for reporting. This may be a problem in the code rather than the Go runtime. In order to investigate, we'll need to know how query being called. Can you provide more information on that please?

@neelance Is there a restriction on where blocking calls can be made (e.g., inside callbacks), and do you know if we've documented it somewhere? I'm not finding it in https://golang.org/wiki/WebAssembly.

@agnivade
Copy link
Contributor

@agnivade agnivade commented Jun 30, 2020

It is documented here: https://golang.org/pkg/syscall/js/#FuncOf.

@pigfox
Copy link
Author

@pigfox pigfox commented Jun 30, 2020

@dmitshur You can clone the repo: https://github.com/pigfox/wasm and view the error.

@dmitshur
Copy link
Member

@dmitshur dmitshur commented Jun 30, 2020

Yep, that code is making query the callback handler by setting an element's onclick property:

onclick="query('colors');

The deadlock is working as intended per documentation at https://golang.org/pkg/syscall/js/#FuncOf. (Thanks @agnivade.) To avoid the deadlock, you'll need to follow the suggestion of ensuring the callback itself is non-blocking.

@dmitshur dmitshur closed this Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.