Skip to content

#638: the Linux engine's clipboard tests stamp their requests after the windows they mean, not after a sleep (test-only) - #640

Merged
SSFSKIM merged 4 commits into
mainfrom
fix/638-clipboard-refused-window
Nov 30, 2026
Merged

SSFSKIM merged 4 commits into
mainfrom
fix/638-clipboard-refused-window

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Closes #638

Under runner load, the Linux engine's clipboard suite failed on PR #637 (run 46423943167): attempt 2 failed
a_pause_before_taking_clipboard_does_not_shorten_the_refused_window, and attempt 3 failed
an_unconfirmed_paste_s_current_time_refusal_covers_the_next_paste_too. This PR changes tests only. The production
code is the same as main and linux-engine stays at 0.4.8. One real race in the code remains, and it is tracked by
#639.

The tests that were wrong

Two live tests read a server timestamp just after a take or a cleanup and assumed it fell in a later millisecond.
They fail most often when things run fast and share a millisecond, not only under load.

  • an_unconfirmed_paste_s_current_time_refusal_covers_the_next_paste_too read the next paste's Ctrl+V time right
    after the first paste's cleanup. That time was still inside the first paste's refused window, whose end is estimated
    late on purpose, so the Ctrl+V's own conversion was refused. This was the CI failure on attempt 3:
    expected data, got Refused.
  • a_late_conversion_never_receives_the_restored_snapshot_as_the_paste used ctrl_v - 1 as a time "from ownership
    on". That time falls before the ownership when the take and the Ctrl+V share a millisecond. It also waited 50 ms
    before reading a time it needed to be past the window.

Both now wait for the server's clock to pass the time they actually mean, using a new helper, server_time_after.
That time is either the window's end as the code records it (Closed::cleaned_at, read through a test-only
Clipboard::last_closed), or the ownership timestamp, which the test now reads itself. Two more ceilings are now exact
checks:

  • the "both locks were free again" check takes the lock with no wait (lock_within(ZERO)), instead of timing a 100 ms
    wait against 50 ms;
  • the 10 s refusal's ceiling is the bound the code guarantees (CURRENT_TIME_REFUSAL plus the window's length),
    instead of a fixed 10.1 s.

What each test asserts is unchanged. The tests that measure the engine's own time limits (500 ms for TARGETS, 2 s per
INCR chunk, the lock's wait, the refusal's 9 s floor) still use the wall clock, with their existing slack, because
those limits are what they measure.

The race that remains

a_pause_before_taking_clipboard_does_not_shorten_the_refused_window failed on attempt 2 because of a real race, and
it stays enabled as that race's evidence. Run alone under stress-ng --cpu 8, it fails about 1 time in 300.

When a paste is cleaned up, the engine estimates the X server's time from its own CLOCK_MONOTONIC. Xorg reads
CLOCK_MONOTONIC_COARSE (os/utils.c, GetTimeInMillis), which lagged by up to 1.3 ms here. So the refused window
can end a millisecond or so before the server's time at the cleanup.

A fixed allowance was tried and dropped in review. The fix is to stamp the cleanup with a timestamp the server issues,
and it needs a linux-engine release: #639.

Evidence

Runs on an 8-vCPU KVM guest under stress-ng --cpu 8. The clipboard suite has 44 tests, runs on one thread as
engine-linux runs it, and uses a private Xvfb.

