Skip to content

Commit

Permalink
[LOGMGR-343] Check the thread id and name of a record when entering t…
Browse files Browse the repository at this point in the history
…he AsyncHandler and do not add it to the queue if there is a match.

https://issues.redhat.com/browse/LOGMGR-343
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Nov 10, 2023
1 parent 1b97737 commit 30c932e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/jboss/logmanager/handlers/AsyncHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ protected void doPublish(final ExtLogRecord record) {
// Copy the MDC over
record.copyMdc();
}
if (Thread.currentThread() == thread) {
publishToNestedHandlers(record);
return;
}
if (overflowAction == OverflowAction.DISCARD) {
recordQueue.offer(record);
} else {
Expand Down
36 changes: 34 additions & 2 deletions src/test/java/org/jboss/logmanager/handlers/AsyncHandlerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setup() {
handler = new BlockingQueueHandler();

asyncHandler = new AsyncHandler();
asyncHandler.setOverflowAction(OverflowAction.DISCARD);
asyncHandler.setOverflowAction(OverflowAction.BLOCK);
asyncHandler.addHandler(handler);
}

Expand Down Expand Up @@ -99,6 +99,37 @@ public void testMdc() throws Exception {
Assertions.assertEquals(mdcValue, handler.getFirst());
}

@Test
public void reentry() throws Exception {
final ExtHandler reLogHandler = new ExtHandler() {
private final ThreadLocal<Boolean> entered = ThreadLocal.withInitial(() -> false);

@Override
protected void doPublish(final ExtLogRecord record) {
if (entered.get()) {
return;
}
try {
entered.set(true);
super.doPublish(record);
// Create a new record and act is if this was through a logger
asyncHandler.publish(createRecord());
} finally {
entered.set(false);
}
}
};
handler.addHandler(reLogHandler);
handler.setFormatter(new PatternFormatter("%s"));
asyncHandler.publish(createRecord());
// We should end up with two messages and a third should not happen
Assertions.assertEquals("Test message", handler.getFirst());
Assertions.assertEquals("Test message", handler.getFirst());
// This should time out. Then we end up with a null value. We could instead sleep for a shorter time and check
// if the queue is empty. However, a 5 second delay does not seem too long.
Assertions.assertNull(handler.getFirst(), () -> "Expected no more entries, but found " + handler.queue);
}

static ExtLogRecord createRecord() {
return new ExtLogRecord(Level.INFO, "Test message", AsyncHandlerTests.class.getName());
}
Expand All @@ -107,13 +138,14 @@ static class BlockingQueueHandler extends ExtHandler {
private final BlockingDeque<String> queue;

BlockingQueueHandler() {
queue = new LinkedBlockingDeque<String>();
queue = new LinkedBlockingDeque<>();
setErrorManager(AssertingErrorManager.of());
}

@Override
protected void doPublish(final ExtLogRecord record) {
queue.addLast(getFormatter().format(record));
publishToNestedHandlers(record);
super.doPublish(record);
}

Expand Down

0 comments on commit 30c932e

Please sign in to comment.