-
Notifications
You must be signed in to change notification settings - Fork 112
cmd/swamr-smoke: fix waitToPushSynced connection closing #1781
Conversation
cmd/swarm-smoke/upload_and_sync.go
Outdated
@@ -334,9 +334,6 @@ func waitToPushSynced(tagname string) { | |||
time.Sleep(200 * time.Millisecond) | |||
|
|||
rpcClient, err := rpc.Dial(wsEndpoint(hosts[0])) | |||
if rpcClient != nil { | |||
defer rpcClient.Close() | |||
} | |||
if err != nil { | |||
log.Error("error dialing host", "err", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@janos what about this err
? I am not sure rpcClient
is guaranteed to be nil
within this error.
I think the simplest solution is to keep the defer, but add a scope with a func() {} block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should't this error mean that tpcClient is nil? At least that would be an expected behaviour to me. And it looks like that this is the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@janos I checked rpc.Dial
but the stack is too long. As a comparison, the logic in rpc/http.go is the following:
respBody, err := hc.doRequest(ctx, msg)
if respBody != nil {
defer respBody.Close()
}
if err != nil {
I am not saying it is the same thing, but generally it is a good practice to always call Close()
if the client is not nil, no matter the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In src/net/http/client.go
they also say:
// When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
They don't give any guarantees on what happens if err != nil
, and because of that the code I've seen at most projects, check for the return value and always calls Close()
irrespective of err
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, lets be safe, but I think that if this kind of handling is expected from doRequest and Dial, it is a bad design.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nonsense, I thin that closing is not what the comment on http.Client is suggesting. It states that it is safe to ensure that it is not nil, not that it must be closed regardless of error value. All examples and documentation https://golang.org/pkg/net/http/ for that package defers closing after the error check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@janos i've read posts that state exactly the opposite - that clients should be closed regardless of the error value. i don't have hard feelings about this here, just wanted to raise it up from an engineering perspective as i found it interesting when i read it :) will try to find the blog post and share it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sure that if that is the case, net/http documentation would suggest it or at least there would be issues raised on golang/go repo. On the other side, if client.Do returns the error, no connection was made, so closing the body in order to close the connection is not needed. This should be also easily checked with a test.
* 'master' of github.com:ethersphere/swarm: (32 commits) network/stream: refactor cursors tests (ethersphere#1786) network: Add capabilities if peer from store does not have it (ethersphere#1791) Swap logger (ethersphere#1754) network: Add capability filtered depth calculation (ethersphere#1787) travis: remove go1.12 job (ethersphere#1784) cmd/swarm: correct bzznetworkid flag description (ethersphere#1761) network, pss: Capability in pss (ethersphere#1764) network/stream: handle nil peer in TestNodesExchangeCorrectBinIndexes (ethersphere#1779) protocols, retrieval: swap-enabled messages implement Price (ethersphere#1771) cmd/swarm-smoke: fix waitToPushSynced connection closing (ethersphere#1781) cmd/swarm: simplify testCluster.StartNewNodes (ethersphere#1777) build: increase golangci-lint deadline (ethersphere#1778) docker: ignore build/bin when copying files (ethersphere#1780) swap: fix and rename Peer.getLastSentCumulativePayout (ethersphere#1769) network/stream: more resilient TestNodesCorrectBinsDynamic (ethersphere#1776) network: Add Capabilities to Kademlia database (ethersphere#1713) network: add own address to KademliaInfo (ethersphere#1775) pss: Refactor. Step 2. Refactor forward cache (ethersphere#1742) all: configurable payment/disconnect thresholds (ethersphere#1729) network/stream/v2: more resilient TestNodesExchangeCorrectBinIndexes (ethersphere#1760) ...
No description provided.