2

I am using a custom WebFilter in Spring Boot to gather diagnostic information about reactive HTTP requests, in the filter(ServerWebExchange exchange, WebFilterChain chain) method.

At the start of filter(), I can successfully get the method value via: exchange.getRequest().getMethodValue()

However, the same code does not work 100% of the time if it is in .doAfterSuccessOrError(). For example:

String method = exchange.getRequest().getMethodValue(); // Always works

return (exchange, chain) ->
  chain.filter(exchange)
    .doAfterSuccessOrError((aVoid, throwable) -> {
      LOG.info("getMethodValue() is sometimes null: " + exchange.getRequest().getMethodValue()); 
    });

I know there are workarounds to the immediate problem, like storing the method value in a variable while it is available, etc.

What I want to understand is the mechanism that sometimes causes null values when calling exchange.getRequest().getMethodValue()) inside doAfterSuccessOrError?

From searching around, there are suggestions that the underlying request object is not available anymore in the ServerWebExchange object because it gets recycled (GitHub Issue description). How/where does this happen? Is this happening in Jetty, or somewhere else? (I see that Jetty's HttpChannelState.java has a recycle() method, not sure if it is a factor here.)

I can never reproduce this issue locally to better understand what is happening. I've tried many different kinds of tests, from reducing the number of threads to the absolute minimum, to simulating large numbers of concurrent requests. I know there is a solution to the problem but I want to understand how the problem occurred in the first place.

  • I'm facing the same problem right now. `exchange.getRequest().getMethod()`is null and I cannot reproduce it locally. Are there any more insights on this? – Erunafailaro Sep 01 '22 at 09:40

0 Answers0