@@ -424,6 +424,12 @@ def _message_generator(self):
424424 elif fetch_offset == position :
425425 log .log (0 , "Returning fetched records at offset %d for assigned"
426426 " partition %s" , position , tp )
427+
428+ # We can ignore any prior signal to drop pending message sets
429+ # because we are starting from a fresh one where fetch_offset == position
430+ # i.e., the user seek()'d to this position
431+ self ._subscriptions .assignment [tp ].drop_pending_message_set = False
432+
427433 for msg in self ._unpack_message_set (tp , messages ):
428434
429435 # Because we are in a generator, it is possible for
@@ -436,9 +442,17 @@ def _message_generator(self):
436442 " since it is no longer fetchable" , tp )
437443 break
438444
445+ # If there is a seek during message iteration,
446+ # we should stop unpacking this message set and
447+ # wait for a new fetch response that aligns with the
448+ # new seek position
449+ elif self ._subscriptions .assignment [tp ].drop_pending_message_set :
450+ log .debug ("Skipping remainder of message set for partition %s" , tp )
451+ self ._subscriptions .assignment [tp ].drop_pending_message_set = False
452+ break
453+
439454 # Compressed messagesets may include earlier messages
440- # It is also possible that the user called seek()
441- elif msg .offset != self ._subscriptions .assignment [tp ].position :
455+ elif msg .offset < self ._subscriptions .assignment [tp ].position :
442456 log .debug ("Skipping message offset: %s (expecting %s)" ,
443457 msg .offset ,
444458 self ._subscriptions .assignment [tp ].position )
0 commit comments