test before (main) after (this PR)
an_unconfirmed_paste_s_current_time_refusal_covers_the_next_paste_too 25 of 47 runs 0 of 200
a_late_conversion_never_receives_the_restored_snapshot_as_the_paste 10 of 47 runs 0 of 200
a_pause_before_taking_clipboard_does_not_shorten_the_refused_window (the race, #639) 0 of 47 in the suite; 1 of 300 alone 1 of 200 (the known race)
the other 41 clipboard tests 0 of 47 0 of 200
suite runs with any failure 27 of 47 1 of 200 (that test)

ci-gate at 90c28a0: GREEN (plugin, test-1, test-2, engine-linux), https://github.com/SSFSKIM/cua/actions/runs/46461435772

Unresolved Review Findings

The final check (reviewer-medium on 4edf7b6..90c28a0) confirmed that production behaviour and the engine version are unchanged and that the three accessors compile only for tests. It found that two of the rewritten assertions lost some coverage. They ship with this PR to unblock CI, and the follow-up for the race, #639, rewrites the cleanup timestamp and these same tests, where an independent observation becomes available.

  • The late-conversion test accepts production's own cutoff (native/linux-engine/src/clipboard/tests.rs:564-570). It waits for Closed::cleaned_at as production computed it, so it would pass even if that cutoff were wrong. For example, an accidental 500 ms extension that the old test's 50 ms wait would have caught. Fix: keep the condition-driven wait, but take the acceptance timestamp from an independent server-clock observation around the cleanup.
  • The quarantine ceiling grows with production's numbers (tests.rs:598-603). The old test held the remaining quarantine to at most 10.1 s. The new ceiling grows with the duration constant and the computed ownership-to-cleanup interval, so a regression adding 300 ms could pass while a paste is held for 500 ms. Fix: an independent 10 s expectation, allowing only measured timestamp uncertainty.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)

…han the server's time at its cleanup

The cleanup estimates the server's time as the ownership timestamp plus the
engine's elapsed time since an anchor taken before that timestamp was read,
plus 1 ms. That holds only if the server's clock never runs ahead of the
engine's over the interval. The X server reads CLOCK_MONOTONIC_COARSE
(GetTimeInMillis, os/utils.c, when its resolution is 1 ms or finer), which
stands still between ticks: when it lags at the ownership read and has caught
up by the cleanup, it has run further than the engine's CLOCK_MONOTONIC by
that lag. 1 ms covers one tick on time; a late tick (1.3 ms measured on this
KVM guest under stress-ng) left the window's last millisecond unrefused, so a
conversion stamped then was served the restored snapshot. That is
a_pause_before_taking_clipboard_does_not_shorten_the_refused_window's failure
on PR #637's run 46423943167 (1 in 300 runs here under load).

The estimate moves into serving::cleaned_at and adds SERVER_CLOCK_LAG (10 ms):
a tick of 1 ms delivered up to 9 ms late. The window refuses 10 ms more after
the cleanup, far less than any next paste's distance. The new unit test sweeps
the anchor, the read's delay, the lag and the hold across the clock's wrap; it
failed on the old estimate at a lag of 1.3 ms.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
… mean, not after a sleep

Two live tests read a server timestamp right after a take or a cleanup and
assumed it fell on the far side of a millisecond boundary:

- an_unconfirmed_paste_s_current_time_refusal_covers_the_next_paste_too read
  the next paste's Ctrl+V time right after the first paste's cleanup, inside
  the first paste's window, whose end is estimated late on purpose; the
  Ctrl+V's conversion was refused (PR #637's run 46423943167, attempt 3;
  25 of 47 suite runs here under stress-ng).
- a_late_conversion_never_receives_the_restored_snapshot_as_the_paste took
  ctrl_v - 1 as a time "from ownership on", which it is not when the take and
  the Ctrl+V share a millisecond, and waited 50 ms for a request stamped after
  the window (10 of 47).

They now wait for the server's clock to pass the time they mean
(server_time_after): the first paste's cleanup estimate, read from the owner's
last closed refusal (Clipboard::last_closed, tests only), or the ownership
timestamp, which the late-conversion test now reads itself through take_from.
What each asserts is unchanged.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
…exact checks

- a_silent_owner_ends_the_snapshot_at_its_bound_and_the_locks_are_released
  checked that both locks were free again by timing a 100 ms lock wait under
  50 ms; it now takes them with no wait at all (lock_within(ZERO): one try
  each), which passes only when they are free at once.
