title: Jersey endpoint with SEVERE: An I/O error has occurred while writing a response message entity to the container output stream
body: <p>I have a jersey v2.30 application using <strong>jersey-container-grizzly2-http</strong> and <strong>jersey-grizzly-connector</strong>. I have an endpoint which returns a pdf file, the endpoint seems to work which means the client gets the file successfully, however on the server side I can see the exception below.</p>
  
  <p>From google seems like this error means the client is closing the connection and I would like to dig a little bit more and understand why is doing that and what can I do to stop this from happening.</p>
  
  <pre><code>Mar 26, 2020 10:58:56 AM org.glassfish.jersey.server.ServerRuntime$Responder writeResponse
  SEVERE: An I/O error has occurred while writing a response message entity to the container output stream.
  org.glassfish.jersey.server.internal.process.MappableException: java.io.IOException: Connection is closed
      at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:67)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
      at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1116)
      at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:638)
      at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:371)
      at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:361)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:256)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
      at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:356)
      at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:200)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: java.io.IOException: Connection is closed
      at org.glassfish.grizzly.nio.NIOConnection.assertOpen(NIOConnection.java:441)
      at org.glassfish.grizzly.http.io.OutputBuffer.write(OutputBuffer.java:663)
      at org.glassfish.grizzly.http.server.NIOOutputStreamImpl.write(NIOOutputStreamImpl.java:59)
      at org.glassfish.jersey.message.internal.CommittingOutputStream.write(CommittingOutputStream.java:200)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$UnCloseableOutputStream.write(WriterInterceptorExecutor.java:276)
      at org.glassfish.jersey.message.internal.ReaderWriter.writeTo(ReaderWriter.java:93)
      at org.glassfish.jersey.message.internal.AbstractMessageReaderWriterProvider.writeTo(AbstractMessageReaderWriterProvider.java:56)
      at org.glassfish.jersey.message.internal.InputStreamProvider.writeTo(InputStreamProvider.java:82)
      at org.glassfish.jersey.message.internal.InputStreamProvider.writeTo(InputStreamProvider.java:37)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:242)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:227)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
      at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:85)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
      at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:61)
      ... 19 more
  Caused by: java.io.IOException: Broken pipe
      at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
      at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
      at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
      at sun.nio.ch.IOUtil.write(IOUtil.java:51)
      at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:471)
      at org.glassfish.grizzly.nio.transport.TCPNIOUtils.flushByteBuffer(TCPNIOUtils.java:125)
      at org.glassfish.grizzly.nio.transport.TCPNIOUtils.writeCompositeBuffer(TCPNIOUtils.java:64)
      at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:105)
      at org.glassfish.grizzly.nio.transport.TCPNIOAsyncQueueWriter.write0(TCPNIOAsyncQueueWriter.java:82)
      at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:236)
      at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:145)
      at org.glassfish.grizzly.nio.AbstractNIOAsyncQueueWriter.write(AbstractNIOAsyncQueueWriter.java:47)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransportFilter.handleWrite(TCPNIOTransportFilter.java:102)
      at org.glassfish.grizzly.filterchain.TransportFilter.handleWrite(TransportFilter.java:167)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$8.execute(ExecutorResolver.java:87)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
      at org.glassfish.grizzly.filterchain.FilterChainContext.write(FilterChainContext.java:866)
      at org.glassfish.grizzly.filterchain.FilterChainContext.write(FilterChainContext.java:834)
      at org.glassfish.grizzly.http.io.OutputBuffer.flushBuffer(OutputBuffer.java:1068)
      at org.glassfish.grizzly.http.io.OutputBuffer.write(OutputBuffer.java:695)
      ... 32 more
  </code></pre>
  
  <p>This is the code:</p>
  
  <pre><code>@POST
  @Path("/au/last-mile-label")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces("application/pdf")
  public Object postLastMileAu(UberShipment shipment) {
  
      EntityManager em = MyPonyApp.emf.createEntityManager();
  
      String carrier = "apiProvider1";
  
      Response response = CarrierApiFactory.carrierApiMapping.get(carrier)
              .createDomesticShipmentAndCreateAndGetLabelLastMile(
                      em, 
                      shipment);
  
      em.close();
  
      return response;
  }
  
  @Override
  public Response createDomesticShipmentAndCreateAndGetLabelLastMile(EntityManager em, UberShipment uberShipment) {
  
      String shipmentId = this.createShipment(em, uberShipment, uberShipment.getLastMileOrigin(), uberShipment.getLastMileDestination());
  
      ...
  
      return this.getLabel(em, request_id);
  }
  
  public Response getLabel(EntityManager em, String request_id) {
  
      ...
  
      return getLabelFromUrl(jsonNode.get("labels").elements().next().get("url").textValue());
  }
  
  private Response getLabelFromUrl(String url) {
  
      Client client = ClientBuilder.newClient()
              .register(MultiPartFeature.class)
              .register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.INFO, null, null));
  
      client.property(ClientProperties.CONNECT_TIMEOUT, 2000);
      client.property(ClientProperties.READ_TIMEOUT, 20000);
  
      Invocation.Builder req = client.target(url)
              .request()
              .header("client_id", api_key);
  
      Response response = req.get(Response.class);
  
      return response;
  }
  </code></pre>
  
  <p>Any advice would be highly appreciated</p>
  
  <p>thank you</p>