-
Notifications
You must be signed in to change notification settings - Fork 71
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
Define OutputStreamTaskListener
& close BufferedBuildListener
#294
Changes from all commits
c57af92
89dc150
9b7f964
14eedc2
59f1af2
12f6b09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2023 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.workflow.log; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.model.TaskListener; | ||
import java.io.FilterOutputStream; | ||
import java.io.OutputStream; | ||
import java.io.PrintStream; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InaccessibleObjectException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import org.apache.commons.io.output.ClosedOutputStream; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.Beta; | ||
|
||
/** | ||
* {@link TaskListener} which can directly return an {@link OutputStream} not wrapped in a {@link PrintStream}. | ||
* This is important for logging since the error-swallowing behavior of {@link PrintStream} is unwanted, | ||
* and {@link PrintStream#checkError} is useless. | ||
*/ | ||
@Restricted(Beta.class) | ||
public interface OutputStreamTaskListener extends TaskListener { | ||
|
||
/** | ||
* Returns the {@link OutputStream} from which {@link #getLogger} was constructed. | ||
*/ | ||
@NonNull OutputStream getOutputStream(); | ||
|
||
/** | ||
* Tries to call {@link #getOutputStream} and otherwise falls back to reflective access to {@link PrintStream#out} when possible, at worst returning the {@link PrintStream} itself. | ||
*/ | ||
static @NonNull OutputStream getOutputStream(@NonNull TaskListener listener) { | ||
if (listener instanceof OutputStreamTaskListener) { | ||
return ((OutputStreamTaskListener) listener).getOutputStream(); | ||
} | ||
PrintStream ps = listener.getLogger(); | ||
if (ps.getClass() != PrintStream.class) { | ||
Logger.getLogger(OutputStreamTaskListener.class.getName()).warning(() -> "Unexpected PrintStream subclass " + ps.getClass().getName() + " which might override write(…); error handling is degraded unless OutputStreamTaskListener is used: " + listener.getClass().getName()); | ||
return ps; | ||
} | ||
if (Runtime.version().compareToIgnoreOptional(Runtime.Version.parse("17")) >= 0) { | ||
Logger.getLogger(OutputStreamTaskListener.class.getName()).warning(() -> "On Java 17+ error handling is degraded unless OutputStreamTaskListener is used: " + listener.getClass().getName()); | ||
Comment on lines
+63
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a stack trace to these messages? Otherwise I don't really see how a Jenkins developer would be able to track down the problematic code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is just with the named class. A stack trace would not really add anything important. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the interesting thing is where that class is being used, right? E.g. If I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should only happen for custom There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ok, probably fine then.
Sure, but it was not obvious to me what classes we would expect here. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case probably, but something is already pretty broken then. The warning could be limited to classes defined in plugins, and/or for that error handler we could make a special implementation. I can file a follow-up to refine a little bit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, it does not seem particularly important given your explanation, it just wasn't totally clear to me what kinds of scenarios we should anticipate. |
||
return ps; | ||
} | ||
Field printStreamDelegate; | ||
try { | ||
printStreamDelegate = FilterOutputStream.class.getDeclaredField("out"); | ||
} catch (NoSuchFieldException x) { | ||
Logger.getLogger(OutputStreamTaskListener.class.getName()).log(Level.WARNING, "PrintStream.out defined in Java Platform and protected, so should not happen.", x); | ||
return ps; | ||
} | ||
try { | ||
printStreamDelegate.setAccessible(true); | ||
} catch (InaccessibleObjectException x) { | ||
Logger.getLogger(OutputStreamTaskListener.class.getName()).warning(() -> "Using --illegal-access=deny? Error handling is degraded unless OutputStreamTaskListener is used: " + listener.getClass().getName()); | ||
return ps; | ||
} | ||
OutputStream os; | ||
try { | ||
os = (OutputStream) printStreamDelegate.get(ps); | ||
} catch (IllegalAccessException x) { | ||
Logger.getLogger(OutputStreamTaskListener.class.getName()).log(Level.WARNING, "Unexpected failure to access PrintStream.out", x); | ||
return ps; | ||
} | ||
if (os == null) { | ||
// like PrintStream.ensureOpen | ||
return ClosedOutputStream.CLOSED_OUTPUT_STREAM; | ||
} | ||
return os; | ||
} | ||
|
||
/** | ||
* Convenience implementation handling {@link #getLogger}. | ||
*/ | ||
abstract class Default implements OutputStreamTaskListener { | ||
|
||
private transient PrintStream ps; | ||
|
||
@Override public synchronized PrintStream getLogger() { | ||
if (ps == null) { | ||
ps = new PrintStream(getOutputStream(), false, StandardCharsets.UTF_8); | ||
} | ||
return ps; | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://issues.jenkins.io/browse/JENKINS-71164 FTR