Meshbits / Shurli

Shurli App - Proof of Concept App for DEXP2P
MIT License
11 stars 2 forks source link

[Bug] panic: runtime error: invalid memory address or nil pointer dereference #25

Closed satindergrewal closed 4 years ago

satindergrewal commented 4 years ago

If any coin's RPC Is busy and fails to respond with RPC query, gRPC calls throws pointer dereference errors and exit the process.

WalletInfo function was invoked with 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x13274c5]

goroutine 114 [running]:
github.com/satindergrewal/kmdgo.AppType.APICall(0xc0000a2310, 0x6, 0xc000724ef8, 0xc000402cf8, 0x0)
        /Users/satinder/go/pkg/mod/github.com/satindergrewal/kmdgo@v0.0.0-20200626223543-4915188bede5/common.go:134 +0x625
github.com/satindergrewal/kmdgo.AppType.GetInfo(0xc0000a2310, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /Users/satinder/go/pkg/mod/github.com/satindergrewal/kmdgo@v0.0.0-20200626223543-4915188bede5/control.go:71 +0xec
github.com/Meshbits/shurli/sagoutil.WalletInfo(0xc00043e080, 0x6, 0x8, 0xc00043e080, 0x6, 0x8)
        /Users/satinder/go/src/github.com/Meshbits/shurli/sagoutil/sago_lib.go:80 +0x281
main.(*server).WalletInfo(0x1b149c0, 0x170c860, 0xc0002ae3c0, 0xc000176140, 0x1b149c0, 0xc0002ae3c0, 0xc00036dba0)
        /Users/satinder/go/src/github.com/Meshbits/shurli/shurli_grpc/shurli_server/server.go:30 +0x11c
github.com/Meshbits/shurli/shurli_grpc/shurlipb._ShurliService_WalletInfo_Handler(0x158aec0, 0x1b149c0, 0x170c860, 0xc0002ae3c0, 0xc00040c2a0, 0x0, 0x170c860, 0xc0002ae3c0, 0x0, 0x0)
        /Users/satinder/go/src/github.com/Meshbits/shurli/shurli_grpc/shurlipb/shurli.pb.go:693 +0x217
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0000a5d40, 0x1712940, 0xc000001680, 0xc0002d6000, 0xc000220e10, 0x1ad8580, 0x0, 0x0, 0x0)
        /Users/satinder/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1082 +0x50a
google.golang.org/grpc.(*Server).handleStream(0xc0000a5d40, 0x1712940, 0xc000001680, 0xc0002d6000, 0x0)
        /Users/satinder/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1405 +0xccb
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc000402020, 0xc0000a5d40, 0x1712940, 0xc000001680, 0xc0002d6000)
        /Users/satinder/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:746 +0xa1
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /Users/satinder/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:744 +0xa1
exit status 2
satindergrewal commented 4 years ago

Recieved the same error while testing Shurli on Windows. Had DEX chain started, and PIRATE stopped, and following error was printed on console output of shurli if ran with shurli.exe main:

2020/07/05 00:19:02 http: panic serving 127.0.0.1:50152: runtime error: invalid memory address or nil pointer dereference
goroutine 208 [running]:
net/http.(*conn).serve.func1(0xc0004fad20)
        /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:1772 +0x140
panic(0x8566e0, 0xc4c960)
        /usr/local/Cellar/go/1.14.4/libexec/src/runtime/panic.go:975 +0x3f1
github.com/satindergrewal/kmdgo.AppType.APICall(0xc00045b868, 0x6, 0xc0001d0e18, 0xc00045bc28, 0x0)
        /Users/satinder/go/src/github.com/satindergrewal/kmdgo/common.go:134 +0x62c
github.com/satindergrewal/kmdgo.AppType.GetInfo(0xc00045b868, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        /Users/satinder/go/src/github.com/satindergrewal/kmdgo/control.go:71 +0xf3
github.com/Meshbits/shurli/sagoutil.WalletInfo(0xc00008ee00, 0x5, 0x8, 0xc00008ee00, 0x5, 0x8)
        /Users/satinder/go/src/github.com/Meshbits/shurli/sagoutil/sago_lib.go:80 +0x288
main.idx(0x980b40, 0xc00047e700, 0xc000133a00)
        /Users/satinder/go/src/github.com/Meshbits/shurli/main.go:296 +0xa9
net/http.HandlerFunc.ServeHTTP(0x8fc0b8, 0x980b40, 0xc00047e700, 0xc000133a00)
        /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2012 +0x4b
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000282000, 0x980b40, 0xc00047e700, 0xc000133800)
        /Users/satinder/go/src/github.com/gorilla/mux/mux.go:210 +0xe9
net/http.serverHandler.ServeHTTP(0xc00029e0e0, 0x980b40, 0xc00047e700, 0xc000133800)
        /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2807 +0xaa
net/http.(*conn).serve(0xc0004fad20, 0x981440, 0xc000095a40)
        /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:1895 +0x873
created by net/http.(*Server).Serve
        /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2933 +0x363

Due to this error, the web UI was failing to load. Just starting back PIRATE, even if it's in loading state, the web UI responded fine.

satindergrewal commented 4 years ago

To replicate, I just start import process of address in komodod with importprivkey command and then test by executing example file getinfo.go from kmdgo package which just queries the komodo-cli getinfo API. And as it is busy and doesn't respond while importing and doing full rescan of blockchain, it throws the following output for invalid memory address error:

➜  examples git:(master) ✗ go run getinfo.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x1283f15]

goroutine 1 [running]:
github.com/satindergrewal/kmdgo.AppType.APICall(0x131945e, 0x6, 0xc00014f958, 0x8905c00, 0x20300000000000)
    /Users/satinder/go/src/github.com/satindergrewal/kmdgo/common.go:135 +0x625
github.com/satindergrewal/kmdgo.AppType.GetInfo(0x131945e, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
    /Users/satinder/go/src/github.com/satindergrewal/kmdgo/control.go:71 +0xec
main.main()
    /Users/satinder/go/src/github.com/satindergrewal/kmdgo/examples/getinfo.go:51 +0xac
exit status 2
➜  examples git:(master) ✗

I found that I have not applied proper error check for API response body in common.go line 134: https://github.com/satindergrewal/kmdgo/blob/master/common.go#L134

bodyText, err := ioutil.ReadAll(resp.Body)

This causing the issue. Working on its fix now.

satindergrewal commented 4 years ago

This link helped with the error: https://stackoverflow.com/questions/53926818/invalid-memory-address-in-golang-using-ioutil-readall

Fixed issue by extending error handling for:

resp, err := client.Do(req)

Added error handling for context deadline exceeded and also adding any other error handling to end the code execution further. In my current tests it fixed the issue with this git commit: https://github.com/satindergrewal/kmdgo/commit/22e72f9aa7caa20bb80f276a24968f8036175236

Can report back if the same issue arise again somehow.