diff --git a/blockservice/test/blocks_test.go b/blockservice/test/blocks_test.go index dbbc51562..c94a2357e 100644 --- a/blockservice/test/blocks_test.go +++ b/blockservice/test/blocks_test.go @@ -42,7 +42,8 @@ func TestBlocks(t *testing.T) { t.Error("returned key is not equal to block key", err) } - ctx, _ := context.WithTimeout(context.TODO(), time.Second*5) + ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5) + defer cancel() b2, err := bs.GetBlock(ctx, b.Key()) if err != nil { t.Error("failed to retrieve block from BlockService", err) @@ -75,7 +76,8 @@ func TestGetBlocksSequential(t *testing.T) { t.Log("one instance at a time, get blocks concurrently") for i := 1; i < len(servs); i++ { - ctx, _ := context.WithTimeout(context.TODO(), time.Second*50) + ctx, cancel := context.WithTimeout(context.TODO(), time.Second*50) + defer cancel() out := servs[i].GetBlocks(ctx, keys) gotten := make(map[key.Key]*blocks.Block) for blk := range out { diff --git a/core/bootstrap.go b/core/bootstrap.go index 2c4752970..a7508de1f 100644 --- a/core/bootstrap.go +++ b/core/bootstrap.go @@ -110,7 +110,8 @@ func Bootstrap(n *IpfsNode, cfg BootstrapConfig) (io.Closer, error) { func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) error { - ctx, _ = context.WithTimeout(ctx, cfg.ConnectionTimeout) + ctx, cancel := context.WithTimeout(ctx, cfg.ConnectionTimeout) + defer cancel() id := host.ID() // get bootstrap peers from config. retrieving them here makes diff --git a/core/core.go b/core/core.go index b63d88d7c..6a47c04f0 100644 --- a/core/core.go +++ b/core/core.go @@ -320,7 +320,8 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption { func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) { log.Warning("trying peer info: ", p) - ctx, _ := context.WithTimeout(n.Context(), time.Second*10) + ctx, cancel := context.WithTimeout(n.Context(), time.Second*10) + defer cancel() if err := n.PeerHost.Connect(ctx, p); err != nil { log.Warning("Failed to connect to peer found by discovery: ", err) } diff --git a/diagnostics/diag.go b/diagnostics/diag.go index 104cb5977..3877ee139 100644 --- a/diagnostics/diag.go +++ b/diagnostics/diag.go @@ -298,7 +298,8 @@ func (d *Diagnostics) HandleMessage(ctx context.Context, s inet.Stream) error { if timeout < HopTimeoutDecrement { return fmt.Errorf("timeout too short: %s", timeout) } - ctx, _ = context.WithTimeout(ctx, timeout) + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() pmes.SetTimeoutDuration(timeout - HopTimeoutDecrement) dpeers, err := d.getDiagnosticFromPeers(ctx, d.getPeers(), pmes) diff --git a/exchange/bitswap/bitswap_test.go b/exchange/bitswap/bitswap_test.go index e70b3885a..41f0e6c08 100644 --- a/exchange/bitswap/bitswap_test.go +++ b/exchange/bitswap/bitswap_test.go @@ -50,7 +50,8 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this solo := g.Next() defer solo.Exchange.Close() - ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond) + ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond) + defer cancel() _, err := solo.Exchange.GetBlock(ctx, block.Key()) if err != context.DeadlineExceeded { @@ -76,7 +77,8 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) { wantsBlock := peers[1] defer wantsBlock.Exchange.Close() - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() received, err := wantsBlock.Exchange.GetBlock(ctx, block.Key()) if err != nil { t.Log(err) @@ -226,14 +228,16 @@ func TestSendToWantingPeer(t *testing.T) { alpha := bg.Next() // peerA requests and waits for block alpha - ctx, _ := context.WithTimeout(context.TODO(), waitTime) + ctx, cancel := context.WithTimeout(context.TODO(), waitTime) + defer cancel() alphaPromise, err := peerA.Exchange.GetBlocks(ctx, []key.Key{alpha.Key()}) if err != nil { t.Fatal(err) } // peerB announces to the network that he has block alpha - ctx, _ = context.WithTimeout(context.TODO(), timeout) + ctx, cancel = context.WithTimeout(context.TODO(), timeout) + defer cancel() err = peerB.Exchange.HasBlock(ctx, alpha) if err != nil { t.Fatal(err) @@ -266,7 +270,8 @@ func TestBasicBitswap(t *testing.T) { t.Fatal(err) } - ctx, _ := context.WithTimeout(context.TODO(), time.Second*5) + ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5) + defer cancel() blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Key()) if err != nil { t.Fatal(err) diff --git a/exchange/bitswap/notifications/notifications_test.go b/exchange/bitswap/notifications/notifications_test.go index e9be15aa4..8ab9887ff 100644 --- a/exchange/bitswap/notifications/notifications_test.go +++ b/exchange/bitswap/notifications/notifications_test.go @@ -112,7 +112,8 @@ func TestSubscribeIsANoopWhenCalledWithNoKeys(t *testing.T) { func TestCarryOnWhenDeadlineExpires(t *testing.T) { impossibleDeadline := time.Nanosecond - fastExpiringCtx, _ := context.WithTimeout(context.Background(), impossibleDeadline) + fastExpiringCtx, cancel := context.WithTimeout(context.Background(), impossibleDeadline) + defer cancel() n := New() defer n.Shutdown() diff --git a/namesys/publisher.go b/namesys/publisher.go index 3f5e15ae5..e3dd1d81b 100644 --- a/namesys/publisher.go +++ b/namesys/publisher.go @@ -60,7 +60,8 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa log.Debugf("Storing pubkey at: %s", namekey) // Store associated public key - timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*10)) + timectx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second*10)) + defer cancel() err = p.routing.PutValue(timectx, namekey, pkbytes) if err != nil { return err @@ -70,9 +71,9 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa log.Debugf("Storing ipns entry at: %s", ipnskey) // Store ipns entry at "/ipns/"+b58(h(pubkey)) - timectx, _ = context.WithDeadline(ctx, time.Now().Add(time.Second*10)) - err = p.routing.PutValue(timectx, ipnskey, data) - if err != nil { + timectx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Second*10)) + defer cancel() + if err := p.routing.PutValue(timectx, ipnskey, data); err != nil { return err } diff --git a/pin/pin_test.go b/pin/pin_test.go index 223beb03e..d3947254d 100644 --- a/pin/pin_test.go +++ b/pin/pin_test.go @@ -210,21 +210,21 @@ func TestPinRecursiveFail(t *testing.T) { } // Note: this isnt a time based test, we expect the pin to fail - mctx, _ := context.WithTimeout(ctx, time.Millisecond) + mctx, cancel := context.WithTimeout(ctx, time.Millisecond) + defer cancel() err = p.Pin(mctx, a, true) if err == nil { t.Fatal("should have failed to pin here") } - _, err = dserv.Add(b) - if err != nil { + if _, err := dserv.Add(b); err != nil { t.Fatal(err) } // this one is time based... but shouldnt cause any issues - mctx, _ = context.WithTimeout(ctx, time.Second) - err = p.Pin(mctx, a, true) - if err != nil { + mctx, cancel = context.WithTimeout(ctx, time.Second) + defer cancel() + if err := p.Pin(mctx, a, true); err != nil { t.Fatal(err) } } diff --git a/routing/dht/ext_test.go b/routing/dht/ext_test.go index c77116578..75219da5c 100644 --- a/routing/dht/ext_test.go +++ b/routing/dht/ext_test.go @@ -202,7 +202,8 @@ func TestNotFound(t *testing.T) { } // long timeout to ensure timing is not at play. - ctx, _ = context.WithTimeout(ctx, time.Second*20) + ctx, cancel := context.WithTimeout(ctx, time.Second*20) + defer cancel() v, err := d.GetValue(ctx, key.Key("hello")) log.Debugf("get value got %v", v) if err != nil { @@ -274,7 +275,8 @@ func TestLessThanKResponses(t *testing.T) { }) } - ctx, _ = context.WithTimeout(ctx, time.Second*30) + ctx, cancel := context.WithTimeout(ctx, time.Second*30) + defer cancel() if _, err := d.GetValue(ctx, key.Key("hello")); err != nil { switch err { case routing.ErrNotFound: