Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SendUtils onDiscard handler class cast exception #1077

Closed
Svenskunganka opened this issue Nov 1, 2022 · 5 comments · Fixed by #1091
Closed

SendUtils onDiscard handler class cast exception #1077

Svenskunganka opened this issue Nov 1, 2022 · 5 comments · Fixed by #1091
Labels
enhancement superseded Issue is superseded by another

Comments

@Svenskunganka
Copy link

Svenskunganka commented Nov 1, 2022

I'm using RSocket Java with Spring Webflux and RSocket JS in the front-end, using REQUEST_STREAM with an infinite Kafka source. The returned Flux receives quite a lot of small messages and does quite a bit of filtering and buffering. The relevant code in SendUtils does an unchecked cast of each discarded object to a ReferenceCounted which seems to throw a class cast exception that is caught and ignored.
The problem is that due to the sheer amount of objects being discarded (due to filtering and discarded buffers on disconnect), this uses up a substantial amount of CPU resources looking at the profiler.

profiler

The returned Fluxes are scheduled on Schedulers.boundedElastic(), and for some reason entire threads are started up just for this exception:
flamegraph

Expected Behavior

Not use up so much CPU resources

Actual Behavior

Uses a lot of CPU resources

Steps to Reproduce

Possible Solution

Not exactly sure, but perhaps an instanceof check:

if (data instanceof ReferenceCounted) ((ReferenceCounted) data).release();

Your Environment

  • RSocket version(s) used: rsocket-core:1.1.3, rsocket-transport-netty:1.1.3
  • Other relevant libraries versions (eg. netty, ...): Spring Webflux 5.3.23
  • Platform (eg. JVM version (javar -version) or Node version (node --version)): 17
  • OS and version (eg uname -a): Happens on all tested platforms (Debian, Mac)
@OlegDokuka
Copy link
Member

@Svenskunganka! Sounds relevant!

Feel free to contribute if you see the direct solution, otherwise, I will investigate it!

Best,
Oleh

@Svenskunganka
Copy link
Author

Hi Oleh!

If my possible solution is acceptable I can definitely contribute it! But if you perhaps know of another way this could be solved without needing to perform an instanceof check for every discarded object that might be a better route to explore.
From my limited experience the only thing I can think of is using e.g Mono.using()/Flux.using() when creating a Payload.

input.flatMap(bytes -> Mono.using(
    () -> ByteBufPayload.create(bytes),
    data -> ...,
    Payload::release
));

Kind regards,
Tom

luczus pushed a commit to luczus/rsocket-java that referenced this issue Dec 1, 2022
@luczus
Copy link

luczus commented Dec 1, 2022

Hi Oleh, @Svenskunganka!

I've created PR #1079 with a fix.
Please let me know what you think about these changes.

Best
Lukasz

@tukez
Copy link

tukez commented Dec 13, 2022

I noticed the same problem and in my case at least the offending instance is of type org.springframework.core.io.buffer.NettyDataBuffer which holds a ByteBuf. Should the inner ByteBuf be released? That is not possible (other than reflection) in rsocket-java though?

@kmccarp
Copy link

kmccarp commented Apr 10, 2023

@OlegDokuka are there any plans to integrate the changes made by @luczus? This is heavily polluting our logs.

@OlegDokuka OlegDokuka added the superseded Issue is superseded by another label Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement superseded Issue is superseded by another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants