From 007c3b9df12145f5175a7dd17c476a01e6f1f5b8 Mon Sep 17 00:00:00 2001 From: Richard North Date: Sun, 23 Sep 2018 20:56:29 +0100 Subject: [PATCH] Guard against potential NPE if startup fails before container creation (#876) --- .../containers/GenericContainer.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 0c4c2dd5a6a..32cae84951a 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -296,18 +296,20 @@ private void tryStart(Profiler profiler) { } catch (Exception e) { logger().error("Could not start container", e); - // Log output if startup failed, either due to a container failure or exception (including timeout) - logger().error("Container log output (if any) will follow:"); - FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback(); - resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger())); - resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger())); - dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback); - - // Try to ensure that container log output is shown before proceeding - try { - resultCallback.getCompletionLatch().await(1, TimeUnit.MINUTES); - } catch (InterruptedException ignored) { - // Cannot do anything at this point + if (containerId != null) { + // Log output if startup failed, either due to a container failure or exception (including timeout) + logger().error("Container log output (if any) will follow:"); + FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback(); + resultCallback.addConsumer(STDOUT, new Slf4jLogConsumer(logger())); + resultCallback.addConsumer(STDERR, new Slf4jLogConsumer(logger())); + dockerClient.logContainerCmd(containerId).withStdOut(true).withStdErr(true).exec(resultCallback); + + // Try to ensure that container log output is shown before proceeding + try { + resultCallback.getCompletionLatch().await(1, TimeUnit.MINUTES); + } catch (InterruptedException ignored) { + // Cannot do anything at this point + } } throw new ContainerLaunchException("Could not create/start container", e);