Skip to content

Commit

Permalink
Localize the scope of context.WithCancel for every DAG.Get
Browse files Browse the repository at this point in the history
Instead put it inside of DAG.Get.
The fix is applied only in the case when the context.WithCancel
before a DAG.Get is also used later on in the scope.

License: MIT
Signed-off-by: rht <rhtbot@gmail.com>
  • Loading branch information
rht committed Aug 23, 2015
1 parent ccd06e5 commit de5c0ce
Showing 13 changed files with 16 additions and 53 deletions.
6 changes: 1 addition & 5 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
@@ -6,8 +6,6 @@ import (
"io"
"text/tabwriter"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
merkledag "github.com/ipfs/go-ipfs/merkledag"
@@ -80,9 +78,7 @@ it contains, with the following format:
Links: make([]LsLink, len(dagnode.Links)),
}
for j, link := range dagnode.Links {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
link.Node, err = link.GetNode(ctx, node.DAG)
link.Node, err = link.GetNode(req.Context(), node.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
7 changes: 1 addition & 6 deletions core/commands/object.go
Original file line number Diff line number Diff line change
@@ -9,10 +9,8 @@ import (
"io/ioutil"
"strings"
"text/tabwriter"
"time"

mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

key "github.com/ipfs/go-ipfs/blocks/key"
cmds "github.com/ipfs/go-ipfs/commands"
@@ -480,14 +478,11 @@ resulting object hash.
return
}

ctx, cancel := context.WithTimeout(req.Context(), time.Second*30)
rnode, err := nd.DAG.Get(ctx, rhash)
rnode, err := nd.DAG.Get(req.Context(), rhash)
if err != nil {
res.SetError(err, cmds.ErrNormal)
cancel()
return
}
cancel()

action := req.Arguments()[1]

6 changes: 1 addition & 5 deletions core/commands/unixfs/ls.go
Original file line number Diff line number Diff line change
@@ -7,8 +7,6 @@ import (
"sort"
"text/tabwriter"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

cmds "github.com/ipfs/go-ipfs/commands"
core "github.com/ipfs/go-ipfs/core"
path "github.com/ipfs/go-ipfs/path"
@@ -110,9 +108,7 @@ size is the IPFS link size.
links := make([]LsLink, len(merkleNode.Links))
output.Objects[hash].Links = links
for i, link := range merkleNode.Links {
getCtx, cancel := context.WithCancel(ctx)
defer cancel()
link.Node, err = link.GetNode(getCtx, node.DAG)
link.Node, err = link.GetNode(ctx, node.DAG)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
6 changes: 2 additions & 4 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
@@ -367,8 +367,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
}
}

err = i.node.DAG.AddRecursive(newnode)
if err != nil {
if err := i.node.DAG.AddRecursive(newnode); err != nil {
webError(w, "Could not add recursively new node", err, http.StatusInternalServerError)
return
}
@@ -439,8 +438,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
}
}

err = i.node.DAG.AddRecursive(newnode)
if err != nil {
if err := i.node.DAG.AddRecursive(newnode); err != nil {
webError(w, "Could not add recursively new node", err, http.StatusInternalServerError)
return
}
13 changes: 3 additions & 10 deletions core/coreunix/metadata.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package coreunix

import (
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

key "github.com/ipfs/go-ipfs/blocks/key"
core "github.com/ipfs/go-ipfs/core"
dag "github.com/ipfs/go-ipfs/merkledag"
@@ -12,9 +10,7 @@ import (
func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) {
ukey := key.B58KeyDecode(skey)

ctx, cancel := context.WithCancel(n.Context())
defer cancel()
nd, err := n.DAG.Get(ctx, ukey)
nd, err := n.DAG.Get(n.Context(), ukey)
if err != nil {
return "", err
}
@@ -26,8 +22,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error
}

mdnode.Data = mdata
err = mdnode.AddNodeLinkClean("file", nd)
if err != nil {
if err := mdnode.AddNodeLinkClean("file", nd); err != nil {
return "", err
}

@@ -42,9 +37,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error
func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) {
ukey := key.B58KeyDecode(skey)

ctx, cancel := context.WithCancel(n.Context())
defer cancel()
nd, err := n.DAG.Get(ctx, ukey)
nd, err := n.DAG.Get(n.Context(), ukey)
if err != nil {
return nil, err
}
5 changes: 1 addition & 4 deletions importer/trickle/trickledag.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ package trickle

import (
"errors"
"time"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

@@ -268,9 +267,7 @@ func verifyTDagRec(nd *dag.Node, depth, direct, layerRepeat int, ds dag.DAGServi
}

for i := 0; i < len(nd.Links); i++ {
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
defer cancel()
child, err := nd.Links[i].GetNode(ctx, ds)
child, err := nd.Links[i].GetNode(context.TODO(), ds)
if err != nil {
return err
}
5 changes: 1 addition & 4 deletions ipnsfs/dir.go
Original file line number Diff line number Diff line change
@@ -139,10 +139,7 @@ func (d *Directory) childDir(name string) (*Directory, error) {
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
for _, lnk := range d.node.Links {
if lnk.Name == name {
ctx, cancel := context.WithCancel(d.ctx)
defer cancel()

return lnk.GetNode(ctx, d.fs.dserv)
return lnk.GetNode(d.ctx, d.fs.dserv)
}
}

2 changes: 2 additions & 0 deletions merkledag/merkledag.go
Original file line number Diff line number Diff line change
@@ -93,6 +93,8 @@ func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
if n == nil {
return nil, fmt.Errorf("dagService is nil")
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()

b, err := n.Blocks.GetBlock(ctx, k)
if err != nil {
6 changes: 1 addition & 5 deletions merkledag/traverse/traverse.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package traverse

import (
"errors"
"time"

"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

@@ -67,10 +66,7 @@ func (t *traversal) callFunc(next State) error {
func (t *traversal) getNode(link *mdag.Link) (*mdag.Node, error) {

getNode := func(l *mdag.Link) (*mdag.Node, error) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
defer cancel()

next, err := l.GetNode(ctx, t.opts.DAG)
next, err := l.GetNode(context.TODO(), t.opts.DAG)
if err != nil {
return nil, err
}
3 changes: 1 addition & 2 deletions merkledag/utils/utils.go
Original file line number Diff line number Diff line change
@@ -52,8 +52,7 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s
return nil, err
}

_, err = ds.Add(root)
if err != nil {
if _, err := ds.Add(root); err != nil {
return nil, err
}
return root, nil
2 changes: 0 additions & 2 deletions path/resolver.go
Original file line number Diff line number Diff line change
@@ -87,8 +87,6 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]*me
}

log.Debug("Resolve dag get.")
ctx, cancel := context.WithCancel(ctx)
defer cancel()
nd, err := s.DAG.Get(ctx, key.Key(h))
if err != nil {
return nil, err
3 changes: 1 addition & 2 deletions pin/pin.go
Original file line number Diff line number Diff line change
@@ -106,8 +106,7 @@ func (p *pinner) Pin(ctx context.Context, node *mdag.Node, recurse bool) error {

p.recursePin.AddBlock(k)
} else {
_, err := p.dserv.Get(ctx, k)
if err != nil {
if _, err := p.dserv.Get(ctx, k); err != nil {
return err
}

5 changes: 1 addition & 4 deletions unixfs/mod/dagmodifier.go
Original file line number Diff line number Diff line change
@@ -459,9 +459,6 @@ func dagTruncate(ctx context.Context, nd *mdag.Node, size uint64, ds mdag.DAGSer
var modified *mdag.Node
ndata := new(ft.FSNode)
for i, lnk := range nd.Links {
_ctx, cancel := context.WithCancel(ctx)
defer cancel()

child, err := lnk.GetNode(ctx, ds)
if err != nil {
return nil, err
@@ -474,7 +471,7 @@ func dagTruncate(ctx context.Context, nd *mdag.Node, size uint64, ds mdag.DAGSer

// found the child we want to cut
if size < cur+childsize {
nchild, err := dagTruncate(_ctx, child, size-cur, ds)
nchild, err := dagTruncate(ctx, child, size-cur, ds)
if err != nil {
return nil, err
}

0 comments on commit de5c0ce

Please sign in to comment.