spring-cloud-gateway: When i use the getFormData in GlobalFilter produces error

When a use exchange.getFormData(), the spring cloud gateway produces java.lang.IllegalStateException: Only one connection receive subscriber allowed., anyone can help me?

Follow snippet (follow a simple example of what I’m trying to):

@Component
public class SampleFilter implements GlobalFilter, Ordered  {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return exchange.getFormData()
                .flatMap(s -> chain.filter(exchange));
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}

Stacktrace:

java.lang.IllegalStateException: Only one connection receive subscriber allowed.
	at reactor.ipc.netty.channel.FluxReceive.startReceiver(FluxReceive.java:276) ~[reactor-netty-0.7.8.RELEASE.jar:0.7.8.RELEASE]
	at reactor.ipc.netty.channel.FluxReceive.lambda$subscribe$2(FluxReceive.java:127) ~[reactor-netty-0.7.8.RELEASE.jar:0.7.8.RELEASE]
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.25.Final.jar:4.1.25.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-common-4.1.25.Final.jar:4.1.25.Final]
	at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:313) ~[netty-transport-native-epoll-4.1.25.Final-linux-x86_64.jar:4.1.25.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-common-4.1.25.Final.jar:4.1.25.Final]
	at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_181]

NOTES: spring cloud version: Finchley.RELEASE spring cloud gateway core version: 2.0.1.RELEASE

Thanks šŸ˜„

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 30 (15 by maintainers)

Most upvoted comments

You are not doing what I am suggesting. Your filter needs to do something similar to what ModifyRequestBodyGatewayFilterFactory does except with getFormData instead of bodyToMono.

I dont see this as a bug in the Gateway rather a bug in your filter. The limitation here is that the body can only be read once. Since your filter runs first and reads the body, the NettyRoutingFilter runs into this error when trying to read the body a second time to route the request. You need to do some additional work in your filter to make sure the NettyRoutingFilter can successfully read the body to route the request. See the ModifyRequestBodyFilter for an example.

Looks like this is only in snapshots based on spring framework 5.1.x

This happens in a core test as well.