You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
reactor.core.Exceptions$OverflowException: The receiver is overrun by more signals than expected (bounded queue...) #959
Using RSocket REQUEST_STREAM to implement a query where results are streamed back to the client. The client sends REQUEST_N frame 512. It is successfully receiving results. Once the client successfully receives >256 results, it requests an additional 256. The communication works fine. FrameLogger shows expected receiving and sending frames. After some time the following exception is thrown on the event of additional REQUEST_N on client;
@OlegDokuka why is in handleRequestStream/handleChannel the assumption that the receiver queue has bounded size 1? I would understand it in handleRequestResponse, but not in Flux variants.
final UnicastProcessor<Payload> receiver = UnicastProcessor.create(Queues.<Payload>one().get());
Handle frame function can consume network frames as fast as it can so it can happen that processor did not yet dispatched previous frame downstream. When I used unbounded queue it started working without exception.
The .hide operator should disable fusion and no issue should appear after that.
@OlegDokuka why is in handleRequestStream/handleChannel the assumption that the receiver queue has bounded size 1? I would understand it in handleRequestResponse, but not in Flux variants.
final UnicastProcessor receiver = UnicastProcessor.create(Queues.one().get());
Handle frame function can consume network frames as fast as it can so it can happen that processor did not yet dispatched previous frame downstream. When I used unbounded queue it started working without exception.
there is a strict reactive streams relationship. If you requested N elements, you clearly stated that you are ready to receive N elements. Following reactive streams, the producer follows your demand and produce exactly N elements (or less).
That said, there is no reason for us (rsocket core logic) to cache N elements anywhere before your events handler. That is why to avoid any redundant allocations we use a queue of a single element since we expect no values will be enqueued at all
The mentioned issue is the result of Reactor optimizations that we have not taken into account. Which fuse with the unicastProcessor queue and does not take into account the queue capacity
@OlegDokuka I am not sure I described that properly.
Ofcourse I know that N requested elements are ready to be consumed. But the problem is that there is also asynchronous effect. So downstream can consume, but it can take a time. Our application uses usual backpressure (512 elements) and normal operators.
As I said issue is fixed when queue is unbounded, because in rare case it hits the condition where in queue there is one element still not dispatched (randomly after 10k - 1M items received). But it also sometimes happen with much shorter streams.
I tries to remove fusion everywhere. I will try also your suggestion, but again:
why is in handleRequestStream/handleChannel the assumption that the receiver queue has bounded size 1? When receiving side is processed in other threads than consuming side.
here is the point. Your backpressure operator is responsible for storing up to 512 elements, RSocket has no clue about your request. it just transparently transforms your request N into a binary frame and ensures that that frame is decoded back upon receiveal into the same requestN method invocation on your subscription on the receiver side.
The problem is that Reactor is a smart framework and tries to do lots of optimizations.
That said, if you have a workflow of UnicastProcessor[Queue] -> PublishOn[Queue]
then Reactor will optimize your workflow and make it as the following PublishOn[UnicastProcessor[Queue]], which means that 1 less queue will be allocated.
But here comes the problem. We use UnicastProcessor just because it is a convenient API that lets us send elements manually. On the other hand, we don't need to store any elements, because we know the strict relationship between publisher and subscriber, which says that if subscriber say request(5) it is ready to consume(5) if later on, it says request(10), but previous 5 was not yet delivered, it means it is ready to receive 15 (5 + 10) (and again we don't have to store anything).
The problems come from the fact that UnicastProcessor fuses with any operator which creates its queue. This means now if we create queue(1) and publishOn wanted queue(512), the UnicastProcessor will have no clue about that requirement. hence we see overflow exceptions because of requirements mismatch.
As I said, to avoid this issue for now, just place the .hide operator before your publishOn or any other operator like groupBy. which can fuse with our internal UnicastProcessor.
Also, feel free to migrate to 1.1.0 which provides custom implementations of request operators and does not use UnicastProcessor anymore
On the other hand, we have subscribeOn between UnicastProcessor and your logic. SubscribeOn should disable fusion. Thus now I'm at the point of I don't have a clue what is going on there.
@lkolisko appologies for the confusion. Let me reproduce that issue locally and then I can provide you with more updates. For now please, use either 1.0.2 or 1.1.0
I am working with Lukas on this issue. We use spring-boot unfortunately and I am not sure we can upgrade to 1.1.0 and it brings some incompatibilities on onterfaces. On the other hand we tried 1.0.2 and it did not help. We placed ".hide" almost everywhere without any effect as we cannot go under "subscribedOn" in handleRequestStream. The only fix that helped is to use unbounded queue in handleRequestStream to cover that glitch. In worst case there will be 2 elements in memory, but still better to not crash the stream.
You are right, we have just verified that. The issue was that we got same exception text (overrun by more signals than expected) from server side as well thus were confused. So it looked the same, but was not. It means similar problem is also on the other side (RSocketResponder) . When we changed server and client issue was solved.
@koldat one more question. Do you have any custom publishers on the producer side?
I did a couple of different test cases, and I can not reproduce this issue. Also, we have an extensive number of tests inside RSocket which means if we have a bug, it would eventually appear on the CI but it's not. Thus, I suspect that something is on your end. Thus to make sure it is our bug, I would appreciate if you share more details on what kind of operators do you use: what is the producer, what is the consumer; what Reactor operators do you use (if not reactor, what libraries do you use in combination with rsocket)
@koldat Also, I would appreciate it if you can enable rsocket frame logging and your flux logging and share these logs with me. That can helo with debugging as well
Issue is that there are two threads. One is requesting and one is dispatching to queue. As request from downstream is higher than one then there is more than one inflight message coming to receiver.
Now imagine you are dispatching to queue(io.rsocket.core.RSocketRequester.handleFrame(int, FrameType, ByteBuf)), but at the same time there is a "request" call from downstream side. It locks WIP so that onNext will NOT drain. Then it dispatch immediately second message from wire which as a result end up with two elements in the queue -> overflow.
Do you want me to do PR for this or you will handle that? I would simply revert to default create (unbounded). It does not matter much if for tiny a bit of a time there will be couple of elements.
Let's keep what we have right and do that differently. Having an unbounded queue behind UnboundedProcessor brings other DoS-related problems (for more info see -> #887). Thus, I don't wanna circle back to that problem in 1.0.x again and would rather relax UnboundedProcessor (taking into account our execution model which should never fail because of the overflow) and have that relaxed impl locally. We are planning the release of 1.0.4 next week and this one should be included in it.
However, if you see what I mean by "relaxed impl" which has to be Queue-free and WIP-free, feel free to send a PR before me. (I'm planning to work on that this weekends)
#887 makes sense. What I would personally do is to add requestN assertion which is to check if queue is drained and filled with respect to downstream requests. If client does not follow the rules it will throw an exception. It can be maybe even nice operator in reactor. The point is that even if you fix this place the same type of attack can be applied to downstream so that every downstream service would maybe need to have same protection. So having that on entry would be the best I think.
I am not sure what is meant by relaxed impl, but if it is easier then I would leave it up to you as an expert in this area.
Right. The more challenging part is to have Processor API which does that + Single Subscriber Assertion + RequestN assertion though it is apart of rsocket impl. The point here is to avoid any queue on our end. Thus the first trial was implementing UnicastProcessor with Queues.one() but it seems that we have to come back to a custom UnicastProcessor impl again...
Although... there is a simpler way. What if we request(unbounded) to UnicastProcessor upfront at the first request at the RequestOperator? So what we shell do is:
Having that, we can keep UnicastProcessor.create(Queue.one()) and the requestN assertion should be deferred to the downstream logic (e.g. publishOn operator, or after all the actual subscribe must ensure the produced number of elements is what was requested) (and in case of attack we will not be queueing anything on our end)
@koldat, Do you wish to make a PR for that and adding a test ensuring such racing is not reproducing anymore?
here is the point. Your backpressure operator is responsible for storing up to 512 elements, RSocket has no clue about your request. it just transparently transforms your request N into a binary frame and ensures that that frame is decoded back upon receiveal into the same requestN method invocation on your subscription on the receiver side.
The problem is that Reactor is a smart framework and tries to do lots of optimizations. That said, if you have a workflow of UnicastProcessor[Queue] -> PublishOn[Queue]
then Reactor will optimize your workflow and make it as the following PublishOn[UnicastProcessor[Queue]], which means that 1 less queue will be allocated.
But here comes the problem. We use UnicastProcessor just because it is a convenient API that lets us send elements manually. On the other hand, we don't need to store any elements, because we know the strict relationship between publisher and subscriber, which says that if subscriber say request(5) it is ready to consume(5) if later on, it says request(10), but previous 5 was not yet delivered, it means it is ready to receive 15 (5 + 10) (and again we don't have to store anything).
The problems come from the fact that UnicastProcessor fuses with any operator which creates its queue. This means now if we create queue(1) and publishOn wanted queue(512), the UnicastProcessor will have no clue about that requirement. hence we see overflow exceptions because of requirements mismatch.
As I said, to avoid this issue for now, just place the .hide operator before your publishOn or any other operator like groupBy. which can fuse with our internal UnicastProcessor.
Also, feel free to migrate to 1.1.0 which provides custom implementations of request operators and does not use UnicastProcessor anymore
Activity
OlegDokuka commentedon Nov 18, 2020
Looks like it is possible with fusion enabled. I will try to arrange something similar with the
.publisheOnoperator to see if this reproduceskoldat commentedon Nov 19, 2020
It is happening for us on more places. So far did not found any workaround. How to disable fusion?
koldat commentedon Nov 19, 2020
@OlegDokuka why is in handleRequestStream/handleChannel the assumption that the receiver queue has bounded size 1? I would understand it in handleRequestResponse, but not in Flux variants.
io.rsocket.core.RSocketRequester.handleRequestStream(Payload)
Handle frame function can consume network frames as fast as it can so it can happen that processor did not yet dispatched previous frame downstream. When I used unbounded queue it started working without exception.
OlegDokuka commentedon Nov 19, 2020
@koldat
There is no way to do so on your end.
For now, to work around what you have, you can try to add a
RequesterInterceptorlike the following :please find a full example here -> https://github.com/rsocket/rsocket-java/blob/master/rsocket-examples/src/main/java/io/rsocket/examples/transport/tcp/plugins/LimitRateInterceptorExample.java
The
.hideoperator should disable fusion and no issue should appear after that.there is a strict reactive streams relationship. If you requested N elements, you clearly stated that you are ready to receive N elements. Following reactive streams, the producer follows your demand and produce exactly N elements (or less).
That said, there is no reason for us (rsocket core logic) to cache N elements anywhere before your events handler. That is why to avoid any redundant allocations we use a queue of a single element since we expect no values will be enqueued at all
The mentioned issue is the result of Reactor optimizations that we have not taken into account. Which fuse with the unicastProcessor queue and does not take into account the queue capacity
koldat commentedon Nov 19, 2020
@OlegDokuka I am not sure I described that properly.
Ofcourse I know that N requested elements are ready to be consumed. But the problem is that there is also asynchronous effect. So downstream can consume, but it can take a time. Our application uses usual backpressure (512 elements) and normal operators.
As I said issue is fixed when queue is unbounded, because in rare case it hits the condition where in queue there is one element still not dispatched (randomly after 10k - 1M items received). But it also sometimes happen with much shorter streams.
I tries to remove fusion everywhere. I will try also your suggestion, but again:
why is in handleRequestStream/handleChannel the assumption that the receiver queue has bounded size 1? When receiving side is processed in other threads than consuming side.
OlegDokuka commentedon Nov 19, 2020
here is the point. Your
backpressureoperator is responsible for storing up to 512 elements, RSocket has no clue about your request. it just transparently transforms your request N into a binary frame and ensures that that frame is decoded back upon receiveal into the same requestN method invocation on your subscription on the receiver side.The problem is that Reactor is a smart framework and tries to do lots of optimizations.
That said, if you have a workflow of UnicastProcessor[Queue] -> PublishOn[Queue]
then Reactor will optimize your workflow and make it as the following PublishOn[UnicastProcessor[Queue]], which means that 1 less queue will be allocated.
But here comes the problem. We use UnicastProcessor just because it is a convenient API that lets us send elements manually. On the other hand, we don't need to store any elements, because we know the strict relationship between publisher and subscriber, which says that if subscriber say request(5) it is ready to consume(5) if later on, it says request(10), but previous 5 was not yet delivered, it means it is ready to receive 15 (5 + 10) (and again we don't have to store anything).
The problems come from the fact that UnicastProcessor fuses with any operator which creates its queue. This means now if we create queue(1) and publishOn wanted queue(512), the UnicastProcessor will have no clue about that requirement. hence we see overflow exceptions because of requirements mismatch.
As I said, to avoid this issue for now, just place the
.hideoperator before yourpublishOnor any other operator likegroupBy. which can fuse with our internalUnicastProcessor.Also, feel free to migrate to 1.1.0 which provides custom implementations of request operators and does not use UnicastProcessor anymore
OlegDokuka commentedon Nov 19, 2020
On the other hand, we have
subscribeOnbetween UnicastProcessor and your logic. SubscribeOn should disable fusion. Thus now I'm at the point ofI don't have a clue what is going on there.@lkolisko appologies for the confusion. Let me reproduce that issue locally and then I can provide you with more updates. For now please, use either 1.0.2 or 1.1.0
koldat commentedon Nov 19, 2020
I am working with Lukas on this issue. We use spring-boot unfortunately and I am not sure we can upgrade to 1.1.0 and it brings some incompatibilities on onterfaces. On the other hand we tried 1.0.2 and it did not help. We placed ".hide" almost everywhere without any effect as we cannot go under "subscribedOn" in handleRequestStream. The only fix that helped is to use unbounded queue in handleRequestStream to cover that glitch. In worst case there will be 2 elements in memory, but still better to not crash the stream.
koldat commentedon Nov 19, 2020
The change (unbounded vs. one()) was introduced in 18050ed after 1.0.2. Hmm... We will try again if it is really happening even for 1.0.2.
OlegDokuka commentedon Nov 19, 2020
@koldat @lkolisko It should not happening in 1.0.2. We will do our best to land a fix for that ASAP and do 1.0.4
koldat commentedon Nov 19, 2020
You are right, we have just verified that. The issue was that we got same exception text (overrun by more signals than expected) from server side as well thus were confused. So it looked the same, but was not. It means similar problem is also on the other side (RSocketResponder) . When we changed server and client issue was solved.
Thanks a lot for looking into this.
OlegDokuka commentedon Nov 20, 2020
@koldat one more question. Do you have any custom publishers on the producer side?
I did a couple of different test cases, and I can not reproduce this issue. Also, we have an extensive number of tests inside RSocket which means if we have a bug, it would eventually appear on the CI but it's not. Thus, I suspect that something is on your end. Thus to make sure it is our bug, I would appreciate if you share more details on what kind of operators do you use: what is the producer, what is the consumer; what Reactor operators do you use (if not reactor, what libraries do you use in combination with rsocket)
koldat commentedon Nov 20, 2020
We use Spring boot 2.3.5. Flux we return is flatmap:
where inner flux is using and fromIterable:
9 remaining items
OlegDokuka commentedon Jan 25, 2021
can be a bug in the reactor. I will check that
OlegDokuka commentedon Jan 25, 2021
@koldat Also, I would appreciate it if you can enable rsocket frame logging and your flux logging and share these logs with me. That can helo with debugging as well
OlegDokuka commentedon Feb 17, 2021
@koldat @lkolisko do you observe that issue in the combination of 1.0.3 and the latest reactor-core 3.3.14 and the fix that you just recently added?
koldat commentedon Feb 18, 2021
@OlegDokuka no latest reactor did not help, it is another issue. Problem is still in this queue:
Issue is that there are two threads. One is requesting and one is dispatching to queue. As request from downstream is higher than one then there is more than one inflight message coming to receiver.
Now imagine you are dispatching to queue(io.rsocket.core.RSocketRequester.handleFrame(int, FrameType, ByteBuf)), but at the same time there is a "request" call from downstream side. It locks WIP so that onNext will NOT drain. Then it dispatch immediately second message from wire which as a result end up with two elements in the queue -> overflow.
OlegDokuka commentedon Feb 18, 2021
Indeed. I see what you mean. Good catchup 👍
koldat commentedon Feb 18, 2021
Do you want me to do PR for this or you will handle that? I would simply revert to default create (unbounded). It does not matter much if for tiny a bit of a time there will be couple of elements.
OlegDokuka commentedon Feb 19, 2021
@koldat thank you for offering!
Let's keep what we have right and do that differently. Having an unbounded queue behind UnboundedProcessor brings other DoS-related problems (for more info see -> #887). Thus, I don't wanna circle back to that problem in 1.0.x again and would rather relax
UnboundedProcessor(taking into account our execution model which should never fail because of the overflow) and have that relaxed impl locally. We are planning the release of 1.0.4 next week and this one should be included in it.However, if you see what I mean by "relaxed impl" which has to be Queue-free and WIP-free, feel free to send a PR before me. (I'm planning to work on that this weekends)
Thanks,
Oleh
koldat commentedon Feb 19, 2021
#887 makes sense. What I would personally do is to add requestN assertion which is to check if queue is drained and filled with respect to downstream requests. If client does not follow the rules it will throw an exception. It can be maybe even nice operator in reactor. The point is that even if you fix this place the same type of attack can be applied to downstream so that every downstream service would maybe need to have same protection. So having that on entry would be the best I think.
I am not sure what is meant by relaxed impl, but if it is easier then I would leave it up to you as an expert in this area.
OlegDokuka commentedon Feb 19, 2021
Right. The more challenging part is to have Processor API which does that + Single Subscriber Assertion + RequestN assertion though it is apart of rsocket impl. The point here is to avoid any queue on our end. Thus the first trial was implementing UnicastProcessor with Queues.one() but it seems that we have to come back to a custom UnicastProcessor impl again...
Although... there is a simpler way. What if we
request(unbounded)toUnicastProcessorupfront at the first request at theRequestOperator? So what we shell do is:this.s.request(n)from here -> https://github.com/rsocket/rsocket-java/blob/1.0.x/rsocket-core/src/main/java/io/rsocket/core/RequestOperator.java#L96this.s.request(Long.MAX_VALUE)there -> https://github.com/rsocket/rsocket-java/blob/1.0.x/rsocket-core/src/main/java/io/rsocket/core/RequestOperator.java#L118 to ensure thatUnicastProcessorhas requested once and will never be touched by the downstream requestHaving that, we can keep
UnicastProcessor.create(Queue.one())and the requestN assertion should be deferred to the downstream logic (e.g.publishOnoperator, or after all the actual subscribe must ensure the produced number of elements is what was requested) (and in case of attack we will not be queueing anything on our end)@koldat, Do you wish to make a PR for that and adding a test ensuring such racing is not reproducing anymore?
OlegDokuka commentedon Feb 26, 2021
@koldat this issue should be fixed in 1.0.4 release by #985. Let me know if it works for you!
chaoyoung commentedon Sep 13, 2022
There are many such issue in our production environment.
dependency: spring-boot:2.3.12.RELEASE, rsocket:1.0.5, reactor-core:3.3.17
error:chaoyoung commentedon Sep 13, 2022
It does not work for me, rsocket:1.0.5
dependency and context: #959 (comment)