From be8bac09532bb56338e29980bd0b0e0e7346dfb0 Mon Sep 17 00:00:00 2001 From: Matheus Buschermoehle Date: Wed, 27 Nov 2024 20:59:42 -0300 Subject: [PATCH] refactor: extract method from a complex thread call method --- .idea/.name | 1 - .../hudson/cli/FlightRecorderInputStream.java | 38 +++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) delete mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 608ccbb80aad..000000000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -jenkins diff --git a/cli/src/main/java/hudson/cli/FlightRecorderInputStream.java b/cli/src/main/java/hudson/cli/FlightRecorderInputStream.java index 41d7a719dd09..92028021288c 100644 --- a/cli/src/main/java/hudson/cli/FlightRecorderInputStream.java +++ b/cli/src/main/java/hudson/cli/FlightRecorderInputStream.java @@ -52,12 +52,29 @@ public DiagnosedStreamCorruptionException analyzeCrash(Exception problem, String final ByteArrayOutputStream readAhead = new ByteArrayOutputStream(); final IOException[] error = new IOException[1]; - Thread diagnosisThread = new Thread(diagnosisName + " stream corruption diagnosis thread") { + Thread diagnosisThread = createDiagnosisThread(diagnosisName, readAhead, error); + + diagnosisThread.start(); + try { + diagnosisThread.join(1000); + } catch (InterruptedException ignored) { + Thread.currentThread().interrupt(); + } + + if (diagnosisThread.isAlive()) { + diagnosisThread.interrupt(); + } + + IOException diagnosisProblem = error[0]; + return new DiagnosedStreamCorruptionException(problem, diagnosisProblem, getRecord(), readAhead.toByteArray()); + } + + private Thread createDiagnosisThread(String diagnosisName, ByteArrayOutputStream readAhead, IOException[] error) { + return new Thread(diagnosisName + " stream corruption diagnosis thread") { @Override public void run() { int b; try { - // not all InputStream will look for the thread interrupt flag, so check that explicitly to be defensive while (!Thread.interrupted() && (b = source.read()) != -1) { readAhead.write(b); } @@ -66,23 +83,6 @@ public void run() { } } }; - - // wait up to 1 sec to grab as much data as possible - diagnosisThread.start(); - try { - diagnosisThread.join(1000); - } catch (InterruptedException ignored) { - // we are only waiting for a fixed amount of time, so we'll pretend like we were in a busy loop - Thread.currentThread().interrupt(); - // fall through - } - - IOException diagnosisProblem = error[0]; // capture the error, if any, before we kill the thread - if (diagnosisThread.isAlive()) - diagnosisThread.interrupt(); // if it's not dead, kill - - return new DiagnosedStreamCorruptionException(problem, diagnosisProblem, getRecord(), readAhead.toByteArray()); - } @Override