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

Fix request error handling to avoid segmentation violation #99

Merged
merged 2 commits into from Jan 16, 2018

Conversation

Projects
None yet
2 participants
@donce
Copy link
Contributor

commented Jan 15, 2018

In case request fails before reaching server (i.e. timeout) and no response is generated, such error occurs:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x43503e9]

goroutine 37 [running]:
github.com/mysterium/node/server.parseResponseError(0x0, 0x1, 0x1)
	/Users/donce/go/src/github.com/mysterium/node/server/response.go:12 +0x29
github.com/mysterium/node/server.(*mysteriumApi).doRequest(0xc420126500, 0xc4200b2300, 0x4469f00, 0xc4301cdec0)
	/Users/donce/go/src/github.com/mysterium/node/server/mysterium_api.go:127 +0x1a2
github.com/mysterium/node/server.(*mysteriumApi).NodeSendStats(0xc420126500, 0xc4200833e0, 0x2a, 0x47091a0, 0xc43017c0a0, 0xc42014e300, 0xc42013a030)
	/Users/donce/go/src/github.com/mysterium/node/server/mysterium_api.go:77 +0x12f
github.com/mysterium/node/cmd/mysterium_server/command_run.(*CommandRun).Run.func1(0xc42009cd90, 0xc4200833e0, 0x2a, 0x47091a0, 0xc43017c0a0)
	/Users/donce/go/src/github.com/mysterium/node/cmd/mysterium_server/command_run/command.go:78 +0x6f
created by github.com/mysterium/node/cmd/mysterium_server/command_run.(*CommandRun).Run
	/Users/donce/go/src/github.com/mysterium/node/cmd/mysterium_server/command_run/command.go:75 +0x6de
@@ -77,6 +77,8 @@ func (mApi *mysteriumApi) NodeSendStats(nodeKey string, signer identity.Signer)
err = mApi.doRequest(req)
if err == nil {
log.Info(mysteriumApiLogPrefix, "Node stats sent: ", nodeKey)
} else {
log.Error(mysteriumApiLogPrefix, "Node stats failed: ", err)

This comment has been minimized.

Copy link
@Waldz

Waldz Jan 15, 2018

Member

Before client_rest.go had common error loging, which was removed during refactoring.
Can You return it back:

func (client *mysteriumRestClient) executeRequest(method, fullPath string, payloadJson []byte) (*http.Response, error) {
	request, err := http.NewRequest(method, fullPath, bytes.NewBuffer(payloadJson))
	request.Header.Set("User-Agent", MYSTERIUM_API_CLIENT)
	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("Accept", "application/json")
	if err != nil {
		log.Critical(MYSTERIUM_API_LOG_PREFIX, err)
		return nil, err
	}

	response, err := client.http.Do(request)

	if err != nil {
		log.Error(MYSTERIUM_API_LOG_PREFIX, err)
		return response, err
	}

	err = parseResponseError(response)
	if err != nil {
		log.Error(MYSTERIUM_API_LOG_PREFIX, err)
		return response, err
	}

	return response, nil
}

This comment has been minimized.

Copy link
@donce

donce Jan 15, 2018

Author Contributor

Done.

}
defer resp.Body.Close()
return parseResponseError(resp)

This comment has been minimized.

Copy link
@Waldz

Waldz Jan 15, 2018

Member

What if parsing fails

This comment has been minimized.

Copy link
@donce

donce Jan 16, 2018

Author Contributor

This doesn't parse body - it just checks status code and if it's failure, returns error.

@Waldz

Waldz approved these changes Jan 16, 2018

@Waldz Waldz merged commit 7e6750f into master Jan 16, 2018

@Waldz Waldz deleted the hotfix/fix-request-error-handling branch Jan 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.