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

net/http: Client.Do hangs when called from a wasm function presented to a JS event in a web browser #43298

Closed
VinceJnz opened this issue Dec 21, 2020 · 1 comment
Labels
arch-wasm WebAssembly issues FrozenDueToAge

Comments

@VinceJnz
Copy link

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

$ go version
go version go1.15.6 windows/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
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=js
set GOARCH=wasm

What did you do?

index.html
< html>
	< head>
		< meta charset="utf-8"/>
		< script src="/wasm/wasm_exec.js">< /script>
		< script>
			const go = new Go();
			WebAssembly.instantiateStreaming(fetch("/wasm/main.wasm"), go.importObject).then((result) => {
				go.run(result.instance);
			});
		< /script>
		< script>
			function myEvent(event) {
				var target = document.getElementById("result2")
				var data = GetRecord(1)
				target.innerHTML = data //Display the data in the browser
			}
		< /script>
	< /head>
	< body>wasm test
		< div id="wasm1">< /div>
		< div>< b>result1=< /b>< /div>< div id="result1">put the first record here< /div>
		< div>< b>result2=< /b>< /div>< div id="result2">put the second record here< /div>
	< /body>
< /html>

main.go https://play.golang.org/p/idrXUU7hxkD

What did you expect to see?

When the wasm starts up it sends a request to a rest api and the resulting record is displayed in the browser.
When the myEvent() function is triggered it calls the GetRecord function in the wasm and this should display the same record in the browser.

What did you see instead?

When the wasm starts up it the correct result is displayed.
When the myEvent() function is triggered it sends a request to the rest api. The rest api responds but the http.Client.Do function appears to hang. It eventually times out, the record is never received and the error message "context deadline exceeded" is displayed in the browser.

@mdempsky mdempsky changed the title http.Client.Do hangs when called from a wasm function presented to a JS event in a web browser net/http: Client.Do hangs when called from a wasm function presented to a JS event in a web browser Dec 22, 2020
@VinceJnz
Copy link
Author

I might have been a bit hasty logging this as a bug.
After further investigation and lots of reading the problem is caused by JavaScript's event loop being blocked.

This is documented in the following golang documentation
https://golang.org/pkg/syscall/js/#FuncOf

It is also nicely explained in this blog by Alessandro Segala (@ItalyPaleAle) under the heading "Async JS with Promises from Go"
https://withblue.ink/2020/10/03/go-webassembly-http-requests-and-promises.html

@dmitshur dmitshur added the arch-wasm WebAssembly issues label Mar 28, 2021
@golang golang locked and limited conversation to collaborators Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly issues FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants