← 输入框|github.com|刷新
Skip to content

Spring Boot RSocket high CPU usage after client disconnects #992

Closed

Description

@mister11

Origin of this issue is StackOverflow post where I showed some code, but I'll add more details here.

Expected Behavior

After a client is shut down, CPU usage should not increase especially not to >50%.

Actual Behavior

When using the latest version of Spring Boot and its RSocket support (which pulls rsocket-java version 1.1.0), after a client requests a stream of data and it disconnects (manual termination of the client application or Ctrl+C of the rsc CLI), CPU usage gets high (~20%). If more clients are connected and disconnected, CPU usage grows even higher (theoretically it could reach 100% with enough clients).

Steps to Reproduce

I'm attaching Spring Boot application. It's an unfiltered version, meaning I haven't change anything from the point in time when CPU usage was behaving as described. sample-service.zip

Once this service is running, I opened few terminals and run rsc tcp://localhost:8888 --stream --route test in each of them. After ~10 seconds I'd start closing it one by one. As I'm closing them, I can see CPU usage in htop increasing and finally stopping at around 70-80%. Only after service is shut down, CPU usage drops to an idle state. Maybe it's not important, but I had to click that red skull icon in IntelliJ since the application did not properly shut down only with the red square.

Just for the case of completeness, I'm attaching rsocket-kotlin sample-client.zip that can be used to reproduce the same behavior. For this, it's enough to just start/stop the client couple of times.

Your Environment

Arch Linux with Java 13 (uname -a: Linux tara 5.4.39-1-MANJARO #1 SMP PREEMPT Wed May 6 10:36:44 UTC 2020 x86_64 GNU/Linux)
Spring Boot 2.4.3, Kotlin + JVM 11 as a target
RSocket Java 1.1.0

Activity

  1. OlegDokuka commented on Mar 4, 2021

    @OlegDokuka
    Member

    I did the following threadump, and what I see there is ->

    "reactor-http-nio-3@6154" daemon prio=5 tid=0x1b nid=NA runnable
      java.lang.Thread.State: RUNNABLE
    	  at reactor.core.publisher.Operators.onDiscardQueueWithClear(Operators.java:508)
    	  at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.cancel(FluxConcatMap.java:339)
    	  at reactor.core.publisher.FluxPeek$PeekSubscriber.cancel(FluxPeek.java:152)
    	  at reactor.core.publisher.FluxMap$MapSubscriber.cancel(FluxMap.java:167)
    	  at reactor.core.publisher.FluxMap$MapSubscriber.cancel(FluxMap.java:167)
    	  at org.springframework.messaging.handler.invocation.reactive.ChannelSendOperator$WriteBarrier.cancel(ChannelSendOperator.java:335)
    	  at reactor.core.publisher.FluxMap$MapSubscriber.cancel(FluxMap.java:167)
    	  at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.drainLoop(Operators.java:2251)
    	  at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.drain(Operators.java:2219)
    	  at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.cancel(Operators.java:2031)
    	  at io.rsocket.core.RequestStreamResponderSubscriber.handleCancel(RequestStreamResponderSubscriber.java:300)
    	  at io.rsocket.core.RSocketResponder$$Lambda$707/0x0000000800fb8040.accept(Unknown Source:-1)
    	  at java.lang.Iterable.forEach(Iterable.java:75)
    	  at io.rsocket.core.RSocketResponder.cleanUpSendingSubscriptions(RSocketResponder.java:186)
    	  - locked <merged>(a io.rsocket.core.RSocketResponder)
    	  at io.rsocket.core.RSocketResponder.doOnDispose(RSocketResponder.java:174)
    	  at io.rsocket.core.RSocketResponder.tryTerminate(RSocketResponder.java:108)
    	  at io.rsocket.core.RSocketResponder.tryTerminateOnConnectionClose(RSocketResponder.java:101)
    	  at io.rsocket.core.RSocketResponder$$Lambda$685/0x0000000800f9fdd0.run(Unknown Source:-1)
    	  at reactor.core.publisher.LambdaMonoSubscriber.onComplete(LambdaMonoSubscriber.java:135)
    	  at reactor.core.publisher.NextProcessor$NextInner.onComplete(NextProcessor.java:403)
    	  at reactor.core.publisher.NextProcessor.tryEmitValue(NextProcessor.java:185)
    	  at reactor.core.publisher.NextProcessor.tryEmitEmpty(NextProcessor.java:137)
    	  at reactor.core.publisher.NextProcessor.onComplete(NextProcessor.java:132)
    	  at io.rsocket.internal.BaseDuplexConnection.dispose(BaseDuplexConnection.java:35)
    	  at io.rsocket.transport.netty.TcpDuplexConnection.lambda$new$0(TcpDuplexConnection.java:49)
    	  at io.rsocket.transport.netty.TcpDuplexConnection$$Lambda$646/0x0000000800f7c370.operationComplete(Unknown Source:-1)
    	  at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)
    	  at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571)
    	  at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550)
    	  at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)
    	  at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)
    	  at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605)
    	  at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)
    	  at io.netty.channel.DefaultChannelPromise.trySuccess(DefaultChannelPromise.java:84)
    	  at io.netty.channel.AbstractChannel$CloseFuture.setClosed(AbstractChannel.java:1174)
    	  at io.netty.channel.AbstractChannel$AbstractUnsafe.doClose0(AbstractChannel.java:761)
    	  at io.netty.channel.AbstractChannel$AbstractUnsafe.close(AbstractChannel.java:737)
    	  at io.netty.channel.AbstractChannel$AbstractUnsafe.close(AbstractChannel.java:608)
    	  at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.closeOnRead(AbstractNioByteChannel.java:105)
    	  at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:174)
    	  at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
    	  at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
    	  at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
    	  at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    	  at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    	  at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    	  at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    	  at java.lang.Thread.run(Thread.java:832)
    

    From the stack trace, we can see that the concatMap tries to discard an infinite sequence from Kotlin which ends up in infinity looping. Thus, it is not really an RSocket bug but rather a reactor one.

    /cc @simonbasle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions