Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it worth cleaning ThreadLocals in Filter to solve thread pool-related issues?

In short - tomcat uses a thread pool, so threads are reused. Some libraries use ThreadLocal variables, but don't clean them up (using .remove()), so in fact they return "dirty" threads to the pool.

Tomcat has new features of detecting these things on shutdown, and cleaning the thread locals. But it means the threads are "dirty" during the whole execution.

What I can do is implement a Filter, and right after the request completes (and the thread is returned to the pool), clean all ThreadLocals, using the code from tomcat (the method there is called checkThreadLocalsForLeaks).

The question is, is it worth it? Two pros:

  • preventing memory leaks
  • preventing undeterministic behaviour by the libraries, which assume the thread is "fresh"

One con:

  • The solution uses reflection, so it's potentially slow. All reflection data (Fields) will be cached, of course, but still.

Another option is to report the issue to the libraries that don't clean their thread locals.

like image 926
Bozho Avatar asked Feb 19 '11 23:02

Bozho


People also ask

When should I use ThreadLocal?

Most common use of thread local is when you have some object that is not thread-safe, but you want to avoid synchronizing access to that object using synchronized keyword/block. Instead, give each thread its own instance of the object to work with.

How do I clear a local thread?

The remove() method of ThreadLocal class is used to remove the current thread's value for this thread-local variable.

Is ThreadLocal thread safe?

Thread Safety With the exception of Dispose(), all public and protected members of ThreadLocal<T> are thread-safe and may be used concurrently from multiple threads.

What is the purpose of ThreadLocal in Java?

Java ThreadLocal is used to create thread local variables. We know that all threads of an Object share it's variables, so the variable is not thread safe. We can use synchronization for thread safety but if we want to avoid synchronization, we can use ThreadLocal variables.


2 Answers

I would go through the route of reporting the issue to the library developers for 2 reasons:

  • It will help to other people who want to use the same library, but lack the skills / time to find such a HORRIBLE memory leak.
  • To help the developers of the library to build a better product.

Honestly, I've never seen this type of error before and I think it's an exception rather than something that we should guard as it happens often. Could you share on which library you've seen this behaviour?

As a side note, I wouldn't mind enabling that filter in the development / test environment and logging a critical error if a ThreadLocal variable is still attached.

like image 133
Augusto Avatar answered Sep 28 '22 21:09

Augusto


in theory, this seems like a good idea. however, i could see some situations where you might not want to do this. for instance, some of the xml related technologies have some non-trivial setup costs (like setting up DocumentBuilders and Transformers). if you are doing a lot of that in your webapp, it may make sense to cache these instances in ThreadLocals (as the utilities are generally not thread-safe). in this case, you probably don't want to clean these between requests.

like image 28
jtahlborn Avatar answered Sep 28 '22 23:09

jtahlborn