Javalin: Async does not return the result body

Created on 9 Nov 2017  路  8Comments  路  Source: tipsy/javalin

The "async" does return the set status code, but not the body. The provided example does not checks that.

I have provided an updated example (see link below) where I do set a body for the 2 examples already provided by javalin and also added 2 examples using a real life async scenario: obtaining the data from a service that returns a CompletionStage (or CompletableFuture).

https://github.com/Adeynack/javalin/blob/async-does-not-response-with-body/src/test/kotlin/io/javalin/examples/AsyncExample.kt

From that example, we can observe the following results.

/test-custom

$ http http://localhost:5454/test-custom
HTTP/1.1 418 I'm a Teapot
Content-Length: 0
Content-Type: text/plain;charset=utf-8
Date: Thu, 09 Nov 2017 10:57:19 GMT
Server: Javalin

Body is empty.

/test-async

$ http http://localhost:5454/test-async
HTTP/1.1 418 I'm a Teapot
Content-Length: 0
Content-Type: text/plain;charset=utf-8
Date: Thu, 09 Nov 2017 10:57:56 GMT
Server: Javalin

Body is empty.

/test-future

Run 1

$ http http://localhost:5454/test-future
HTTP/1.1 418 I'm a Teapot
Content-Length: 13
Content-Type: text/plain;charset=utf-8
Date: Thu, 09 Nov 2017 10:58:50 GMT
Server: Javalin

I am a Teapot

With a body!

Run 2

$ http http://localhost:5454/test-future
HTTP/1.1 418 I'm a Teapot
Content-Length: 0
Content-Type: text/plain;charset=utf-8
Date: Thu, 09 Nov 2017 10:59:10 GMT
Server: Javalin

No more body :(

/test-future-long

$ http http://localhost:5454/test-future-long
HTTP/1.1 418 I'm a Teapot
Content-Length: 0
Content-Type: text/plain;charset=utf-8
Date: Thu, 09 Nov 2017 10:59:49 GMT
Server: Javalin

Body is empty.

BUG

All 8 comments

Also, the unit tests covering the async use cases do not assert that a body is returned. If needed, I can modify them and propose a PR for it. But to fix the async bug itself, I would need a lot more knowledge in the internals of the library (which at the moment would require more time than I can afford to spend in contributing, sadly).

Thanks for the thorough issue. I don't have a lot of experience with async (as you might have guessed), so it was added as an after-thought. I'll dig around a bit and see if I can get things working.

OK thanks. I will also on my side try to figure out if it is an easy fix or not. I am getting _fairly_ comfortable with async and multi-threading issues. Let's see what I can come up with.

OK thanks. I will also on my side try to figure out if it is an easy fix or not. I am getting fairly comfortable with async and multi-threading issues. Let's see what I can come up with.

Cool. Setting the response body itself isn't really a problem, you can just do something like

 simulateAsyncTask({
    ctx.status(418)
    ctx.response().getWriter().write("I'm a teapot");
    future.complete(null)
})

But I imagine after-filters/request logging will run right away, exception mappers won't work, and so on. Fixing this would be a bit of work 馃

i also dont have a body when i use a different thread. It does work though when I put Thread.sleep(5000) below something like

 simulateAsyncTask({
    ctx.status(418)
    ctx.response().getWriter().write("I'm a teapot");
})

Thread.sleep(5000)

Update: I will not have time to work on this issue as discussed before. I will leave it open since it is a bug and at some point will be need to be fixed.

@Adeynack, what do you think about removing this way of doing async in favor of ctx.result(future)?

I'll just close this, as it can't really be fixed in an acceptable way. I have deprecated the method.

Was this page helpful?
0 / 5 - 0 ratings