# Max spans happened in Hikari-pool connection addr thred

**URL:** <https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145>\
**Category:** APM\
**Tags:** java\
**Created:** [October 20, 2021, 2:46am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145 "2021-10-20T02:46:36Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![fido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fido/32/96099_2.png) [@fido](https://discuss.elastic.co/u/fido)\
**Post date:** [October 20, 2021, 2:46am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/1 "2021-10-20T02:46:36Z")

</div>

Hi，I am having trouble using Hikari connection pool with java APM agent.  
I meet Transaction Max spans warnning, when it happend at a transaction has more than spanLimited spans, which i set is 500. Then i found it happend always in HikariPool connection adder,but not in business thread,which is a thread that Hikari-pool use for keep normal connection count.  
Then i read the source code,i found that when in com.zaxxer.hikari.pool.HikariPool#addBagItem ,it will submit a task ,which has a ScheduleTaskThreadPoolExecutor working in it,but the ScheduleTaskThreadPoolExecutor will submit the same task ,combined with apm-java-concurrent-plugin it will leak to the Transaction never end. Also,we improved "DataSource#getConnection" ,so that the Transaction will keep create spans .  
If we need to fix this probleam, our team want to excluded "java.util.concurrent.ScheduledThreadPoolExecutor" in ExecutorInstrumentation.  
I want to know if there has more suitable method to slove this probleam or is my guess correct?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/d/1d6a697b12e7acdec010bcb5148aed8a515011b5.png)

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [October 20, 2021, 5:50am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/2 "2021-10-20T05:50:46Z")

</div>

Hi @fido and welcome to our forum!  
Thanks for reporting.  
Please try these two things and report back:

1. Try our [latest snapshot](https://github.com/elastic/apm-agent-java#snapshots) and see if the problem persists. It contains a recent [change](https://github.com/elastic/apm-agent-java/pull/2173) to the way references of transactions and spans are managed in the concurrency plugin

2. If I understand this correctly:

> Also,we improved "DataSource#getConnection" ,so that the Transaction will keep create spans`

you added some custom instrumentation. Please try the same scenario without any instrumentation other than the one the agent applies automatically. It is quite common for custom instrumentations to leak transactions/span due to improper reference counting.

---

<div class="post-metadata">

**Author:** ![fido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fido/32/96099_2.png) [@fido](https://discuss.elastic.co/u/fido)\
**Post date:** [October 21, 2021, 2:09am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/3 "2021-10-21T02:09:18Z")

</div>

Thanks for replying ! It seems like the recent change slove my probleam. But i read the change, it change the contextMap implement in JavaConcurrent and no need to control the reference change in restoreContext methord and captureContext method , but i still can not understand why it can work ？

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [October 21, 2021, 3:39am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/4 "2021-10-21T03:39:33Z")

</div>

The map was replaced with one that automatically manages the reference counting of spans/transactions in a way that accurately reflects how they are referenced by the map.

If you look at the former `captureContext` implementation, it would always try to put in the map AND increment references. If the span/transaction was already mapped to this key, its entry would only be **replaced** , but its ref counting would be increased as if there is an additional ref to it. If you look at the implementation of `co.elastic.apm.agent.collections.SpanConcurrentHashMap#onPut` that is now used instead, you would see that it handles it properly, meaning - the ref count will only account for one ref per map entry.

This is a bug fix to ref counting. What happens next with that I cannot say, either you relied on the ref count for something or that the agent does, causing further implications. For example, a transaction with ref count higher than 0 will never be recycled, so its child span count will never be reset.

---

<div class="post-metadata">

**Author:** ![fido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fido/32/96099_2.png) [@fido](https://discuss.elastic.co/u/fido)\
**Post date:** [October 21, 2021, 9:44am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/5 "2021-10-21T09:44:04Z")

</div>

I seems like understand your answer. I have another querstion, if the reference count is correct , the transaction has been ended and its state are reseted, but the submit task has been delay execute , such like ScheduleThreadPoolExecutor , Then,when the task is executed , is that the current thread tracer hold a transaction which is after recyle without any state? I am a fresh about apm-agent-java, i appreciate your understanding of all inconvenience that i brought to you.

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [October 21, 2021, 10:25am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/6 "2021-10-21T10:25:18Z")

</div>

That is the exact purpose of the `contextMap` - it stores references to spans/transactions so that they can be retrieved from other threads, and as long as they are in the map, their reference count cannot go to 0, so they won't be recycled (even if they are ended and reported).  
When a task is submitted, it will be stored in the map as the key, where the value is the span/transaction that was active when it was submitted. When the task is executed, it will be retrieved from the map, together with the corresponding span/transaction.

I hope this clears things.

---

<div class="post-metadata">

**Author:** ![fido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fido/32/96099_2.png) [@fido](https://discuss.elastic.co/u/fido)\
**Post date:** [October 22, 2021, 9:47am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/7 "2021-10-22T09:47:13Z")

</div>

I use the lastest snapshot , but also has the same probleam. I add some logs for tracing, i found that the question is two executor exist loop call in HikariPool .  
As you say, the former `captureContext` implementation may cause span/transaction incorrect ref counting, this can cause these objects can not be recycled, and make memory leak? But how does it can make max spans? Max spans will happened in transaction create span . Also, when deactivate a span, it will check if this span is the correct active span. I don't found this warn log in my program, it means that all the span/transaction in the tracer's activeStack are correctly handled. I draw a simple flowchart.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/3/b38a3009538be2b484c7f60a492e8e1c0021e41d.png)  
Here is some code in HikariPool  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/7/27d54d1700f84cf683acee65941febb125f51a7d.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/0/409fb198e619058ac87c11a94b786f5c663ac50e.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/e/2e08a5758034fc81df5419b84925ff5fa5ceb4a0.png)  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/8/586b3e51532c8a8961075ab4fac2f08d7e570ba9.png)

