Skip to content

Conversation

hadadil
Copy link

@hadadil hadadil commented Mar 13, 2025

Motivation:

I had a change recently in 4.5.13 (not a lucky number)
After upgrading to this version we have noticed after resume we do receive more than one message at one moment.
First I extended the test to reproduce the case... and after that I done the prod code change.

Conformance:

You should have signed the Eclipse Contributor Agreement as explained in https://github.com/eclipse/vert.x/blob/master/CONTRIBUTING.md
Please also make sure you adhere to the code style guidelines: https://github.com/vert-x3/wiki/wiki/Vert.x-code-style-guidelines

@hadadil hadadil marked this pull request as draft March 14, 2025 07:35
@hadadil hadadil force-pushed the do-not-receive-msgs-after-resume branch 2 times, most recently from 9d8836e to bdf86b3 Compare March 14, 2025 07:54
@@ -171,7 +158,7 @@ protected void dispatch(Message<T> msg, ContextInternal context, Handler<Message
if (handler == null) {
throw new NullPointerException();
}
context.emit(msg, handler);
context.dispatch(msg, handler);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was reverted as it had a side effect:
after resume we were received many messages even we paused again during the first message processing.

@@ -183,8 +170,8 @@ private void deliver(Handler<Message<T>> theHandler, Message<T> message) {

private synchronized void checkNextTick() {
// Check if there are more pending messages in the queue that can be processed next time around
if (!pending.isEmpty() && demand > 0L) {
context.nettyEventLoop().execute(() -> {
if (demand > 0L && !pending.isEmpty()) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I flip here so the demand evaulated first

if (!pending.isEmpty() && demand > 0L) {
context.nettyEventLoop().execute(() -> {
if (demand > 0L && !pending.isEmpty()) {
context.executor().execute(() -> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we should schedult the task for the next tick on the context thread (so we have no 2 thread dispatching a message at the same moment).
In the past we had a scenario like:

  • received
  • pause
  • resume
  • received + paused
  • received ?!
    so after we resume we had sometimes got two messages even in the 1st on we said already we don't want to have more right now.

}
if (pending.size() < maxBufferedMessages) {
pending.add(message);
checkNextTick();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method simplified a lot and here we always just put it in hte pending (if the buffer not full yet of course) and send a tick (that will decied if we need to do anything right now or not)
as the tick evaulation happen on the contex thread we are safe (single threaded)

@hadadil hadadil marked this pull request as ready for review March 14, 2025 08:01
@hadadil hadadil force-pushed the do-not-receive-msgs-after-resume branch 2 times, most recently from 98dc54c to a2860aa Compare March 14, 2025 09:06
@hadadil hadadil force-pushed the do-not-receive-msgs-after-resume branch from a2860aa to e93ab84 Compare March 14, 2025 09:55
@@ -1401,7 +1401,6 @@ public void testUnregisterConsumerDiscardPendingMessages() {
eb.send(ADDRESS1, "val1");
Context ctx = Vertx.currentContext();
ctx.runOnContext(v -> {
consumer.resume();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was in the past executed on eventloop thread on the context... now this is executed on the context thread (in case we area lready on it it will not schedule new job, in case we are on another thread it will schedule an async job.
in short:
now this is executed here so the logic below was not anymore fine as this call wipe out sync the pending msgs

if (!pending.isEmpty() && demand > 0L) {
context.nettyEventLoop().execute(() -> {
if (demand > 0L && !pending.isEmpty()) {
context.execute(__ -> {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we do execute the resume/fetch from the context thread so we do not have situations when we call resume and the same time we do receive a msg and we get 2msgs (on on the context thread and one on eventloop thread)

}
theHandler = handler;
return false;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was also not 100% right, when we do discard msg we should return with false to have that in the metrics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant