Skip to content

Make sure ctx in commands are derived from req.Context #1585

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

Merged
merged 9 commits into from
Aug 24, 2015

Conversation

rht
Copy link
Contributor

@rht rht commented Aug 16, 2015

Fixes #505

@jbenet jbenet added the status/in-progress In progress label Aug 16, 2015
@rht
Copy link
Contributor Author

rht commented Aug 16, 2015

@whyrusleeping there are still several left, pending. And how to add a context to https://github.com/ipfs/go-ipfs/blob/master/core/coreunix/metadata_test.go#L52?

@rht
Copy link
Contributor Author

rht commented Aug 16, 2015

Or keep the option that ipfsnode can be initialized without ctx. and modify (n *ipfsnode) Context() method to assign context.TODO() when n.ctx is nil

@rht rht force-pushed the cleanup-context branch from 24ee3df to 0e96262 Compare August 16, 2015 18:13
@@ -29,22 +27,13 @@ func NewDirectory(dserv mdag.DAGService) *directoryBuilder {
}

// AddChild adds a (name, key)-pair to the root node.
func (d *directoryBuilder) AddChild(name string, k key.Key) error {
// TODO(cryptix): consolidate context managment
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no sure if WithTimeout should be removed.
In other places sometimes it is 30 seconds or a minute, or no WithTimeout at all.

I there must be WithTimeout, then this should be inside dserv.Get

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, most of the places we do this (context.TODO with a timeout) should be replaced with a passed in context. and the caller can decide the timeout if they want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to removing this timeout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove or replace with WithCancel?

@whyrusleeping
Copy link
Member

One thing to note, we should try to avoid node.Context() if we can, as its the 'global' context. We should prefer a finer grained context when we can get it.

@rht
Copy link
Contributor Author

rht commented Aug 16, 2015

though for now, node.Context() is more fine grained than context.tODO()

@whyrusleeping
Copy link
Member

for sure! just wanted to make a note of that.

@@ -460,7 +460,7 @@ func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, er
var modified *mdag.Node
ndata := new(ft.FSNode)
for i, lnk := range nd.Links {
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
_ctx, cancel := context.WithTimeout(ctx, time.Minute)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this timeout one is one to remove?

We'd still want a context derived from the one passed in, so things can be cancelled when this func returns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetNode basically calls DAG.Get, so WithCancel can be done inside DAG.Get too.
(ok but most are used for func-specific context)

@jbenet
Copy link
Member

jbenet commented Aug 17, 2015

thanks for this PR, much needed cleanup

@rht
Copy link
Contributor Author

rht commented Aug 17, 2015

WithTimeout cleanup should be in a separate PR.

@rht rht force-pushed the cleanup-context branch 2 times, most recently from 93fd7f0 to 7ecbdbd Compare August 17, 2015 08:25
@rht
Copy link
Contributor Author

rht commented Aug 17, 2015

The remaining cleanup TODO has larger depths, and requires review:

  1. p2p/host/basic/natmgr.go#L147: too buried down
  2. eventlog: not sure if this needs cleanup
  3. merkledag/traverse/traverse.go is not used
  4. importer/helpers/helpers.go: too buried down

Won't touch:

  1. test files
  2. in exchange/bitswap/testnet/virtual.go func (n *bitswap.network) SendMessage because of the comment 'nb: terminate the context since the context wouldn't actually be passed over the network in a real scenario', so won't touch
  3. routing/mock/dht.go mockrouting.NewDHTNetwork
  4. in routing/supernode/proxy/loopback.go func (lb *Loopback) HandleStream(s inet.Stream), seems to be intentional
  5. exchange/bitswap/testnet/peernet.go func (pn *bitswap.peernet) Adapter seems to be intentional
  6. trickle.VerifyTrickleDagStructure only used in test (and tests already use context.TODO())

@rht
Copy link
Contributor Author

rht commented Aug 17, 2015

WithTimeout-specific question. Should this be moved inside DAG.Get? While outside of it, also use WithCancel.

@whyrusleeping
Copy link
Member

  1. probably okay to leave for now
  2. eventlog doesnt really use contexts for cancellation, so no need to worry about that
  3. probably fine not to touch then
  4. i think there is a context passed in by the dagbuilderhelper that you might have access to.

Thanks for doing all this, its really good stuff :)

@rht
Copy link
Contributor Author

rht commented Aug 18, 2015

4 dagbuilderhelper doesn't have ctx by default. Should it have a ctx field?

@whyrusleeping
Copy link
Member

@rht hrm.. i'm torn on this one. The only time a context is used is in a situtation where we are expected to already have the node local. We could wire a context down through there, but i dont think that its going to be worthwhile (i could be wrong though). What we could do is change the context.TODO() to a context.Background() and put a comment saying why

@rht
Copy link
Contributor Author

rht commented Aug 20, 2015

Or just add ctx explicitly as an extra argument, here the ctx flow is:
DagModifier.ctx -> trickle.TrickleAppend -> (n *helpers.UnixfsNode) GetChild

@rht rht force-pushed the cleanup-context branch from 68c51b8 to 70e8427 Compare August 20, 2015 01:08
@rht
Copy link
Contributor Author

rht commented Aug 20, 2015

@whyrusleeping how about this?

Though most of the buried down, long context pipes are constructed because of the need to call dagservice GetNode / GetDAG. It is possibly much cleaner to hide the pipes inside dagservice? (will check if this is the case)

@rht
Copy link
Contributor Author

rht commented Aug 20, 2015

why it is worthwhile: to make sure there are no 1-min zombie dagservices when an IpfsNode closes.

@rht
Copy link
Contributor Author

rht commented Aug 20, 2015

  1. p2p/host/basic/natmgr.go#L147: too buried down

This could be fixed with replacing L120 with worker.GoChild(func(p goprocess.Process) {addPortMapping(nmgr, addr)})?

Edit: i c, this natmgr.go#L147 is used for eventlog only, so it's fine.

@rht
Copy link
Contributor Author

rht commented Aug 20, 2015

if err == nil { ready to merge }

@whyrusleeping
Copy link
Member

LGTM

@jbenet jbenet mentioned this pull request Aug 22, 2015
16 tasks
@rht rht force-pushed the cleanup-context branch from 70e8427 to fcf915f Compare August 23, 2015 12:51
@rht
Copy link
Contributor Author

rht commented Aug 23, 2015

 move ctx inside dag.Get? https://github.com/ipfs/go-ipfs/pull/1585/files#r37702188

I think this should be done in separate PR. Not every dag.Get caller waits only on this function to finish.

License: MIT
Signed-off-by: rht <[email protected]>
@rht
Copy link
Contributor Author

rht commented Aug 23, 2015

 move ctx inside dag.Get? https://github.com/ipfs/go-ipfs/pull/1585/files#r37702188
I think this should be done in separate PR. Not every dag.Get caller waits only on this function to finish.

Added here anyway. I only made the change here when it is unambiguous that having a context.WithCancel before calling dag.Get makes it redundant AND when this ctx is not used again later on in the scope.

This also "auto-fix" parts of the code where there is no context.WithCancel before a dag.Get.

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 <[email protected]>
@rht rht force-pushed the cleanup-context branch from 4f2939b to de5c0ce Compare August 23, 2015 15:38
@jbenet
Copy link
Member

jbenet commented Aug 23, 2015

This means all lines matching 'ctx, _ *' are to be replaced with 'ctx, cancel ' + defer cancel()?

yeah.

@@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • we can probably make this a constant too (and possibly even remove it??)
  • maybe a future PR can go over all the timeouts and make sure they make sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better in another PR. This PR is specifically to make sure all context trees are linked to the root caller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good 👍

@jbenet
Copy link
Member

jbenet commented Aug 23, 2015

Ok, this LGTM. the only remaining things:

  • do all commits pass? (maybe run gpe? or squash them if too annoying/slow?)
  • is @whyrusleeping ok with merging this, given dev 0.4.0 and rebase ugliness?

maybe let's make all further refactor/cleanup happen on top of 0.4.0

@jbenet
Copy link
Member

jbenet commented Aug 23, 2015

thanks @rht , this will improve codebase (+ perf for users in slow networkes!) a lot!

@rht
Copy link
Contributor Author

rht commented Aug 23, 2015

(+ perf for users in slow networkes!)

in what way?

@jbenet
Copy link
Member

jbenet commented Aug 23, 2015

requests wont timeout prematurely

@rht
Copy link
Contributor Author

rht commented Aug 23, 2015

if slow means high latency: lunar RTT is about 2.5s, mars RTT is about 500s-2500s.

@rht
Copy link
Contributor Author

rht commented Aug 23, 2015

gpe (on rht/go-ipfs) doesn't run the test unless it is done on ipfs/go-ipfs itself.

@jbenet
Copy link
Member

jbenet commented Aug 23, 2015

@rht you have push access to this repo directly. you can push branches here

@rht
Copy link
Contributor Author

rht commented Aug 24, 2015

  1. gpe-ed the commits
  2. Just tested a dev0.40 rebase against this branch. There are merge conflicts due to commits for dagmodifier/ipfs add (already in master), but not from this branch.

@jbenet
Copy link
Member

jbenet commented Aug 24, 2015

Ok, sounds good! thanks @rht

jbenet added a commit that referenced this pull request Aug 24, 2015
Make sure ctx in commands are derived from req.Context
@jbenet jbenet merged commit a965316 into ipfs:master Aug 24, 2015
hacdias pushed a commit to ipfs/boxo that referenced this pull request Jan 27, 2023
Make sure ctx in commands are derived from req.Context

This commit was moved from ipfs/kubo@a965316
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants