Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

cmd/swamr-smoke: fix waitToPushSynced connection closing #1781

Merged
merged 2 commits into from
Sep 19, 2019

Conversation

janos
Copy link
Member

@janos janos commented Sep 19, 2019

No description provided.

@janos janos self-assigned this Sep 19, 2019
@@ -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)
Copy link
Contributor

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.

Copy link
Member Author

@janos janos Sep 19, 2019

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

@janos janos requested a review from nonsense September 19, 2019 10:00
@nonsense nonsense merged commit f185630 into master Sep 19, 2019
@janos janos deleted the fix-smoke-wait-to-push-sync branch September 19, 2019 10:29
@skylenet skylenet added this to the 0.5.0 milestone Sep 20, 2019
chadsr added a commit to chadsr/swarm that referenced this pull request Sep 23, 2019
* '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)
  ...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants