Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1036

Skipped change events when using a context timeout on stream.Next in a change stream

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.0.1
    • Component/s: CRUD

      I noticed that I was missing change events when using the change stream API.  This seems to be related to my use of a context timeout on the stream.Next() call.  

      My expectation is that by using a context with timeout I can force the stream.Next() to return after a given amount of time.  I was expecting that when this occurs and stream.Next() returns false, the stream will not advance.  I detect this cancellation and then attempt to call stream.Next() again expecting to get the next result but instead it seems to jump over some results.

      The rationale for using a context timeout is that stream.Next() will not return control to the calling go routine if context.Background() is used.  I wish to regain control to check if the go routine should exit due to some external condition. 

      Attached is go program to replicate the issue.  It should count 10K events but it often comes short of this count.  At the top of the file are two variables (timeoutBug and fixWithRestart) to fix the behavior.  If timeoutBug is turned off then context.Background is used and the count is correct, but control is lost.  if fixWithRestart is turned on then instead of calling stream.Next() again on a context timeout, the stream is restarted from the last received change event.

            Assignee:
            Unassigned Unassigned
            Reporter:
            ryan.m.wynn@gmail.com Ryan Wynn
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: