|
56 | 56 | import org.apache.tomcat.util.http.parser.HttpParser; |
57 | 57 | import org.apache.tomcat.util.http.parser.Priority; |
58 | 58 | import org.apache.tomcat.util.net.ApplicationBufferHandler; |
| 59 | +import org.apache.tomcat.util.net.SocketEvent; |
59 | 60 | import org.apache.tomcat.util.net.WriteBuffer; |
60 | 61 | import org.apache.tomcat.util.res.StringManager; |
61 | 62 |
|
@@ -114,6 +115,11 @@ class Stream extends AbstractNonZeroStream implements HeaderEmitter { |
114 | 115 |
|
115 | 116 | private final Object recycledLock = new Object(); |
116 | 117 | private volatile boolean recycled = false; |
| 118 | + // The processor handling the request for this stream. Set when the processor is created. |
| 119 | + private volatile StreamProcessor streamProcessor = null; |
| 120 | + // A stream error raised by the connection thread while the processor was still processing the request. |
| 121 | + private volatile StreamException streamError = null; |
| 122 | + private final AtomicBoolean streamErrorNotified = new AtomicBoolean(false); |
117 | 123 |
|
118 | 124 |
|
119 | 125 | Stream(Integer identifier, Http2UpgradeHandler handler) { |
@@ -885,6 +891,74 @@ final boolean isInputFinished() { |
885 | 891 | } |
886 | 892 |
|
887 | 893 |
|
| 894 | + /** |
| 895 | + * Closes the stream following a stream error detected by the connection thread while processing incoming frames. |
| 896 | + * <p> |
| 897 | + * If a {@link StreamProcessor} is still processing the request, the request is, or may become, asynchronous. A |
| 898 | + * waiting processor can only complete its clean-up (remove itself from the waiting processors and recycle itself) |
| 899 | + * when it processes an event so it must be notified of the error. If the request is already in asynchronous mode |
| 900 | + * that is done here. Otherwise it is done by the processor once the request becomes asynchronous. |
| 901 | + * |
| 902 | + * @param streamException The stream error |
| 903 | + */ |
| 904 | + @SuppressWarnings("null") // processor is not null when it is referenced |
| 905 | + final void closeOnStreamError(StreamException streamException) { |
| 906 | + StreamProcessor processor = streamProcessor; |
| 907 | + close(streamException); |
| 908 | + if (processor != null && !recycled) { |
| 909 | + /* |
| 910 | + * Record the error before checking if the request is asynchronous. The processor checks for a recorded |
| 911 | + * error after it has started asynchronous processing (see notifyProcessorOfStreamError()). With both |
| 912 | + * fields volatile, at least one of the two threads is guaranteed to see the other's update so the processor |
| 913 | + * is always notified, whichever thread wins the race. |
| 914 | + */ |
| 915 | + streamError = streamException; |
| 916 | + if (processor.isAsync()) { |
| 917 | + notifyProcessorOfStreamError(processor); |
| 918 | + } |
| 919 | + } |
| 920 | + } |
| 921 | + |
| 922 | + |
| 923 | + /** |
| 924 | + * Called by the {@link StreamProcessor} once the request has started asynchronous processing. If the connection |
| 925 | + * thread raised a stream error before asynchronous processing started it will not have notified the processor, so |
| 926 | + * do so now. Without the notification, nothing would clean up the processor until the asynchronous timeout fired |
| 927 | + * (or never, if there is no timeout). |
| 928 | + * |
| 929 | + * @param processor The processor for this stream |
| 930 | + */ |
| 931 | + final void notifyProcessorOfStreamError(StreamProcessor processor) { |
| 932 | + // Notify once only, whichever thread gets here first |
| 933 | + if (streamError != null && !recycled && streamErrorNotified.compareAndSet(false, true)) { |
| 934 | + /* |
| 935 | + * The request is deliberately not touched here. This may be called from a thread that does not own the |
| 936 | + * request and the attributes of the request are not thread-safe. The processor sets the error attribute on |
| 937 | + * the request when it processes the event (see StreamProcessor.process()). |
| 938 | + * |
| 939 | + * If the processor has already cleaned up before the event is processed, it ignores the event. |
| 940 | + */ |
| 941 | + processor.processSocketEvent(SocketEvent.ERROR, true); |
| 942 | + } |
| 943 | + } |
| 944 | + |
| 945 | + |
| 946 | + /** |
| 947 | + * Obtain the stream error, if any, that was raised by the connection thread while the request for this stream was |
| 948 | + * still being processed. |
| 949 | + * |
| 950 | + * @return The stream error or {@code null} |
| 951 | + */ |
| 952 | + final StreamException getStreamError() { |
| 953 | + return streamError; |
| 954 | + } |
| 955 | + |
| 956 | + |
| 957 | + final void setStreamProcessor(StreamProcessor streamProcessor) { |
| 958 | + this.streamProcessor = streamProcessor; |
| 959 | + } |
| 960 | + |
| 961 | + |
888 | 962 | final void close(Http2Exception http2Exception) { |
889 | 963 | if (http2Exception instanceof ConnectionException) { |
890 | 964 | handler.closeConnection(http2Exception); |
|
0 commit comments