# Context Propagation

**URL:** <https://community.temporal.io/t/context-propagation/8403>\
**Category:** Community Support\
**Tags:** java-sdk, context-propagators\
**Created:** [May 31, 2023, 7:27am UTC](https://community.temporal.io/t/context-propagation/8403 "2023-05-31T07:27:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![haydar](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@haydar](https://community.temporal.io/u/haydar)\
**Post date:** [May 31, 2023, 7:27am UTC](https://community.temporal.io/t/context-propagation/8403/1 "2023-05-31T07:27:54Z")

</div>

**Context:**

I have an application using Temporal and Spring Boot where i want to propagate the JWT of the user who started the workflow to activities, so that the token is propagated across API calls leaving my service. For that purpose I am using `ContextPropagator` from the Java SDK.

In my context propagtor, `setCurrentContext` uses `SecurityContextHolder` of Spring Security to store the token. `SecurityContextHolder` uses under the hood a `ThreadLocal` variable. `getCurrentContext` fetches the token from `SecurityContextHolder` and then **clears the SecurityContext**. I am clearing the SecurityContext since to my understanding, there is no guarantee that the thread currently being used to execute the workflow will continue the execution once an activity is called and done processing and might actually start executing a different workflow. So in order to avoid leaking the `SecurityContext` across workflows re-using the same thread, I am clearing the security context upon any call to Temporal, more specifically: upon calling `getCurrentContext`.

**Problem:** The context is not being propagated upon activity completion. Example:

```auto
class MyWorkflow implements IMyWorkflow {
    private FirstActivity firstActivity = Workflow.newActivityStub(FirstActivity.class);
    private SecondActivity secondActivity = Workflow.newActivityStub(SecondActivity.class);

    @Override
    void execute() {
        // SecurityContext exists here and is propagated correctly to FirstActivity
        firstActivity.doSomething();
        // SecurityContext is empty here
        secondActivity.doSomethingElse();
    }
}

```

Taking a closer look at the events in Temporal, I noticed that the headers of the `ActivityTaskCompleted` and the following `WorkflowTaskScheduled` are empty, and do not hold the same context that was set upon `ActivityTaskScheduled`.

**Question:** Is this behavior intended? How would the propagation work in case a different thread continued the workflow execution after an activity execution is completed? How can we guarantee that any static variable set during context propagation is correct if we cannot clear it upon propagating the context?

Question also posted here: [Context propagation in Temporal - Stack Overflow](https://stackoverflow.com/questions/76365763/context-propagation-in-temporal)

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [May 31, 2023, 2:34pm UTC](https://community.temporal.io/t/context-propagation/8403/2 "2023-05-31T14:34:12Z")

</div>

I would recommend not using ContextPropagator but implementing it using interceptors directly. See [temporal-opentracing](https://github.com/temporalio/sdk-java/tree/master/temporal-opentracing) for inspiration.

Inside the workflow code don’t use ThreadLocal, use its workflow safe equivalent WorkflowThreadLocal instead.
