[JERSEY-2692] Jersey runtime sets context for ChunkedOutput too late. Sometimes write happens before context is set and response is not committed correctly. Created: 22/Oct/14 Updated: 10/Sep/15 Resolved: 14/Jul/15 Status: Project: Component/s: Affects Version/s: Fix Version/s: Closed jersey core 2.10 Type: Reporter: Resolution: Labels: Remaining Estimate: Time Spent: Original Estimate: Environment: Bug amalysh Duplicate 1221 Not Specified Issue Links: Duplicate duplicates JERSEY-2558 ChunkedOutput.close() broken since Je... Closed 2.15 Priority: Assignee: Votes: Major Marek Potociar 0 Not Specified Not Specified Tomcat 7 + Jersey 2.10. Web app with Servlet 3.0 descriptor. Description I need to use ChunkedOutput in my project to control resources download speed. I have a simple example project (attached) which is very close to the ChunkedOutput usage example and Jersey resource in this project does following: 1) Creates ChunkedOutput. 2) Creates and starts thread which uses it. 3) Returns ChunkOutput object. Thread does the following: 1) Reads picture from classpath. 2) Writes it's byte[] representation to the chunked output. 3) Closes chunked output. In most of the invocations picture is shown in the browser correctly when I invoke this service. However, intermittently (in my case each first request after server is restarted), picture is not shown and browser is loading picture forever. This happens because response is not finished and the connection is not closed. I modified ChunkedOutput class (added logging in it - attached as well to the jira) and it shown that in good scenario method setContext() is called before both write() and close() methods are called: setContext(): 2014-10-21 18:27:22.749 2014-10-21 18:27:22.750 2014-10-21 18:27:22.750 2014-10-21 18:27:22.750 2014-10-21 18:27:22.750 write(): 2014-10-21 18:27:22.751 2014-10-21 18:27:22.751 2014-10-21 18:27:22.751 2014-10-21 18:27:22.751 2014-10-21 18:27:22.752 2014-10-21 18:27:22.752 2014-10-21 18:27:22.752 2014-10-21 18:27:22.752 2014-10-21 18:27:22.752 2014-10-21 18:27:22.752 2014-10-21 18:27:22.752 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 close(): 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.753 2014-10-21 18:27:22.754 2014-10-21 18:27:22.754 2014-10-21 18:27:22.755 2014-10-21 18:27:22.755 : : : : : flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() started inside try block inside call method poll 1 shouldClose = false in finally. : : : : : : : : : : : : : : : : : : write() started write() adding chunk write() added chunk flushQueue() started flushQueue() inside try block flushQueue() inside call method flushQueue() poll 1 shouldClose = false flushQueue() setting flushing flag flushQueue() inside second try block. flushQueue() flushing writtenStream. flushQueue() flushed writtenStream. flushQueue() second try block ended flushQueue() poll 2 flushQueue() poll 3 shouldClose = false flushQueue() committing stream. flushQueue() committed stream. flushQueue() in finally. write() ended : : : : : : : : : : : : : close() started flushQueue() started flushQueue() inside try block flushQueue() inside call method flushQueue() poll 1 shouldClose = true flushQueue() setting flushing flag flushQueue() in finally. flushQueue() closed = true flushQueue() closing response context flushQueue() response context closed flushQueue() releasing scope instance flushQueue() scope instance released close() ended but in bad scenario, setContext() is called after both methods. And this is causing an issue: write(): 2014-10-21 2014-10-21 2014-10-21 2014-10-21 close(): 2014-10-21 16:03:31.199 16:03:31.199 16:03:31.199 16:03:31.199 : : : : write() started write() adding chunk write() added chunk flushQueue() started 16:03:31.199 : write() ended 2014-10-21 16:03:31.199 2014-10-21 16:03:31.199 2014-10-21 16:03:31.199 setContext(): 2014-10-21 16:03:31.211 2014-10-21 16:03:31.211 2014-10-21 16:03:31.211 2014-10-21 16:03:31.212 2014-10-21 16:03:31.212 2014-10-21 16:03:31.212 2014-10-21 16:03:31.212 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 2014-10-21 16:03:31.213 : close() started : flushQueue() started : close() ended : : : : : : : : : : : : : : : : : : : flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() flushQueue() started inside try block inside call method poll 1 shouldClose = true setting flushing flag inside second try block. flushing writtenStream. flushed writtenStream. second try block ended poll 2 poll 3 shouldClose = true committing stream. committed stream. in finally. closed = true closing response context response context closed releasing scope instance scope instance released I understand it's not possible to set context before thread starts execution (because ChunkedOutput is created inside of the resource method), but, at least, you should either a)provide a way for designer to know (ideally get notified) when context is set on ChunkedOutput and it is ready for write() and close() method calls. b)implement ChunkedOutput class the way it's safe to call write() and close() methods even before context is set. It seems like it was designed that way, but it does not seem to be working. (I see that flushing happens successfully according to the logs, however response is not closed (data is sent to the client though). I think it's some interaction of Jersey code with async Servlet API. Comments Comment by amalysh [ 22/Oct/14 ] Unfortunately I can't add attachments so here is my Resource class: package com.test; import java.io.InputStream; import import import import javax.ws.rs.GET; javax.ws.rs.Path; javax.ws.rs.PathParam; javax.ws.rs.Produces; import org.glassfish.jersey.server.ChunkedOutput; import org.glassfish.jersey.server.MyChunkedOutput; @Path("/resource") public class AsyncResource { @GET @Path(value="/{path}") @Produces("image/png") public ChunkedOutput<byte[]> getChunkedResponse(@PathParam(value = "path") String path) { final MyChunkedOutput<byte[]> output = new MyChunkedOutput<byte[]>(byte[].class); new Thread() { public void run() { try { byte[] chunk = getNextChunk(); output.write(chunk); } catch (Throwable e) { // IOException thrown when writing the // chunks of response: should be handled e.printStackTrace(); } finally { try { output.close(); } catch (Exception e) { e.printStackTrace(); } } } }.start(); // the output will be probably returned even before // a first chunk is written by the new thread return output; } private byte[] getNextChunk() throws Exception { ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = cl.getResourceAsStream("com/test/trace.png"); byte[] bb = new byte[5000000]; int length = is.read(bb); byte[] result = new byte[length]; System.arraycopy(bb, 0, result, 0, length); return result; } } Comment by amalysh [ 22/Oct/14 ] And here is the modified ChunkedOutput class: /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * http://glassfish.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package org.glassfish.jersey.server; import import import import import import import import import import java.io.Closeable; java.io.IOException; java.io.OutputStream; java.lang.reflect.Type; java.util.Collections; java.util.concurrent.BlockingDeque; java.util.concurrent.Callable; java.util.concurrent.LinkedBlockingDeque; java.text.SimpleDateFormat; java.util.Date; import javax.ws.rs.container.ConnectionCallback; import javax.ws.rs.core.GenericType; import javax.ws.rs.ext.WriterInterceptor; import org.glassfish.jersey.internal.util.collection.Value; import org.glassfish.jersey.process.internal.RequestScope; import org.glassfish.jersey.server.internal.LocalizationMessages; import org.glassfish.jersey.server.internal.process.AsyncContext; import org.glassfish.jersey.server.internal.process.MappableException; /** * Used for sending messages in "typed" chunks. Useful for long running processes, * which needs to produce partial responses. * * @param <T> chunk type. * @author Pavel Bucek (pavel.bucek at oracle.com) * @author Martin Matula (martin.matula at oracle.com) * @author Marek Potociar (marek.potociar at oracle.com) */ // TODO: something like prequel/sequel - usable for EventChannelWriter and XML related writers public class ChunkedOutput<T> extends GenericType<T> implements Closeable { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); private static final byte[] ZERO_LENGTH_DELIMITER = new byte[0]; private final BlockingDeque<T> queue = new LinkedBlockingDeque<>(); private final byte[] chunkDelimiter; private private private private private private private private volatile boolean closed = false; boolean flushing = false; volatile RequestScope requestScope; volatile RequestScope.Instance requestScopeInstance; volatile ContainerRequest requestContext; volatile ContainerResponse responseContext; volatile ConnectionCallback connectionCallback; volatile Value<AsyncContext> asyncContext; /** * Create new {@code ChunkedOutput}. */ protected ChunkedOutput() { this.chunkDelimiter = ZERO_LENGTH_DELIMITER; } /** * Create {@code ChunkedOutput} with specified type. * * @param chunkType chunk type. Must not be {code null}. */ public ChunkedOutput(final Type chunkType) { super(chunkType); this.chunkDelimiter = ZERO_LENGTH_DELIMITER; } /** * Create new {@code ChunkedOutput} with a custom chunk delimiter. * * @param chunkDelimiter custom chunk delimiter bytes. Must not be {code null}. * @since 2.4.1 */ protected ChunkedOutput(byte[] chunkDelimiter) { if (chunkDelimiter.length > 0) { this.chunkDelimiter = new byte[chunkDelimiter.length]; System.arraycopy(chunkDelimiter, 0, this.chunkDelimiter, 0, chunkDelimiter.length); } else { this.chunkDelimiter = ZERO_LENGTH_DELIMITER; } } /** * Create new {@code ChunkedOutput} with a custom chunk delimiter. * * @param chunkType chunk type. Must not be {code null}. * @param chunkDelimiter custom chunk delimiter bytes. Must not be {code null}. * @since 2.4.1 */ public ChunkedOutput(final Type chunkType, byte[] chunkDelimiter) { super(chunkType); if (chunkDelimiter.length > 0) { this.chunkDelimiter = new byte[chunkDelimiter.length]; System.arraycopy(chunkDelimiter, 0, this.chunkDelimiter, 0, chunkDelimiter.length); } else { this.chunkDelimiter = ZERO_LENGTH_DELIMITER; } } /** * Create new {@code ChunkedOutput} with a custom chunk delimiter. * * @param chunkDelimiter custom chunk delimiter string. Must not be {code null}. * @since 2.4.1 */ protected ChunkedOutput(String chunkDelimiter) { if (chunkDelimiter.isEmpty()) { this.chunkDelimiter = ZERO_LENGTH_DELIMITER; } else { this.chunkDelimiter = chunkDelimiter.getBytes(); } } /** * Create new {@code ChunkedOutput} with a custom chunk delimiter. * * @param chunkType chunk type. Must not be {code null}. * @param chunkDelimiter custom chunk delimiter string. Must not be {code null}. * @since 2.4.1 */ public ChunkedOutput(final Type chunkType, String chunkDelimiter) { super(chunkType); if (chunkDelimiter.isEmpty()) { this.chunkDelimiter = ZERO_LENGTH_DELIMITER; } else { this.chunkDelimiter = chunkDelimiter.getBytes(); } } /** * Write a chunk. * * @param chunk a chunk instance to be written. * @throws IOException if this response is closed or when encountered any problem during serializing or writing a chunk. */ public void write(final T chunk) throws IOException { print("write() started"); if (closed) { throw new IOException(LocalizationMessages.CHUNKED_OUTPUT_CLOSED()); } print("write() adding chunk"); if (chunk != null) { queue.add(chunk); } print("write() added chunk"); flushQueue(); print("write() ended"); } private void flushQueue() throws IOException { print("flushQueue() started"); if (requestScopeInstance == null || requestContext == null || responseContext == null) { return; } Exception ex = null; try { print("flushQueue() inside try block"); requestScope.runInScope(requestScopeInstance, new Callable<Void>() { @Override public Void call() throws IOException { print("flushQueue() inside call method"); boolean shouldClose; T t; synchronized (this) { if (flushing) { print("flushQueue() first flushing check passed"); // if another thread is already flushing the queue, we don't have to do anything return null; } // remember the closed flag before polling the queue // (if we did it after, we could miss the last chunk as some other thread may add a chunk // and set closed to true right after we have polled the queue (i.e. we'd think the queue is empty), // but before we check if we should close - so we would close the stream leaving the last chunk // undelivered) shouldClose = closed; print("flushQueue() poll 1 shouldClose = " + shouldClose); t = queue.poll(); if (t != null || shouldClose) { print("flushQueue() setting flushing flag"); // no other thread is flushing this queue at the moment and it is not empty and/or we should close -> // set the flushing flag so that other threads know it is already being taken care of // and they don't have to bother flushing = true; } } while (t != null) { try { print("flushQueue() inside second try block."); final OutputStream origStream = responseContext.getEntityStream(); final OutputStream writtenStream = requestContext.getWorkers().writeTo( t, t.getClass(), getType(), responseContext.getEntityAnnotations(), responseContext.getMediaType(), responseContext.getHeaders(), requestContext.getPropertiesDelegate(), origStream, // The output stream stored in the response context for this chunked output // is already intercepted as a whole (if there are any interceptors); // no need to intercept the individual chunks. Collections.<WriterInterceptor>emptyList()); //noinspection ArrayEquality if (chunkDelimiter != ZERO_LENGTH_DELIMITER) { // if the chunked output is configured with a custom delimiter, use it writtenStream.write(chunkDelimiter); } print("flushQueue() flushing writtenStream."); // flush the chunk (some writers do it, but some don't) writtenStream.flush(); print("flushQueue() flushed writtenStream."); if (origStream != writtenStream) { // if MBW replaced the stream, let's make sure to set it in the response context. responseContext.setEntityStream(writtenStream); } print("flushQueue() second try block ended"); } catch (IOException ioe) { connectionCallback.onDisconnect(asyncContext.get()); throw ioe; } catch (MappableException mpe) { if (mpe.getCause() instanceof IOException) { connectionCallback.onDisconnect(asyncContext.get()); } throw mpe; } print("flushQueue() poll 2"); t = queue.poll(); if (t == null) { synchronized (this) { // queue seems empty // check again in the synchronized block before clearing the flushing flag // first remember the closed flag (this has to be before polling the queue, // otherwise we could miss the last chunk) shouldClose = closed; print("flushQueue() poll 3 shouldClose = " + shouldClose); t = queue.poll(); if (t == null) { // ok, it is really empty - if anyone adds a chunk while we are here, // other thread will take care of it -> flush the stream and unset // the flushing flag at the very end (to make sure it is unset only if no // exception is thrown) print("flushQueue() committing stream."); responseContext.commitStream(); print("flushQueue() committed stream."); // if closing, we keep the "flushing" flag set, since no other thread needs to flush // this queue anymore - finally clause will take care of closing the stream flushing = shouldClose; break; } } } } return null; } }); } catch (Exception e) { closed = true; // remember the exception (it will get rethrown from finally clause, once it does it's work) ex = e; } finally { print("flushQueue() in finally."); if (closed) { print("flushQueue() closed = true"); try { print("flushQueue() closing response context"); responseContext.close(); print("flushQueue() response context closed"); } catch (Exception e) { // if no exception remembered before, remember this one // otherwise the previously remembered exception (from catch clause) takes precedence ex = ex == null ? e : ex; } print("flushQueue() releasing scope instance"); requestScopeInstance.release(); print("flushQueue() scope instance released"); // rethrow remembered exception (if any) if (ex instanceof IOException) { //noinspection ThrowFromFinallyBlock throw (IOException) ex; } else if (ex instanceof RuntimeException) { //noinspection ThrowFromFinallyBlock throw (RuntimeException) ex; } } } } /** * Close this response - it will be finalized and underlying connections will be closed * or made available for another response. */ @Override public void close() throws IOException { print("close() started"); closed = true; flushQueue(); print("close() ended"); } /** * Get state information. * * Please note that {@code ChunkedOutput} can be closed by the client side - client can close connection * from its side. * * @return true when closed, false otherwise. */ public boolean isClosed() { return closed; } @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") @Override public boolean equals(final Object obj) { return this == obj; } @Override public int hashCode() { int result = super.hashCode(); result = 31 * result + queue.hashCode(); return result; } @Override public String toString() { return "ChunkedOutput<" + getType() + ">"; } /** * Set context used for writing chunks. * * @param requestScope request scope. * @param requestScopeInstance current request scope instance. * @param requestContext request context. * @param responseContext response context. * @param connectionCallbackRunner connection callback. * @param asyncContext async context value. * @throws IOException when encountered any problem during serializing or writing a chunk. */ void setContext(final RequestScope requestScope, final RequestScope.Instance requestScopeInstance, final ContainerRequest requestContext, final ContainerResponse responseContext, final ConnectionCallback connectionCallbackRunner, final Value<AsyncContext> asyncContext) throws IOException { this.requestScope = requestScope; this.requestScopeInstance = requestScopeInstance; this.requestContext = requestContext; this.responseContext = responseContext; this.connectionCallback = connectionCallbackRunner; this.asyncContext = asyncContext; flushQueue(); } public String date() { return sdf.format(new Date()); } public void print(String message) { System.out.println(date() + " : " + message); } } Comment by Adam Lindenthal [ 18/Dec/14 ] Hi Amalysh, thanks for submitting the issue. I am moving it to backlog, so that we can plan it into one of the future sprints. Regards, Adam Generated at Wed Feb 10 00:35:48 UTC 2016 using JIRA 6.2.3#6260sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.