- only_an_unconfirmed_paste_that_was_sent_opens_the_current_time_refusal held
  the refusal's remaining time under 10.1 s, which the take's scheduling delay
  under load can pass; the ceiling is now what the code guarantees, the
  refusal past the cleanup's estimate, read from the closed paste's window.

The engine's own bounds (500 ms for TARGETS, 2 s per INCR chunk, the lock's
wait, the 10 s refusal's floor) stay checked against the wall clock with their
existing slack: they are what those tests measure.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
…ch is test-only

Reverts da1d383. Review (ruling 1365) found that padding the cleanup's estimate is
the wrong fix for the race it named. A coarse clock that lags by more than the padding
still ends the window early. The padding refuses genuine conversions made just after
a cleanup. And it can push the display's quarantine record past the 11 s that peer
engines accept. The right fix bounds the window with a timestamp the server issues at
the cleanup, and it needs its own release, tracked by a follow-up ticket.
a_pause_before_taking_clipboard_does_not_shorten_the_refused_window stays as it is:
it is that race's evidence.

What remains of the branch is in the tests. The tests wait on the endpoint the code
computes now, Closed::cleaned_at, which they read through the test-only last_closed
accessors. Production code matches main.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T17:10:58.683857Z 90c28a0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@SSFSKIM
SSFSKIM enabled auto-merge (squash) November 30, 2026 17:08
@SSFSKIM
SSFSKIM merged commit 29dd256 into main Nov 30, 2026
12 of 14 checks passed
SSFSKIM added a commit that referenced this pull request Dec 1, 2026
Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
SSFSKIM added a commit that referenced this pull request Dec 1, 2026
…nged, up to 3 times, logging each failed attempt (test-only) (#646)

* #641: live_device's tail tests read the tail by its order, not by a 20 ms span before the release

Three tests took "the tail" to be whatever the drag source read in the 20 ms
before the release. The tail is 8 ms, a visit, 8 ms, a visit, 8 ms, then the
release. A loaded machine stretches those rests, so the first visit falls
outside the span. The engine still sent every visit; its record says motions 4.

- a_chrome_handle_s_end_with_a_drop_under_way_gets_the_tail_and_off_gives_the_old_end
  (#640's run: 2 of the 4 pairs, the last at 11.46 ms; 3 of 300 runs here under
  stress-ng). It now reads the tail from its first step, x 61, which the path
  and the end's revisit never reach.
- on_xtest_routes_each_tail_visit_steps_forward_and_back_and_shows_nothing_behind_the_end:
  the tail is read from the first step forward of the end.
- a_chrome_drop_whose_path_outlasts_its_floor_..._inside_its_floor, through
  source_before_release: it now gives the last three motions the source read,
  the tail's two and the one before them, oldest first. "Two in the last 20 ms"
  becomes "the last two, each a rest after the one before it".

The in-floor case of that last test is also timed to the millisecond on the
engine's own clock: released 350 to 351 ms after the take, no visit dropped,
and 6 to 20 ms after the last motion. A loaded machine wakes the engine or the
source late. That shows in the engine's record (past the floor, or a visit
dropped) or in the source's reads (6 of 60 runs here failed one of those
bounds). Such an attempt now runs again, up to 3 times, as the visit-dropped
cases in this file already do. The last attempt is held to every bound, so a
release that is always late still fails. What each test asserts is unchanged.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)

* #641: the tail tests' rests, read on the source's poll, run again when a loaded machine holds that poll up

200 runs of the first commit under stress-ng: the order-based tails passed
every time (the handle test always got its four pairs). Four runs still
failed. The cause was the source's own reads: its 1 ms poll was held up, so it
read a visit late and a rest measured short. The visits were 5.3 ms apart
instead of 8 in the handle test and on the background route, and the release
came 3.2 ms after the last motion on the dedicated route. The engine's record
showed it on time.

The handle test's cases and the outlasting path's routes now run again, up to
3 times, when a rest or a gap reads outside its bound, as the in-floor case
already does. The last attempt is held to every bound. Structural checks (the
places, the counts in the engine's record, the route) are still checked on
every attempt.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)

