Skip to content

Premature call MessagesDeliveredAsync #903

Closed

Description

@Merroy

#850

Noticed a trend that MessagesDeliveredAsync method is invoked before the complete method OnNextAsync (consumer), is this correct?

Translating as usual to the scenarios:
There is a method

private async Task OnNextAsync(T message, StreamSequenceToken streamSequenceToken)
{
      await DoSomething1(message);
      await DoSomething2(message);
}

1)In queue (stream) 1 message
2)сonsumer call OnNextAsync
3)receiver's call MessagesDeliveredAsync
4)In queue (stream) 0 messages
5)consumer call await DoSomething1(from OnNextAsync)

"the message is deleted from the queue after processing or at the time "start processing" (when the message was delivered cosumer grain"
The intended behavior is that the event is deleted from the queue -after- it is processed. All consumers OnNextAsync calls should be completed on the event, -before- it is deleted from the queue.
If this is not the behavior you are seeing, please let us know, as that would indicate a bug.

Activity

  1. Merroy commented on Oct 13, 2015

    @Merroy
    Author

    So I guess this is a bug
    I can see the situation when the event is removed before completing call OnNextAsync

  2. Merroy commented on Oct 13, 2015

    @Merroy
    Author

    the problem of block 6
    I think in the diagram you can see what the problem is
    provide the implementation of the "cyclical nature" below

    856783623461234734683654

            private async Task OnNextAsync(T message, StreamSequenceToken streamSequenceToken)
            {
                if (!message.Id.HasValue)
                {
                    message.Id = await GetId(message);
                    await _produserGrain.PushMessage(message);
                    return;
                }
                await DoSomething1(message);
                await DoSomething2(message);
            }

    PS this strange behavior does not pay attention, I'm just experimenting :)
    although if you are interested I can tell you why I decided to experiment with such cycles :)

  3. jason-bragg commented on Oct 15, 2015

    @jason-bragg
    Contributor

    I have confirmed that there is a case where MessagesDeliveredAsync is called prematurely.
    I've a test of the SimpleQueueCache that demonstrates the bug.
    Looking into a fix.

  4. gabikliot commented on Oct 15, 2015

    @gabikliot
    Contributor

    Jason, can you explain how this happens?

  5. jason-bragg commented on Oct 15, 2015

    @jason-bragg
    Contributor

    @gabikliot Sure. I can try :)
    The IQueueCacheCursor was initially modeled off of the IEnumerator interface, with a similar usage pattern. In the IEnumerator usage pattern, one gets an enumerator then loops while MoveNext returns true, getting the current value each loop. In this pattern, MoveNext must be called before the Current value is accessed.

    One common implementation of IEnumerator is for the initial enumerator to point to data before the intended first value. For instance consider list 'a,b,c,d,e'. If you wanted an enumerator that enumerated from ‘c’ forward, the initial enumerator's Current would point at ‘b’, so the first MoveNext would return true and move the Current to c, the value you actually wanted first. This approach requires some special case logic for the case where one wants to enumerate from the start of the sequence, since there is no value ‘before’ the first to point at.

    Another approach is for the enumerator to keep a reference into the list separate from Current, and use Current simply as a storage property. Using the above example, the initial enumerator would have an initial reference into the sequence pointing at ‘c’, and have no Current value. When the MoveNext was called, it would check to see if the reference pointed to a valid list member, if it did (which it does, ‘c’) it would assign that value to Current, and move the reference to the next item in the sequence (‘d’). This pattern requires no special handling for enumerators starting at the first item in the sequence, but depending on the type of sequence, may require special handling for the last item in the sequence.

    Since the queue cache cursor requires special handling of cursors that have reached the end of the sequence of cached items, but did not require special logic for cursors pointing at the start of the cache, the second of the two above approaches fit better, so it was used.

    When the cache was updated to prevent items from being purged if they were still in use, we used the cursor’s reference into the cached items sequence to determine if an item was still in use, and hence could not be purged. The problem with this is that the cursor enumerator algorithm I selected moves the cursor’s queue reference forward to the next item on the move next, so the reference is always one ahead of the Current item. The Current item is the one being delivered, but it has not yet been delivered. This means it is available for purge before it has been delivered to the consumer.

    This issue was not noticed until now because the cache purges items in blocks, so an individual item being available for purge will not in itself usually trigger a purge.

  6. gabikliot commented on Oct 16, 2015

    @gabikliot
    Contributor

    I see. Thanks for explanation.
    The way I see it: Indeed, enumerator can be implemented differently. But we discussed not once in the past that a better approach is to always point to current and move next does what it says: Updates current to next. I think this is not the first bug caused by this discrepancy.
    I would consider changing the cache, and having current, has next, and move next always point to the current.

  7. jason-bragg commented on Oct 20, 2015

    @jason-bragg
    Contributor

    Unfortunately, I've blown through the time I allotted towards looking into this and have yet to devise a clean solution. Hopefully something will come to me, but higher priority issues need my attention. I'll revisit this as time allows. Community help would be appreciated.

  8. lgnashold commented on Aug 16, 2019

    @lgnashold

    @jason-bragg
    I believe I'm encountering this issue.

    I'm using Orleans 2.4.0, and subscribing a stateless grain implicitly to an SQS stream. MessagesDeliveredAsync is called before the subscriber function returns (in the same way Merroy described in their original post). This causes the message to be deleted from the queue before I can guarantee it has been processed correctly.

    Would anyone be able to tell me what this issue's current status is, and why it was marked as closed?

    Thank you!

  9. jason-bragg commented on Aug 17, 2019

    @jason-bragg
    Contributor

    I believe this is still an issue with any stream provider using the Simple Queue Cache (which SQS does). It was closed as it's a fairly narrow edge case, which was never prioritized. It only occurs if all of the streams on a partition become idle (that is, none have more data to deliver).

    Having said that, it is a bug that should be addressed as it can show up regularly in low throughput streaming applications.

  10. added this to the Backlog milestone on Aug 29, 2019
  11. 6 remaining items

  12. self-assigned this
    on Mar 3, 2022
  13. added
    area-streaming
    on Mar 3, 2022
  14. pawanrao commented on Mar 14, 2022

    @pawanrao

    Hi @benjaminpetit Any update on a fix here?

  15. benjaminpetit commented on Mar 17, 2022

    @benjaminpetit
    Contributor

    Not really. I have an ugly workaround, but it's pretty invasive and will definitely impact performance. Hopefully I should be able to do something for 4.0, but backporting to 3.x would be painful

  16. pawanrao commented on Jun 7, 2022

    @pawanrao

    Hi @benjaminpetit Is this something that is still being considered for 4.0?
    we are still seeing message loss when using persistent queues

  17. benjaminpetit commented on Jun 8, 2022

    @benjaminpetit
    Contributor

    It's still in the backlog, not a priority right now for 4.0.

    There is multiple issues regarding using of Azure Queue and SQS with streaming. If you can open a PR with a better implementation of the SimpleQueueCache I would be happy to review it

  18. pawanrao commented on Dec 14, 2022

    @pawanrao

    @benjaminpetit is there any way this can be prioritized?
    We are still seeing multiple instances where we are losing stream messages.
    In this case, Orleans is taking something which is very durable(i.e message queues) and making it far worse.

    I'm not very well versed with SimpleQueueCache or Orleans to contribute effectively here

  19. pawanrao commented on Feb 7, 2025

    @pawanrao

    @ReubenBond just wanted to bring this on your radar
    we're continuing to see this issue with SimpleQueueCache and lose messages particularly when silos are shutting down

    any chance this can be fixed?

  20. ReubenBond commented on Aug 18, 2026

    @ReubenBond
    Member

    Current main still reproduces this when BatchContainerBatchSize > 1: the pulling agent advances each SimpleQueueCache cursor across all containers before awaiting the combined consumer delivery. That can leave the oldest bucket with no cursor while one or more consumers are still processing, so a concurrent queue read purges the bucket and calls MessagesDeliveredAsync prematurely.

    PR #10655 fixes the invariant by pinning the earliest cache bucket for the lifetime of each combined delivery. Every consumer holds its own pin, so acknowledgement becomes possible only after the final in-flight consumer completes. A failed combined delivery marks every contained cache item for redelivery.

    The focused regression failed on unmodified main because MessagesDeliveredAsync was observed while two consumers were blocked. It passes with the fix on both net8.0 and net10.0; all 19 PersistentStreamPullingAgentTests pass on each framework, and dotnet build Orleans.slnx -bl completes with zero errors.

  21. locked and limited conversation to collaborators on Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Status: In PRarea-streamingbugcost: S

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions