[GitHub] [beam] TheNeuralBit commented on a change in pull request #13345: [BEAM-4136] Keep strong reference to loggers to avoid potential NPE

2020-11-16 Thread GitBox


TheNeuralBit commented on a change in pull request #13345:
URL: https://github.com/apache/beam/pull/13345#discussion_r524431127



##
File path: 
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClientTest.java
##
@@ -146,20 +147,25 @@ public void testLogging() throws Exception {
   apiServiceDescriptor,
   (Endpoints.ApiServiceDescriptor descriptor) -> channel);
 
+  // Keep a strong reference to the loggers in this block. Otherwise the 
call to client.close()
+  // removes the only reference and the logger may get GC'd (BEAM-4136).
+  Logger rootLogger = LogManager.getLogManager().getLogger("");
+  Logger configuredLogger = 
LogManager.getLogManager().getLogger("ConfiguredLogger");
+
   // Ensure that log levels were correctly set.
-  assertEquals(Level.OFF, 
LogManager.getLogManager().getLogger("").getLevel());
-  assertEquals(Level.FINE, 
LogManager.getLogManager().getLogger("ConfiguredLogger").getLevel());
+  assertEquals(Level.OFF, rootLogger.getLevel());
+  assertEquals(Level.FINE, configuredLogger.getLevel());
 
   // Should be filtered because the default log level override is OFF
-  LogManager.getLogManager().getLogger("").log(FILTERED_RECORD);
+  rootLogger.log(FILTERED_RECORD);
   // Should not be filtered because the default log level override for 
ConfiguredLogger is DEBUG
-  
LogManager.getLogManager().getLogger("ConfiguredLogger").log(TEST_RECORD);
-  
LogManager.getLogManager().getLogger("ConfiguredLogger").log(TEST_RECORD_WITH_EXCEPTION);
+  configuredLogger.log(TEST_RECORD);
+  configuredLogger.log(TEST_RECORD_WITH_EXCEPTION);
   client.close();
 
   // Verify that after close, log levels are reset.
-  assertEquals(Level.INFO, 
LogManager.getLogManager().getLogger("").getLevel());
-  
assertNull(LogManager.getLogManager().getLogger("ConfiguredLogger").getLevel());
+  assertEquals(Level.INFO, rootLogger.getLevel());
+  assertNull(configuredLogger.getLevel());
 
   assertTrue(clientClosedStream.get());
   assertTrue(channel.isShutdown());

Review comment:
   ok I fixed the other tests as well, PTAL





This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org




[GitHub] [beam] TheNeuralBit commented on a change in pull request #13345: [BEAM-4136] Keep strong reference to loggers to avoid potential NPE

2020-11-13 Thread GitBox


TheNeuralBit commented on a change in pull request #13345:
URL: https://github.com/apache/beam/pull/13345#discussion_r523336104



##
File path: 
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/logging/BeamFnLoggingClientTest.java
##
@@ -146,20 +147,25 @@ public void testLogging() throws Exception {
   apiServiceDescriptor,
   (Endpoints.ApiServiceDescriptor descriptor) -> channel);
 
+  // Keep a strong reference to the loggers in this block. Otherwise the 
call to client.close()
+  // removes the only reference and the logger may get GC'd (BEAM-4136).
+  Logger rootLogger = LogManager.getLogManager().getLogger("");
+  Logger configuredLogger = 
LogManager.getLogManager().getLogger("ConfiguredLogger");
+
   // Ensure that log levels were correctly set.
-  assertEquals(Level.OFF, 
LogManager.getLogManager().getLogger("").getLevel());
-  assertEquals(Level.FINE, 
LogManager.getLogManager().getLogger("ConfiguredLogger").getLevel());
+  assertEquals(Level.OFF, rootLogger.getLevel());
+  assertEquals(Level.FINE, configuredLogger.getLevel());
 
   // Should be filtered because the default log level override is OFF
-  LogManager.getLogManager().getLogger("").log(FILTERED_RECORD);
+  rootLogger.log(FILTERED_RECORD);
   // Should not be filtered because the default log level override for 
ConfiguredLogger is DEBUG
-  
LogManager.getLogManager().getLogger("ConfiguredLogger").log(TEST_RECORD);
-  
LogManager.getLogManager().getLogger("ConfiguredLogger").log(TEST_RECORD_WITH_EXCEPTION);
+  configuredLogger.log(TEST_RECORD);
+  configuredLogger.log(TEST_RECORD_WITH_EXCEPTION);
   client.close();
 
   // Verify that after close, log levels are reset.
-  assertEquals(Level.INFO, 
LogManager.getLogManager().getLogger("").getLevel());
-  
assertNull(LogManager.getLogManager().getLogger("ConfiguredLogger").getLevel());
+  assertEquals(Level.INFO, rootLogger.getLevel());
+  assertNull(configuredLogger.getLevel());
 
   assertTrue(clientClosedStream.get());
   assertTrue(channel.isShutdown());

Review comment:
   Out of time for today, but I just noticed this can happen in other tests 
as well. For example in [this 
run](https://github.com/apache/beam/pull/13128/checks?check_run_id=1398707923) 
we see the same flake in `testWhenServerHangsUpEarlyThatClientIsAbleCleanup`
   
   Leaving a note here so I remember to fix it later





This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org