micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Server request not available when controller binding parameter from json

Open kamer opened this issue 10 months ago • 1 comments

Expected Behavior

In controller parameter's constructor server request should be available via ServerRequestContext.currentRequest()

Actual Behaviour

When body payload is large, sometimes server request is not available via ServerRequestContext.currentRequest().

It was working in Micronaut 3.1.4. In that version, server request was always available in parameter constructor. Now with Micronaut 4.3.7, when request payload is small it's always available but when the request payload is large, in the first request after server is started, server request is not available in parameter constructor. In subsquent requests, even the payload is large (the same request) the server request becomes available.

In order the reproduce the behaviour, you should start/restart the server and make the first request with large payload.

You can find example requests in the reproducer project's README.

Steps To Reproduce

  1. Clone reproducer project from https://github.com/kamer/micronaut-server-request.
  2. Follow steps in README.

Environment Information

  • Arch Linux (6.7.8-arch1-1)
  • java 21.0.1 2023-10-17 Java(TM) SE Runtime Environment Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19) Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19, mixed mode, sharing)

Example Application

https://github.com/kamer/micronaut-server-request

Version

4.3.7

kamer avatar Mar 29 '24 14:03 kamer

Here's additional info.

  • I cannot reproduce this in Micronaut Core 4.2.4. It starts with 4.3.0. Commits between versions are here.
  • I wanted to compare a working request (first request after server is up) and a not working request (following requests). You can find below. Maybe it helps. image

If anyone gives a hint, I can debug further and try to create a pull request if needed.

kamer avatar Apr 18 '24 12:04 kamer

+1

lcavadas avatar Jun 12 '24 09:06 lcavadas

Not sure if it helps or not but while debugging it seems to only happen when the DelayedExecutionFlow is used. When it's not used it works just fine.

lcavadas avatar Jun 13 '24 17:06 lcavadas

The context is available on the controller, just not on the model. My use-case where I'm finding the issue is when trying to access the context from a Serde singleton.

lcavadas avatar Jun 13 '24 17:06 lcavadas

@kamer Quick fix above if you'd like to use.

@graemerocher @sdelamo Still not quite sure why but when the request is large and a DelayedExecutionFlow is used, the context isn't propagated. My quick fix is to simply propagate the context by adding it on the bindFullBody method since the request is available there and just isn't being propagated already. Might not be a good fix but it gets the job done since this bug is has pretty nasty consequences for my use-case.

lcavadas avatar Jun 13 '24 20:06 lcavadas