Skip to content

fix: draw superseded responses while only the current index changes - #290

Open
hongquanli wants to merge 1 commit into
pyapp-kit:mainfrom
hongquanli:fix/draw-superseded-slider-frames
Open

hongquanli wants to merge 1 commit into
pyapp-kit:mainfrom
hongquanli:fix/draw-superseded-slider-frames

Conversation

@hongquanli

Copy link
Copy Markdown

Problem

Since #233, _on_data_response_ready draws a response only if it belongs to the latest request generation (gen != self._current_gen → return). Every slider move issues a new request. So when the slider is dragged faster than one frame can be fetched, every response arrives superseded and is dropped. The image freezes until the slider stops, then jumps to the final position. 0.4.x drew those responses, so the image followed the drag.

How visible this is depends on fetch time. In a downstream viewer (ndviewer_light, 3 channels of 2048 × 2048 uint16 at ~30 ms per fetch), dragging with a slider move every 8 ms drew 1 of 233 positions on 0.5.0, against 38 of 39 on 0.4.1.

Change

This keeps the guarantee the generation check provides: an older response never replaces newer data on screen, and nothing requested before a data/shape/layout change is ever drawn. It relaxes one case: a superseded response is still drawn if only the current index changed since it was requested.

  • _request_data(*, index_only=False): _apply_changes passes index_only=True when visible axes, channel axis and channel mode are unchanged. Every other request (including the data_changed connection, which uses the default) sets _min_drawable_gen to the new generation, which is exactly the old behaviour.
  • _on_dims_changed sets _min_drawable_gen past the current request, so responses fetched for the old shape (possibly other channels or extents) are not drawn as superseded frames. The current request still draws if nothing newer is issued, as before.
  • _cancel_futures removes only futures that were actually cancelled. Futures already running cannot be cancelled. They stay in _futures so their generation is known when they finish, and they pop themselves in the done callback as before.
  • _is_drawable(gen): gen == _current_gen, or _min_drawable_gen <= gen and _shown_gen <= gen. _shown_gen is updated when a response is drawn.

Net effect: during a drag the display shows the newest fetched frame (at most max_workers fetches are in flight; queued ones are still cancelled), and the final position draws when its fetch completes, as before.

Same 8 ms drag as above, with this change applied (as a shim downstream): 104 of 105 positions drawn.

Tests

tests/test_controller.py:

  • test_stale_response_discard now also sets _min_drawable_gen, so it models a superseded response from before a non-index change, which must still be discarded.
  • New tests:
    • a superseded index-only response is drawn;
    • a response older than the one on screen is discarded;
    • _cancel_futures cancels queued futures and keeps running ones with their generation;
    • a visible-axes change makes earlier generations undrawable;
    • a pure index change keeps them drawable;
    • _on_dims_changed makes earlier generations undrawable but keeps the current one.

All 6 fail without the change. Full suite on Windows / Python 3.10 / PyQt6: 159 passed, 24 skipped. ruff check, ruff format --check and typos are clean. mypy and pyright report the same counts as on main for _array_viewer.py.

🤖 Generated with Claude Code

Since pyapp-kit#233, a data response is drawn only if it belongs to the latest
request generation. Every slider move issues a new request (and cancels
queued ones), so while a slider is dragged faster than one frame loads,
every response arrives superseded and is dropped: the image stays frozen
until the slider stops. 0.4 drew those responses.

Keep the guarantee that motivated the generation check (an old response
never replaces newer data on screen, and nothing requested before a
data/shape/layout change is drawn), but draw a superseded response when
only the current index changed since it was requested:

- `_request_data(index_only=...)`: `_apply_changes` passes True when
  visible axes, channel axis and channel mode are unchanged. Any other
  request (including `data_changed`) moves `_min_drawable_gen` up to the
  new generation, as before.
- `_on_dims_changed` moves `_min_drawable_gen` past the current request,
  so responses for the old shape are not drawn as superseded frames.
- `_cancel_futures` keeps futures that already started (they cannot be
  cancelled) so their generation is known when they finish.
- `_is_drawable(gen)`: current generation, or not older than
  `_min_drawable_gen` and not older than the one on screen (`_shown_gen`).

Measured in a downstream viewer (3 channels of 2048 x 2048, ~30 ms per
fetch, a slider move every 8 ms): 1 of 233 positions drawn before, 104 of
105 after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.30%. Comparing base (7cfb38b) to head (bf9875b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #290      +/-   ##
==========================================
+ Coverage   85.20%   85.30%   +0.10%     
==========================================
  Files          53       53              
  Lines        7192     7202      +10     
==========================================
+ Hits         6128     6144      +16     
+ Misses       1064     1058       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +817 to +818
# not older than what is already on screen
return 0 <= gen and self._min_drawable_gen <= gen and self._shown_gen <= gen

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# not older than what is already on screen
return 0 <= gen and self._min_drawable_gen <= gen and self._shown_gen <= gen
# not older than what is already on screen
# and no newer response is already waiting to be drawn
if gen < max(self._min_drawable_gen, self._shown_gen):
return False
return not any(
g > gen and f.done() and not f.cancelled() for f, g in self._futures.items()
)

I think we also want to skip a superseded response if a newer one has already finished and is just waiting for its callback to run on the main thread. Otherwise, whenever several requests finish before the event loop gets to them (e.g. a burst of index updates while the main thread is busy), they all pass the _shown_gen <= gen check (since they arrive in order) and we replay the whole backlog.

note: this breaks the last assertion in test_index_change_keeps_earlier_responses_drawable (before isn't drawable once the newer request is done but not yet drawn). I'd just drop that line, the _min_drawable_gen check above it already covers the intent. And maybe add a test for this:

@no_type_check
@_patch_views
def test_superseded_response_skipped_if_newer_done() -> None:
    """A superseded response is skipped when a newer one is already waiting."""
    ctrl = ArrayViewer()
    ctrl._current_gen = 5
    ctrl._min_drawable_gen = 1
    newer = _response_future(ctrl, 4, value=4)  # done, callback not yet run

    ctrl._on_data_response_ready(_response_future(ctrl, 3, value=3))
    ctrl._canvas.refresh.assert_not_called()

    ctrl._on_data_response_ready(newer)
    assert ctrl._shown_gen == 4
    ctrl._canvas.refresh.assert_called_once()

@tlambert03 tlambert03 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks @hongquanli ... small change requested

This branch has not been deployed

No deployments
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.

2 participants