Spring StreamingResponseBody Request Thread not cleared when client closes connection

I have an endpoint in a controller that returns StreamingResponseBody

which is used to send a file to the client. The code for this looks something like this:

@RestController
@RequestMapping(value="api")
public class Controller {
    static class GetContentResponse implements StreamingResponseBody {

        @Override
        public void writeTo(OutputStream outputStream) throws IOException {
            while (!finished) {
                try {
                    byte[] chunk = <get next chunk>;
                    outputStream.write(chunk);
                } catch (InterruptedException e) {
                    throw new RuntimeException("Interrupted!", e);
                }
            }
            outputStream.close();
        }
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<StreamingResponseBody> get(@PathVariable(value = "id") String id)
        throws FaultResponse, InterruptedException {

        GetContentResponse getContentResponse = new GetContentResponse();

        HttpHeaders header = new HttpHeaders();

        return new ResponseEntity<>(getContentResponse, header, HttpStatus.OK);
    }
}

      

I am setting a timeout for asynchronous requests via:

@Configuration
public class AsyncConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(5000);
    }
}

      

If the client cancels the request, the thread for the response hangs, does not disconnect or flush. Stream tags stack:

Name: MvcAsync9
State: TIMED_WAITING on java.util.concurrent.CountDownLatch$Sync@4c51a108
Total blocked: 1  Total waited: 31

Stack trace: 
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(AbstractQueuedSynchronizer.java:1037)
java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(AbstractQueuedSynchronizer.java:1328)
java.util.concurrent.CountDownLatch.await(CountDownLatch.java:277)
org.apache.tomcat.util.net.NioEndpoint$KeyAttachment.awaitLatch(NioEndpoint.java:1361)
org.apache.tomcat.util.net.NioEndpoint$KeyAttachment.awaitWriteLatch(NioEndpoint.java:1364)
org.apache.tomcat.util.net.NioBlockingSelector.write(NioBlockingSelector.java:114)
org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:172)
org.apache.coyote.http11.InternalNioOutputBuffer.writeToSocket(InternalNioOutputBuffer.java:139)
   - locked org.apache.coyote.http11.InternalNioOutputBuffer@62f2053a
org.apache.coyote.http11.InternalNioOutputBuffer.addToBB(InternalNioOutputBuffer.java:197)
   - locked org.apache.coyote.http11.InternalNioOutputBuffer@62f2053a
org.apache.coyote.http11.InternalNioOutputBuffer.access$000(InternalNioOutputBuffer.java:41)
org.apache.coyote.http11.InternalNioOutputBuffer$SocketOutputBuffer.doWrite(InternalNioOutputBuffer.java:320)
org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:84)
org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:256)
org.apache.coyote.Response.doWrite(Response.java:501)
org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:388)
org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:344)
org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:418)
org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:406)
org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:97)
org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:90)
org.springframework.security.web.context.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:537)
com.mackie.test.Controller$GetContentResponse.writeTo(Controller.java:98)
org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBodyReturnValueHandler$StreamingResponseBodyTask.call(StreamingResponseBodyReturnValueHandler.java:108)
org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBodyReturnValueHandler$StreamingResponseBodyTask.call(StreamingResponseBodyReturnValueHandler.java:94)
org.springframework.web.context.request.async.WebAsyncManager$4.run(WebAsyncManager.java:316)
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
java.util.concurrent.FutureTask.run(FutureTask.java:266)
java.lang.Thread.run(Thread.java:745)

      

How can I make sure the threads are properly destroyed when the client disconnects?

+3


source to share


1 answer


I found the problem myself: there was a proxy between the server and the client and it did not allow the connection to be closed correctly. Everything works fine without a proxy server.



0


source







All Articles