* #641: inside the floor, off and the tail run again apart

After d67c6b6, one run in 217 under stress-ng failed the in-floor case. Its
three attempts were each disturbed: off woke late twice (354 and 364 ms after
the take), then the tail's first visit was read 5.4 ms before its second.
When off and the tail shared an attempt, a late off cost the tail an attempt
too. They now run again apart, each up to 3 times, and each final attempt is
held to its bounds. The tail's bounds include being within a millisecond of
off's release.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)

* #641: the review of 9fb56bd: the tails are read from the path's own boundary, and every rest has a ceiling

The review found four regressions that the new selection rules let through.
Each now fails:

1. Inside the floor, a visit recorded as sent that never arrived. Two spaced
   path motions and one visit passed every spacing check. floor_visits now
   reads from the path's arrival at its end, which a path reaches once, on its
   last sample. After it, there must be exactly the record's keepalives and
   two visits, all at the end. When the engine records both visits as sent,
   that count is asserted on every attempt. Only a visit the engine itself
   dropped can send the attempt round again.
2. On XTEST, a malformed first visit before the first good step. xtest_tail
   reads from the path's arrival, not from the step's place: only the end and
   the step after it, the step at least once, the end last. The handle's tail
   is read the same way (handle_visits): the end's revisits, then exactly the
   two visits.
3. A handle's visits 80 ms further apart. Each rest now has a ceiling,
   REST_READ_MAX (20 ms). It is the release rest's existing bound in these
   tests: the 8 ms rest plus room for a wakeup or a 1 ms poll that a loaded
   machine holds up. The in-floor visits share it.
4. Off, a stray step before the end's revisit. off_sequence (the end last, no
   step) runs on every attempt, before the rests decide whether to run again.

the_tail_checks_refuse_the_review_s_traces checks each helper against the
review's traces and against traces it must accept. The rules as 9fb56bd had
them passed all four traces; I checked that in a scratch test, not committed.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)

* #641: inside the floor, the path's last step is found by the engine's record, not its first arrival at the end

On this Xvfb, the short path reaches its end on two samples, about 42 and 33 ms
before the release. So "after the path's first arrival" counted one path
motion as a keepalive, and every run failed. The boundary is now the path's
last step as the engine records it: release_after_take_ms minus
last_step_after_take_ms, on its own clock. It is found among the motions by
their own times. On the background route that is the time the engine stamped
each motion with as it sent it (Session::now), which the source's held-up
reads do not move; the source now keeps those times in AFTER_TAKE_STAMPS.
After the boundary there must be exactly the record's keepalives and two
visits at the end. The rests are still measured on the source's reads. The
trace test takes the review's trace by stamps, plus a path that reaches its
end twice.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)

* #641: main's tail checks exactly as written, run up to 3 times (ruling 1394)

Every rewrite of how the three tail tests select the tail turned out weaker
than main's checks somewhere, or wrong about the engine. So they are gone:
the order-based selection, AFTER_TAKE_STAMPS, REST_READ_MAX, floor_visits,
xtest_tail, handle_visits and the trace test. The three tests now run main's
bodies verbatim, the 20 ms window included, inside passes_in_one_of:
- each attempt runs every one of main's checks;
- the test passes when one attempt passes them all, up to TAIL_ATTEMPTS = 3;
- a fault the engine repeats fails all three.

Ignoring indentation, the change against main is that wrapper and one
expression rustfmt split over three lines.

Each attempt starts clean, as main's body does: a fresh display, the path set
again, and a drag source of its own. A failed attempt's dropped Sender stops
its source's loop.

Before a retry, the failed attempt's assertion (its message, with the values
it judged) is written to stderr past libtest's capture, so CI's log shows an
intermittent fault even when a later attempt passes.

Sminos-Session: linux171#3c5ca588 (cua on dc-linux171-1e68)
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.

Linux engine: clipboard refused-window test is timing-flaky under hosted load

1 participant