[
https://issues.apache.org/jira/browse/LOG4J2-479?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13857923#comment-13857923
]
MK commented on LOG4J2-479:
---------------------------
Here's the scenario that I ran into:
I am decorating all web request handlers with some log4j context map values
using AOP
Some of the requests kick off long-running jobs which are executed by a thread
pool (ThreadExecutor)
ThreadExecutor will kick of threads as needed
So:
user1 logs in, submits a batch job request
threadExecutor launches a new thread which inherits the thread context with
user1's context map
user1 logs out
user2 logs in
submits a batch job. This batch job is going to be running in the same thread
that user1 spawned, which is still annotated with user1's information. Bah.
As a bonus, notice how when testing with just user1 everything appears to be
working correctly.
Actually, testing with 2 users will also likely work correctly, because 2nd
user will start a new thread, also "correctly" annotated.
It is generally knowns that InheritableThreadLocal does not work with thread
pools for that reason.
I do not really understand under what circumstances inheriting log context to a
child thread is the desired behavior.
As a matter of fact this is not what the ThreadContext.push() does. That stuff
is not inherited and all is well.
So my suggested fix is to just replace InheritableThreadLocal with a regular
ThreadLocal. Or at least provide a simple out of the box way of switching.
Plus both mechanisms should be consistent and the documentation
(http://logging.apache.org/log4j/2.x/manual/thread-context.html) has to be
fixed. There is a vague paragraph there talking about this problem but
offering no sane solution.
> Use of InheritableThreadLocal in Map ThreadContext is dangerous and unhelpful
> -----------------------------------------------------------------------------
>
> Key: LOG4J2-479
> URL: https://issues.apache.org/jira/browse/LOG4J2-479
> Project: Log4j 2
> Issue Type: Bug
> Reporter: MK
>
> Described here http://logging.apache.org/log4j/2.x/manual/thread-context.html
> The use of InheritableThreadLocal creates subtle and hard to track bugs while
> not really adding much useful. It is counterintuitive -- I don't see why
> would anyone expect logging context to be inherited. But it breaks down
> completely when used with Thread Executors.
--
This message was sent by Atlassian JIRA
(v6.1.5#6160)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]