In `createPoolEntry` , it will create some span about sql . So, it seems like it will always keep the transaction , because is a circle , and although the ref count is correct and the activeStack in tracer is properly maintained.

I don't know if i speak clearly ?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [October 24, 2021, 5:16am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/8 "2021-10-24T05:16:36Z")

</div>

> [@fido](#):
>
> As you say, the former `captureContext` implementation may cause span/transaction incorrect ref counting, this can cause these objects can not be recycled, and make memory leak?

It can cause spans to be garbage collected instead of recycled, but not a memory leak.

> [@fido](#):
>
> Also,we improved "DataSource#getConnection" ,so that the Transaction will keep create spans .

This definitely sounds related.  
**First step:** please confirm that the problem still occurs after removing everything you did for manual tracing, relying only on automatic agent tracing. If you get the same behavior only with agent instrumentation, we will analyze further.

---

<div class="post-metadata">

**Author:** ![fido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fido/32/96099_2.png) [@fido](https://discuss.elastic.co/u/fido)\
**Post date:** [October 25, 2021, 1:53am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/9 "2021-10-25T01:53:21Z")

</div>

Yes,I remove all my tracing log , and use the lastest snapshot. It also has the probleam. As my flowchart,it seems like it's a normal sitution in the code ,because when submit a task,it will pass the transaction to a async thread. When two thread  
keep passing each other‘s transaction. It will never end the transaction.

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [October 25, 2021, 5:18am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/10 "2021-10-25T05:18:59Z")

</div>

Your investigation and description of the problem are extremely useful, I didn't intend to doubt them. What I was missing is why these kept-alive transactions should bother us.  
They are kept active on `addConnectionExecutor` and `houseKeepingExecutorService`, which employ house-keeping threads, so I didn't understand what spans are being created on these threads. Looking in the Hikari code now, I can see that these spans are probably related to the connection initiation SQL being executed to validate newly created connections.

The transactions should still be ended and sent normally, the only thing is that spans will continue to be created and sent as child spans. I tried a simple way to [fix this issue](https://github.com/elastic/apm-agent-java/pull/2210), please try out the [snapshot build](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-java%2Fapm-agent-java-mbp/detail/PR-2210/1/artifacts) and verify the issue is resolved with it.

Thanks a lot for your wonderful report and patience!

---

<div class="post-metadata">

**Author:** ![fido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fido/32/96099_2.png) [@fido](https://discuss.elastic.co/u/fido)\
**Post date:** [October 25, 2021, 7:34am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/11 "2021-10-25T07:34:23Z")

</div>

Yes,that's what i mean,please forgive me for my unclear expression.Your lastest bug fix can slove the probleam.  
Thank you for your clear answer!

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [October 25, 2021, 8:26am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/12 "2021-10-25T08:26:24Z")

</div>

> [@fido](#):
>
> Your lastest bug fix can slove the probleam.

just to make sure I got this clear - can you confirm that you tested and the problem is eliminated with this fix?

---

<div class="post-metadata">

**Author:** ![fido](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fido/32/96099_2.png) [@fido](https://discuss.elastic.co/u/fido)\
**Post date:** [October 25, 2021, 9:44am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/13 "2021-10-25T09:44:30Z")

</div>

Yes,i used the lastest snapshot from the snapshot build url. I use the same test code, the probleam not happened.

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [October 25, 2021, 11:04am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/14 "2021-10-25T11:04:50Z")

</div>

Awesome! Thanks for the great report and collaboration!

---

<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:** [November 15, 2021, 7:05am UTC](https://discuss.elastic.co/t/max-spans-happened-in-hikari-pool-connection-addr-thred/287145/15 "2021-11-15T07:05:26Z")

</div>

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