Search code examples
gogoroutine

Pass a copy of main goroutine context to subroutine context


I have a golang API endpoint with a context associated with it.

The endpoint needs to do some heavy lifting behind the scenes, so I create a new sub routine within the main endpoint and return the response then there itself.

In order to deal with context cancellation, I am creating a background context and pass this as the new context to the subroutine.

The problem is by doing this, yes, I can execute the background subroutine, but the values within the main context such as request id, span id etc. (most of keys are unknown to me), which are being used for tracing will be lost.

How do I pass a parent context to the subroutine without cancelling the the execution even after the response is sent to the client.

EDIT

I am not passing any values into the context. But initially we are passing request-id, span-id etc. which are needed for tracing. These informations are in the context. This is an internal library, and context is the place were we are keeping this.

I know this is an anti-pattern for using context to pass values, and no values is passed, except the request-id and other values that important to the library not to the business logic


Solution

  • When you cancel a parent context, all contexts derived from it will also cancel. So you are correct in creating a new context for goroutines spawned from a request handler.

    When you create a new context, you should copy all the values you are interested in from the original context to the new context. However, you said you don't know all the keys. So you can still keep a reference to the parent context so that you can query it for values. Something like this:

    type nestedContext struct {
       context.Context
       parent context.Context
    }
    
    func (n nestedContext)  Value(key any) any {
       return n.parent.Value(key)
    }
    
    ...
    newContext := nestedContext{
       Context:context.Background(),
       parent: parentContext,
    }
    

    This will create a new context from the context.Background() that will lookup values from the canceled parent context.

    Pass newContext as the context to the goroutines created from the handler.