http.Client doesn't work in Go compiled to wasm

Issue

I want to use http.Client for a simple HTTP GET with same X-Header, but an exception occurs after println("Start: getValues (client.Do)").

The error occurs only if the code is compiled to wasm.

I need same help to understand the error.

Code

println("Start: getValues")
client := &http.Client{

}

req, err := http.NewRequest("GET", "https://abc.azurewebsites.net/api/Value", nil)
if err != nil {
    log.Println(err.Error())
    return nil
}

req.Header.Add("X-ApiKey", `ABC`)
req.Header.Add("X-System", `ABC`)
req.Header.Add("X-User", `ABC`)

println("Start: getValues (client.Do)")
// EXCEPTION !!!
resp, err := client.Do(req)
if err != nil {
    return nil
}

println("Start: getValues (ReadAll)")
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
    return nil
}

Browser Logging

wasm_exec.js:45 Hello World!
wasm_exec.js:45 WASM Go Initialized
wasm_exec.js:45 
wasm_exec.js:45 Start: GetData
wasm_exec.js:45 Start: getValues
wasm_exec.js:45 Start: getValues (client.Do)
wasm_exec.js:45 fatal error: all goroutines are asleep - deadlock!
wasm_exec.js:45 
wasm_exec.js:45 goroutine 1 [chan receive]:
wasm_exec.js:45 main.main()
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:43 +0xa
wasm_exec.js:45 
wasm_exec.js:45 goroutine 5 [select]:
wasm_exec.js:45 net/http.(*Transport).RoundTrip(0x36b100, 0xc0ba000, 0x0, 0x0, 0x0)
wasm_exec.js:45     C:/Go/src/net/http/roundtrip_js.go:151 +0x44
wasm_exec.js:45 net/http.send(0xc0ba000, 0xbdc80, 0x36b100, 0x0, 0x0, 0x0, 0xc00c068, 0x22, 0xc028de0, 0x1)
wasm_exec.js:45     C:/Go/src/net/http/client.go:250 +0x16
wasm_exec.js:45 net/http.(*Client).send(0xc028d80, 0xc0ba000, 0x0, 0x0, 0x0, 0xc00c068, 0x0, 0x1, 0x40000000000)
wasm_exec.js:45     C:/Go/src/net/http/client.go:174 +0x10
wasm_exec.js:45 net/http.(*Client).do(0xc028d80, 0xc0ba000, 0x0, 0x0, 0x0)
wasm_exec.js:45     C:/Go/src/net/http/client.go:641 +0x17
wasm_exec.js:45 net/http.(*Client).Do(0xc028d80, 0xc0ba000, 0x6, 0x89f48, 0x24)
wasm_exec.js:45     C:/Go/src/net/http/client.go:509 +0x2
wasm_exec.js:45 main.getValues(0x84392, 0x12, 0x0, 0x10b0003d, 0xc048150)
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:124 +0x10
wasm_exec.js:45 main.GetData(0x7ff800000000001f, 0x83468)
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:53 +0x5
wasm_exec.js:45 main.getValue(0xc040240, 0x3, 0x3)
wasm_exec.js:45     C:/Git/GoWasmExample/GoWasmExample.go:161 +0x1d
wasm_exec.js:45 syscall/js.callbackLoop()
wasm_exec.js:45     C:/Go/src/syscall/js/callback.go:116 +0x7
wasm_exec.js:45 created by syscall/js.NewCallback.func1
wasm_exec.js:45     C:/Go/src/syscall/js/callback.go:40 +0x2
wasm_exec.js:67 exit code: 2
exit @ wasm_exec.js:67
runtime.wasmExit @ wasm_exec.js:185
wasm-function[939]
wasm-function[928]
wasm-function[857]
wasm-function[883]
wasm-function[472]
wasm-function[470]
wasm-function[591]
wasm-function[594]
wasm-function[546]
wasm-function[555]
wasm-function[559]
wasm-function[561]
wasm-function[882]
wasm-function[499]
wasm-function[637]
wasm-function[4291]
wasm-function[926]
run @ wasm_exec.js:383
wasm_exec.js:378 Uncaught (in promise) Error: bad callback: Go program has already exited
    at global.Go._resolveCallbackPromise (wasm_exec.js:378)
    at wasm_exec.js:394
_resolveCallbackPromise @ wasm_exec.js:378
(anonymous) @ wasm_exec.js:394

Solution

So it seems like the http call (in wasm) is blocking the event thread in my main js app, hence the deadlock. Running the http request in a go routine fixed it for me.

https://github.com/golang/go/issues/26382#issuecomment-416926829

Answered By – Peter

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.