# ThreadContext loses headers between threads

**URL:** https://discuss.elastic.co/t/threadcontext-loses-headers-between-threads/166741
**Category:** Elasticsearch
**Created:** [February 1, 2019, 1:43pm UTC](https://discuss.elastic.co/t/threadcontext-loses-headers-between-threads/166741 "2019-02-01T13:43:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sergey\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_ivanov/32/40468_2.png) [@Sergey\_Ivanov](https://discuss.elastic.co/u/Sergey_Ivanov)
#### Post date: [February 1, 2019, 1:43pm UTC](https://discuss.elastic.co/t/threadcontext-loses-headers-between-threads/166741/1 "2019-02-01T13:43:59Z")

</div>

Hello,

We're using our custom plugin for Elasticsearch and we faced the following problem after upgrade from ES 5.6.6 to 6.5.0:

Looks like `ThreadContext` loses headers between `http_server_worker` and `updateTask` threads.

For example, we store custom header in `RestHandler` like:

```
 @Override
public void handleRequest(RestRequest restRequest, RestChannel restChannel, NodeClient nodeClient)
  throws Exception {
    try {
      threadContext.putHeader("user", "test");      
 ......

```

and then we try to get this header in `IndexEventListener` implementation:

```
  @Override
  public void afterIndexCreated(IndexService indexService) {
    final String user = threadContext.getHeader("user");
....

```

But headers are empty.

Could you please help, is it expected behavior? I didn't find any information about these changes in change-log for update to 6.5.0 but it worked fine on ES 5.6.6.

---

<div class="post-metadata">

### Author: ![Sergey\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sergey_ivanov/32/40468_2.png) [@Sergey\_Ivanov](https://discuss.elastic.co/u/Sergey_Ivanov)
#### Post date: [February 12, 2019, 8:56am UTC](https://discuss.elastic.co/t/threadcontext-loses-headers-between-threads/166741/2 "2019-02-12T08:56:44Z")

</div>

In addition, we found the issue which probably affected this behavior:  
[Use system context for cluster state update tasks (#31241)](https://github.com/elastic/elasticsearch/commit/02a4ef38a7960895b4e26111167676ec49302cba#diff-162074b704aa51c938206a14de3aa227R733)

and specifically here is this code:

```
final Supplier<ThreadContext.StoredContext> supplier = threadContext.newRestorableContext(false);
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
        threadContext.markAsSystemContext();

        List<Batcher.UpdateTask> safeTasks = tasks.entrySet().stream()
            .map(e -> taskBatcher.new UpdateTask(config.priority(), source, e.getKey(), safe(e.getValue()), executor))
            .map(e -> taskBatcher.new UpdateTask(config.priority(), source, e.getKey(), safe(e.getValue(), supplier), executor))
            .collect(Collectors.toList());
        taskBatcher.submitTasks(safeTasks, config.timeout());
    } catch (EsRejectedExecutionException e) {

```

`threadContext.stashContext()` - saves old and creates new ThreadContext where we have no access to previous thread context's headers in `updateTask` thread.

Could you please explain, how we can obtain these headers in`updateTask` thread now (after changes of this issue)?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 12, 2019, 8:56am UTC](https://discuss.elastic.co/t/threadcontext-loses-headers-between-threads/166741/3 "2019-03-12T08:56:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
