Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor FlightRecorderInputStream Class #10008

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

38 changes: 19 additions & 19 deletions cli/src/main/java/hudson/cli/FlightRecorderInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down
Loading