http.客户端无法在编译为wasm的Go语言中工作

a8jjtwal  于 2022-12-07  发布在  Go
关注(0)|答案(2)|浏览(180)

我想使用http.Client执行一个简单的HTTPGET,但在println("Start: getValues (client.Do)")之后发生了异常。

在将代码编译为wasm时才会发生此错误。

我需要同样的帮助来理解错误。

代码

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
}

浏览器日志记录

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
o0lyfsai

o0lyfsai1#

所以看起来好像是http调用(在wasm中)阻塞了我的js主应用程序中的事件线程,因此出现了死锁。在go例程中运行http请求为我解决了这个问题。
https://github.com/golang/go/issues/26382#issuecomment-416926829

相关问题