Nikolay Gorylenko opened SPR-16846 and commented

sample of truncated payload (webflux 5.0.5, payload extracted from ServerRequest via .bodyToMono(String.class)):

{"object":"page","entry":[{"id":"161041321255998","time":1526695801986,"messaging":[{"sender":{"id":"1675373035849843"},"recipient":{"id":"161041321255998"},"timestamp":1526695800993,"message":{"mid":"mid.$cAACSd3rlhiVpp254oVjdikvx-1mL","seq":536336,"attachments":[{"title":"Cupcake Corner Bakery","url":"https:\/\/l.facebook.com\/l.php?u=https\u00253A\u00252F\u00252Fwww.bing.com\u00252Fmaps\u00252Fdefault.aspx\u00253Fv\u00253D2\u002526pc\u00253DFACEBK\u002526mid\u00253D8100\u002526where1\u00253DPiotra\u00252BMicha\u002525C5\u00252582owskiego\u00252B14\u0025252C\u00252B31-326\u00252B\u002525D0\u0025259A\u002525D1\u00252580\u0

spring-web 5.0.5 (full payload extracted via @RequestBody into String variable):

{"object":"page","entry":[{"id":"161041321255998","time":1526701141870,"messaging":[{"sender":{"id":"1675373035849843"},"recipient":{"id":"161041321255998"},"timestamp":1526701140949,"message":{"mid":"mid.$cAACSd3rlhiVpp7_z1VjdnqpzbMei","seq":536509,"attachments":[{"title":"Cupcake Corner Bakery","url":"https:\/\/l.facebook.com\/l.php?u=https\u00253A\u00252F\u00252Fwww.bing.com\u00252Fmaps\u00252Fdefault.aspx\u00253Fv\u00253D2\u002526pc\u00253DFACEBK\u002526mid\u00253D8100\u002526where1\u00253DPiotra\u00252BMicha\u002525C5\u00252582owskiego\u00252B14\u0025252C\u00252B31-326\u00252B\u002525D0\u0025259A\u002525D1\u00252580\u002525D0\u002525B0\u002525D0\u002525BA\u002525D0\u002525BE\u002525D0\u002525B2\u002526FORM\u00253DFBKPL1\u002526mkt\u00253Den-US&h=ATM-Sf5N5LphePMuIUa6vwD-qLpC2Lp-hPDo_tvQRa5xlzMy6mLjPP9pdGqmsHmyA4PVrDc_0-X5-8cgAdcUYg78AN3g2qKQBOa95fE0451S_iRPaXR4g67VtMZsUc0&s=1","type":"location","payload":{"coordinates":{"lat":50.06582,"long":19.92785}}}]}}]}]}


Affects: 5.0.5

Attachments: - data.json (942 bytes)

Referenced from: commits https://github.com/spring-projects/spring-framework-issues/commit/3a33b18f47312cedfb92f56496562004443a0188

Comment From: spring-projects-issues

Nikolay Gorylenko commented

Original issue: https://github.com/messenger4j/messenger4j/issues/86

Comment From: spring-projects-issues

Brian Clozel commented

Hi Nikolay Gorylenko, Could you provide us a minimal, sample application that we can run that reproduces the issue? We do have tests covering this feature so we need more info to narrow down the issue here.

Thanks!

Comment From: spring-projects-issues

Nikolay Gorylenko commented

Hi Brian, that would be an overkill for me, sorry - this code is a part of bigger application that is developed under NDA.

Maybe some day in the future, but i provided already everything necessary - frameworks and their respective versions, and two payload samples.

Comment From: spring-projects-issues

Rossen Stoyanchev commented

I created a basic sample based on the description but it doesn't reproduce the issue. We will need more details about the scenario, configuration, or anything else that might be relevant to narrow this down. Or if you could update the sample to demonstrate the issue.

Comment From: spring-projects-issues

Nikolay Gorylenko commented

@rstoya05-aop sure, will try to demonstrate. Got deadline on June 20th, will do my best to provide you reproducible sample.

Maybe there is smth with transitive dependency versions - will see.

Thanks for attention, anyway

Comment From: spring-projects-issues

Daniel commented

@bclozel, @rstoya05-aop this is reproducible here: https://github.com/worldtiki/springwebflux

Start the app and send a POST with the content of [^data.json]:

curl http://localhost:8080/api/v2/spans -H "Content-Type:application/json" -d @data.json 

Expected: * the entire json is printed

Actual: * the string printed is truncated

Comment From: spring-projects-issues

Brian Clozel commented

Hi Nikolay Gorylenko,

Thanks for providing that repro project - I know creating a sample project can be really time consuming and frustrating at times, but this often leads to reporters finding a typo hiding somewhere, or to a misconception hiding in plain sight. In both cases, it's really hard to address the issue with just issue comments.

With this issue, we're actually facing a misunderstanding.

The Spring WebFlux functional handlers return a Mono<ServerResponse> in their contract. This means that not only the handler must provide a response in a reactive way, but it also means that when the Mono has been completed, the framework can safely assume that the underlying request/response exchange and all associated resources can be released.

In this case, calling subscribe on the incoming body separates out the processing of the incoming body from the handling of the response. Because the response is so short, it's written quickly and the exchange is closed before the request body can be fully read. Instead of depending from each other, the request and response processing are made independent by your subscribe call. As a general rule, you should never call subscribe nor block within a Controller handler.

Updating your sample with the following does not show the problem:

public Mono<ServerResponse> addSpans(ServerRequest serverRequest) {
 return serverRequest
 .bodyToMono(String.class)
 .doOnNext(s -> System.out.println(s))
 .then(ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)
 .body(BodyInserters.fromObject("Hello!")));
}

Don't hesitate to ask questions on StackOverflow in the future, the team is actively monitoring tags there to help the commnunity. I'm now closing this issue as invalid.

Comment From: spring-projects-issues

Brian Clozel commented

Whoops,

Just realized that sample was provided by Daniel and not Nikolay Gorylenko.

I'm reopening this issue until Nikolay has a chance to provide their own repro project.

Comment From: rameezshafsad20

Nikolay Gorylenko - were you able to find a solution for this issue? I am currently facing the same issue wherein the payload is being truncated and the last few frames are not sent back to the websocket client. I am using protocol buffers for the message.