Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-367

MDC of parent thread can be wiped from child thread

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • 0.9.28
    • None
    • None

    Description

      In our web framework we're using the MDC, but because we use multiple rendering threads we need to copy context from one thread to another. With log4j this worked fine, with logback it doesn't. It seems that there is an issue at least with setContextMap, which can influence the MDC in other (non child) threads.

      I've reproduced one such issue in a small project:

      https://github.com/iwein/logback-testdrive

      Look at this testcase in particular:

          @Test
          public void shouldCopyContextToOtherThread() throws Exception {
              final AtomicReference<Map> contextMap = new AtomicReference<Map>();
      
              MDC.put("first", "Dorothy");
              MDC.put("last", "Parker");
      
              contextMap.set(MDC.getCopyOfContextMap());
      
              //some context is set in a different thread
              Executors.newSingleThreadExecutor().execute(new Runnable() {
                  public void run() {
                      //not needed, but shouldn't break anything in the parent right?
                      MDC.setContextMap(contextMap.get());
                      logger.info("Check enclosed.");
                  }
              });
      
              //If I don't do this I have lost my context. That seems wrong..
              //MDC.setContextMap(contextMap.get());
      
              logger.debug("The most beautiful two words in English.");
          }
      

      I'll try to reproduce the issue with some sibling threads for the heck of it, but I think the above already illustrates the problem.

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            iwein Iwein Fuld
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated: