NPE when the client closes the connection while a static resource request is being processed

45 views
Skip to first unread message

Mihail Bratoev

unread,
Sep 21, 2016, 5:32:33 AM9/21/16
to vert.x
Hi,

I have the following code running on vertx 3.3.3:

public class SampleBlockingVerticle extends AbstractVerticle {

   protected static final StaticHandler staticHandler = StaticHandler.create("web-app");

   @Override

    public void start() throws Exception {

      Router router = Router.router(vertx);

      router.route().blockingHandler(context -> {

           try {

              Thread.sleep(5000);

              staticHandler.handle(context);

           } catch (Exception ignore) {}

      }, false);

     vertx.createHttpServer().requestHandler(router::accept).listen(8080);

  }

}


If I close the browser within the request execution timeframe (i.e. without waiting for the response to come back) I get this stacktrace:


java.lang.NullPointerException

at io.vertx.core.http.impl.HttpServerResponseImpl.doSendFile(HttpServerResponseImpl.java:491)

at io.vertx.core.http.impl.HttpServerResponseImpl.sendFile(HttpServerResponseImpl.java:342)

at io.vertx.core.http.HttpServerResponse.sendFile(HttpServerResponse.java:317)

at io.vertx.core.http.HttpServerResponse.sendFile(HttpServerResponse.java:303)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.lambda$sendFile$6(StaticHandlerImpl.java:386)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.wrapInTCCLSwitch(StaticHandlerImpl.java:229)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.sendFile(StaticHandlerImpl.java:375)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.lambda$sendStatic$0(StaticHandlerImpl.java:192)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.getFileProps(StaticHandlerImpl.java:275)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.sendStatic(StaticHandlerImpl.java:182)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.handle(StaticHandlerImpl.java:143)

at io.vertx.ext.web.handler.impl.StaticHandlerImpl.handle(StaticHandlerImpl.java:52)

at vip.vertx.app.SampleBlockingVerticle.lambda$0(SampleBlockingVerticle.java:22)

at io.vertx.ext.web.impl.BlockingHandlerDecorator.lambda$handle$0(BlockingHandlerDecorator.java:48)

at io.vertx.core.impl.ContextImpl.lambda$executeBlocking$1(ContextImpl.java:259)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:745)


On our production server we often have such exceptions for HTTP requests all of them with HTTP header - Connection: close

As a temporary fix we have modified HttpServerResponseImpl.doSendFile() to check if the channelFuture created on line 486 is null (i.e. connection has been closed) and handle this accordingly:

      channelFuture = conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT);

      written = true;


      if (resultHandler != null) {

        ContextImpl ctx = vertx.getOrCreateContext();

        if (channelFuture != null) {

        channelFuture.addListener(future -> {

          AsyncResult<Void> res;

          if (future.isSuccess()) {

            res = Future.succeededFuture();

          } else {

            res = Future.failedFuture(future.cause());

          }

          ctx.runOnContext((v) -> resultHandler.handle(res));

        });

        } else {

        resultHandler.handle(Future.failedFuture(new VertxException("channel has been closed by the client!")));

        }

      }


Could someone please take a look at this and eventually escalate the problem?


Regards,

Mihail



Thomas SEGISMONT

unread,
Sep 21, 2016, 9:20:28 AM9/21/16
to ve...@googlegroups.com
Please create an issue in vertx-web. If you have a fix then a PR (along with a test case) would be perfect.

Thanks!

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/c2f3966f-3fcd-4fd4-864e-561c0b490e9a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages