dropwizard org.eclipse.jetty.io.EofException:早期EOF异常

时间:2018-02-16 18:47:53

标签: java rest jersey dropwizard

我在某些请求中收到以下异常。它很少发生在一周一次或两次。任何想法可能导致这个或任何建议我应该在哪里调试这个。

Dropwizard版本:1.1.4 jetty-server版本:2.25.1

错误发生在下面的代码中:request.bufferEntity()

private String getBody(ContainerRequestContext requestContext) {
        if (requestContext instanceof ContainerRequest) {
            ContainerRequest request = (ContainerRequest) requestContext;
            // calling bufferEntity(), without this the entity is marked as closed and causes IllegalStateExceptions
            // on any subsequent read attempt
            request.bufferEntity();
            return request.readEntity(String.class);
        } else {
            // this should never happen as we are using jersey as the jax-rs implementation engine
            throw new RuntimeException("ContainerRequestContext is not an instance of jersey ContainerRequest");
        }
    }
  

无法缓冲邮件内容输入流。在   org.glassfish.jersey.message.internal.InboundMessageContext.bufferEntity(InboundMessageContext.java:931)     在   org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:132)     在   org.glassfish.jersey.server.ContainerFilteringStage.apply(ContainerFilteringStage.java:68)     在   org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)     在   org.glassfish.jersey.server.ServerRuntime $ 2.run(ServerRuntime.java:318)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)at at   org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)at   org.glassfish.jersey.internal.Errors.process(Errors.java:315)at   org.glassfish.jersey.internal.Errors.process(Errors.java:297)at   org.glassfish.jersey.internal.Errors.process(Errors.java:267)at   org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)     在   org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)     在   org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)     在   org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)     在   org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)     在   org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)     在   org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)         在org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)         在org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)         在org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)         在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)         在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)         在com.codahale.metrics.jetty9.InstrumentedHandler.handle(InstrumentedHandler.java:241)         at io.dropwizard.jetty.RoutingHandler.handle(RoutingHandler.java:52)         在org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:454)         at io.dropwizard.jetty.BiDiGzipHandler.handle(BiDiGzipHandler.java:68)         在org.eclipse.jetty.server.handler.StatisticsHandler.handle(StatisticsHandler.java:169)         在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)         在org.eclipse.jetty.server.Server.handle(Server.java:564)         在org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)         在org.eclipse.jetty.server.HttpChannelOverHttp.earlyEOF(HttpChannelOverHttp.java:239)         在org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:1444)         在org.eclipse.jetty.server.HttpConnection.parseRequestBuffer(HttpConnection.java:351)         在org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:234)         at org.eclipse.jetty.io.AbstractConnection $ ReadCallback.succeeded(AbstractConnection.java:279)         在org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)         在org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:289)         at org.eclipse.jetty.io.ssl.SslConnection $ 3.succeeded(SslConnection.java:149)         在org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)         在org.eclipse.jetty.io.ChannelEndPoint $ 2.run(ChannelEndPoint.java:124)         at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)         at org.eclipse.jetty.util.thread.Invocable $ InvocableExecutor.invoke(Invocable.java:222)         在org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)         在org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)         在org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:673)         在org.eclipse.jetty.util.thread.QueuedThreadPool $ 2.run(QueuedThreadPool.java:591)         在java.lang.Thread.run(Thread.java:748)       引起:org.eclipse.jetty.io.EofException:早期EOF         在org.eclipse.jetty.server.HttpInput $ 3.getError(HttpInput.java:1104)         at org.eclipse.jetty.server.HttpInput $ 3.noContent(HttpInput.java:1093)         在org.eclipse.jetty.server.HttpInput.read(HttpInput.java:307)         在java.io.InputStream.read(InputStream.java:101)         在org.glassfish.jersey.message.internal.ReaderWriter.writeTo(ReaderWriter.java:115)         at org.glassfish.jersey.message.internal.InboundMessageContext.bufferEntity(InboundMessageContext.java:918)         ...省略了68个常见帧

1 个答案:

答案 0 :(得分:0)

此问题在1.3.8中仍然存在,并且是open issue

好吧。似乎有一种解决方法。您可以创建一个过滤器类来过滤异常并检查Eof。这是针对我的情况的一种具体技巧。但是,您明白了。我已经对此进行了测试,并且可以正常工作。这是过滤器代码段。


  @Override
  public void doFilter(
      final ServletRequest request,
      final ServletResponse response,
      final FilterChain chain)
      throws IOException, ServletException {
    try {
      chain.doFilter(request, response);
    } catch (ServletException e) {
      if (isEarlyEofException(e)) {
        log.debug("EOF Exception encountered - client disconnected during stream processing.", e);

        if (response instanceof HttpServletResponse) {
          ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
      } else {
        throw e;
      }
    }
  }

  private boolean isEarlyEofException(final ServletException e) {
    return e.getCause() instanceof ProcessingException
        && e.getCause().getCause() instanceof EofException
        && e.getCause().getCause().getMessage().equals(EARLY_EOF_MSG);
  }

然后应用过滤器。

  private void addEarlyEofExceptionFilter(final Environment environment) {
    final FilterRegistration.Dynamic filter =
        environment.servlets().addFilter("EarlyEofExceptionFilter", EarlyEofExceptionFilter.class);

    filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
  }