GNU bug report logs - #82091
X: x-own-selection-internal records ownership the server ignored; Emacs then serves a stale selection

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Xinyang Chen <chenxinyang99@HIDDEN>; dated Fri, 23 Oct 2026 00:59:04 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 82091 <at> debbugs.gnu.org:


Received: (at 82091) by debbugs.gnu.org; 7 Nov 2026 10:42:14 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Nov 07 05:42:14 2026
Received: from localhost ([127.0.0.1]:38560 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1xPdsC-00027v-B1
	for submit <at> debbugs.gnu.org; Sat, 07 Nov 2026 05:42:13 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10]:46362)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <eliz@HIDDEN>) id 1xPds8-00026L-N8
 for 82091 <at> debbugs.gnu.org; Sat, 07 Nov 2026 05:42:10 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1xPds3-0002im-Dz; Sat, 07 Nov 2026 05:42:03 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=IsMXoZVDre60dbLZY1Ebh2jFDa9y07041oRXUAlAQm8=; b=Bb6OYnWQ18hY
 7mvlgsX32Nt1z7KgscRO80B98pXz4sYf8M3EHcNiMLIXO82PDTI63SgQOBvS3lwb5Y73bqq1i6m98
 93AkcdtT5NQZAN77Et49kd+Nc/u27vIm5im7U25HwzNFiHzYvVuc/0A1dY2D47nqR0dJwmkH8AXj8
 2M69Wc1pcVnXDvVgq4MOnF40pdtxa2Sluh4r5k1r1FG2TjdmDm9z2vh8yHtBjhfYhoR4xCjEsf/xU
 3xK78BVWPDN9p/pyHFzO5dvTQSPIofTFLMXTyefeP6G1KgrK7VZ1ZFuTeu7qL/a2I43G5eHcam7Vk
 HwkBsW8qtRMQVeOj/+jcyw==;
Date: Sat, 07 Nov 2026 12:42:00 +0200
Message-Id: <86h5hskjnr.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Xinyang Chen <chenxinyang99@HIDDEN>
In-Reply-To: <CANnQ9Xbcu+k7KNv9SFyJ1yfJrsr6PwX9WyvD1YMhbUAY594nwA@HIDDEN>
 (message from Xinyang Chen on Thu, 22 Oct 2026 18:15:40 -0700)
Subject: Re: bug#82091: X: x-own-selection-internal records ownership the
 server ignored; Emacs then serves a stale selection
References: <CANnQ9XZDKpTct28_-qRR3MgoyboKSNt1jivkCoO6S2ppSwWRoA@HIDDEN>
 <CANnQ9Xbcu+k7KNv9SFyJ1yfJrsr6PwX9WyvD1YMhbUAY594nwA@HIDDEN>
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 82091
Cc: 82091 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

Ping!  Did you have a chance to work on this?

> From: Xinyang Chen <chenxinyang99@HIDDEN>
> Date: Thu, 22 Oct 2026 18:15:40 -0700
> 
> This report was sent by mistake and is untested. I am still verifying it and will follow up.
> 
> On Thu, Oct 22, 2026 08:57 PM, Xinyang Chen <chenxinyang99@HIDDEN> wrote:
> 
>  On X, if another client takes a selection after Emacs's last input event,
> and Emacs then claims that selection, the X server ignores Emacs's
> SetSelectionOwner.  Emacs records itself as the owner anyway.  From then on
> it answers every request for that selection, its own yanks included, from
> its local copy.  Having never been the owner, it never gets a
> SelectionClear, so text copied in other programs stays invisible to Emacs
> until Emacs next claims the selection with a newer timestamp.
> 
> Recipe
> 
> X11 build (not PGTK), `emacs -Q`, `xclip` in a terminal:
> 
> 1. `M-: (run-with-timer 5 nil (lambda () (kill-new "from-emacs"))) RET`
> 2. Switch to the terminal and, within the 5 seconds, run
>    `printf other | xclip -selection clipboard`.  Stay there until the
>    timer has run.
> 3. `xclip -o -selection clipboard` prints `other`: Emacs's claim did not
>    take.
> 4. `printf later | xclip -selection clipboard`
> 5. Back in Emacs, `M-: (x-selection-owner-p 'CLIPBOARD) RET` gives `t`,
>    and `C-y` inserts `from-emacs`, not `later`.  It keeps doing so.
> 
> Expected: either the claim in step 1 takes effect (step 3 prints
> `from-emacs`), or Emacs knows it failed and step 5 yanks `later`.
> 
> A scripted version on Xvfb gave this with GNU Emacs 30.2 (X11, Lucid;
> build details below), `emacs -Q`.  The script presses a key in the Emacs
> frame with xdotool, claims with xclip, then runs the Emacs side through
> emacsclient:
> 
>     == kill-new from a timer
>       xclip -o: other2
>       emacs:    (:owner-p t :yank "from-timer" :C-y "from-timer")
>     == another program copies afterwards
>       xclip -o: later
>       emacs:    (:owner-p t :C-y "from-timer")
> 
> I hit it in daily use with a 31.0.50 GTK3 build on Xwayland.
> `x_own_selection` on master (0ed4fc4646, 2026-10-22) has the same code.
> 
> Cause
> 
> `x_own_selection` (src/xselect.c) calls `XSetSelectionOwner` with
> `dpyinfo->last_user_time` when it is given no timestamp, then adds the
> selection to `Vselection_alist` unconditionally.  The server ignores a
> SetSelectionOwner whose time is earlier than the selection's last-change
> time.  ICCCM 2.1, "Acquiring Selection Ownership", requires this check:
> 
>     SetSelectionOwner(selection=PRIMARY, owner=Window, time=timestamp)
>     owner = GetSelectionOwner(selection=PRIMARY)
>     if (owner != Window) Failure
> 
> `x-get-selection-internal` consults the local value first, and
> `x-selection-owner-p` and `x-selection-exists-p` both return t, so nothing
> in Emacs can tell that it does not own the selection.
> 
> The same false belief lets Emacs clear a selection it never owned:
> `x-disown-selection-internal`, called after a fresh input event (for
> example `M-: (x-disown-selection-internal 'CLIPBOARD) RET` after step 4),
> sends SetSelectionOwner(None) with that event's time.  The server accepts
> it whoever the owner is, and the other program's `later` is gone
> (`xclip -o` then reports `Error: target STRING not available`).
> 
> How it happens in practice
> 
> The claim needs a timestamp older than another client's claim.  That
> happens without any timer:
> 
> - Any claim made without a fresh input event: `kill-new` from timers,
>   from process filters (OSC 52 handlers in terminal emulators such as
>   vterm), or from emacsclient.
> - Mouse drags.  Under XInput 2, `last_user_time` is updated on
>   XI_ButtonPress but not on XI_ButtonRelease, so a selection claimed at the
>   end of a drag carries the press time.
> - Xwayland under cosmic-comp (the COSMIC desktop).  A selection made in a
>   Wayland client while no X window has keyboard focus is held back and
>   handed to X when an X window next gains focus
>   (`xwayland_notify_focus_change` in cosmic-comp's src/xwayland.rs), and
>   Smithay's X window manager then claims it with CurrentTime.  The click
>   that focuses Emacs can therefore carry an earlier time than that claim.
>   Here a drag in a vterm running tmux ends with an OSC 52 copy that
>   `kill-new`s with the press time, and the claim is ignored.  In about 90
>   minutes of logging, the compositor handed selections to X 32 times, each
>   within 50 ms of keyboard focus moving from a Wayland window into an X
>   window.
> 
> Possible fix
> 
> Check ownership after `XSetSelectionOwner` in `x_own_selection`.  On
> failure, either signal an error without recording the selection, or, when
> the timestamp came from `last_user_time` rather than from the caller, retry
> once with a fresh server timestamp.  `x_get_server_time` in xterm.c,
> currently static, gets one from a PropertyNotify, much as ICCCM 2.1
> suggests, and a server timestamp is never earlier than the last-change
> time.  ICCCM asks for the triggering event's time, but a claim from Lisp
> often has no triggering event, and `last_user_time` only stands in for
> one.
> 
> The workaround I use: before claiming a selection Emacs does not own, ask
> the current owner for TIMESTAMP (which owners must support, ICCCM 2.6.2)
> and raise `last_user_time` to it:
> 
>     (advice-add
>      'x-own-selection-internal :before
>      (lambda (selection _value &optional frame)
>        (when (and (not (x-selection-owner-p selection frame))
>                   (x-selection-exists-p selection frame))
>          (let* ((x-selection-timeout 1000)
>                 (time (x-get-selection-internal
>                        selection 'TIMESTAMP nil frame)))
>            (when (natnump time)
>              (x-display-set-last-user-time time frame))))))
> 
> With `emacs -Q` 30.2 on Xvfb, this lets the claim through against an owner
> that answers TIMESTAMP.  It does nothing against xclip 0.13, which answers
> every target other than TARGETS with its text, so a TIMESTAMP request gets
> `"other"`.  A server timestamp from Emacs's side does not depend on the
> owner, which is one reason to fix this in `x_own_selection`.
> 
> Related, minor
> 
> Emacs tracks foreign selection owners for `x-selection-exists-p`
> (`x_find_selection_owner`) with XFixes, but `x_term_init` selects only
> SetSelectionOwnerNotify and SelectionClientCloseNotify, not
> SelectionWindowDestroyNotify.  After an owner destroys its window while
> staying connected, `x-selection-exists-p` keeps returning t although the
> selection has no owner.  I hit this while testing the workaround above;
> it may deserve its own report.
> 
> Build used for the recipe output
> 
> In GNU Emacs 30.2 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
> version 1.18.4, Xaw3d scroll bars)
> System Description: Ubuntu 24.04.4 LTS
> 
> Configured using:
>  'configure
>  --prefix=/nix/store/ghk7vc0l1baxsy0mjcswz2spdw8zgi0l-emacs-30.2
>  --disable-build-details --with-modules --with-x-toolkit=lucid
>  --with-cairo --without-xft --disable-gc-mark-trace
>  --with-compress-install --with-toolkit-scroll-bars
>  --with-native-compilation --without-imagemagick --with-mailutils
>  --without-small-ja-dic --with-tree-sitter --with-xinput2
>  --without-xwidgets --with-dbus --with-selinux'




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#82091; Package emacs. Full text available.

Message received at 82091 <at> debbugs.gnu.org:


Received: (at 82091) by debbugs.gnu.org; 23 Oct 2026 01:15:51 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Oct 22 21:15:51 2026
Received: from localhost ([127.0.0.1]:56609 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1xK3sr-0006oz-LN
	for submit <at> debbugs.gnu.org; Thu, 22 Oct 2026 21:15:51 -0400
Received: from mail-yw1-x112c.google.com ([2607:f8b0:4864:20::112c]:51452)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.84_2) (envelope-from <chenxinyang99@HIDDEN>)
 id 1xK3sp-0006no-3Y
 for 82091 <at> debbugs.gnu.org; Thu, 22 Oct 2026 21:15:48 -0400
Received: by mail-yw1-x112c.google.com with SMTP id
 00721157ae682-8b0a28a720aso4303967b3.2
 for <82091 <at> debbugs.gnu.org>; Thu, 22 Oct 2026 18:15:47 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1792718141; cv=none;
 d=google.com; s=arc-20260327;
 b=MZEDHLU/KGRZFasG5F77nDh+MX4uqj6PdgLNeM9AH4mdeGaF32OEatHzCkWs1b+QgY
 hNLd60Is9rtidgJakGcDWzKblbZ9SZ0d3XflPNf0BBe4UF2YjRHU36+q0OfhqzEKDjw9
 VcoaRRHznCXK7OGICMvpDzLbl+lJfkJKcWAZBSwf8Vu3aZapwGLxrZr3OuDnKX9W4T6i
 5p+PypFcq4bb4739za4twe2Z5lo04z6p3G7ZnXVp1zjGMoWM/u6KhpTio17vprNFuolY
 SyI+yob7KlvEyBN2qU1mldVBBavWil5ZuA+Eve+/U2l3AJJdR3+d61NZhWeDStjk6x4B
 KOjg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;
 s=arc-20260327; 
 h=to:subject:message-id:date:mime-version:references:in-reply-to:from
 :dkim-signature;
 bh=j/8q6ur3tCEFzxm5yRk0n3zE3CBkwZG9EkQNox7AqaQ=;
 fh=tsH4/VUoL0D5IukcyRHILYFfjTK6PXxSBNj+hfxJ+BY=;
 b=SbqxnMLF6cuCqkFfaCtuuV3VQAc9lsONtfLWF5vyvhsnYBbXn6Uvzrtl/VFHF+SHle
 x4vhXFGsgYipCgfylmcJmbUi3FFm9Y0YI3JaV+2+8vFvaE+69XVRR1MQgRzd1/KDitiU
 XVaDp/KtrUdRTxcMJHm6Si/lveMuyasZ7NsLZJ4wK+ZaIm9RETFmfN9ee8ISoz9e5zEP
 4syoNyyoyfOAwp7UVRVC0oH1AbOUy2o0TQd83vRmZnXlByn3+RM/K4pthVwctHxoMn2h
 vmPZFMWYKu6+ZZ/35SQsWUOnGuCxpmB6TPO5V4oqwsCbodt8eVBSvUnqko7OoDL2g/d+
 cwDg==; darn=debbugs.gnu.org
ARC-Authentication-Results: i=1; mx.google.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20251104; t=1792718141; x=1793322941; darn=debbugs.gnu.org;
 h=content-type:to:subject:message-id:date:mime-version:references
 :in-reply-to:from:from:to:cc:subject:date:message-id:reply-to
 :content-type; bh=j/8q6ur3tCEFzxm5yRk0n3zE3CBkwZG9EkQNox7AqaQ=;
 b=EfQcuIlsqQXWSE01f139ckIu+ZJDkbTBPdLiXA2hMvF36gBipfQLXELBP0N/h9qeXa
 sMfd/OfGmQNKBjr3fauql15SZtjuFw5cXxKphH5I4VIJlKYSSPTldhKpwcLMKav1eCJv
 23eSeIbhbrADZ1yAlz0bUuxi6/9bajlTIJBmLJ8282yQU/p+U3wMehkdqVniCKGqS0j3
 xUO097w0l6CRvnsUdlkp21RJzbGY0/g0LgB0T0Drrth1cPD8AGj07NqedHa01pXBGywG
 IcO6AhYCI2pcYh4mTa9HgIkcf874tSD758TTMvx/a8MtyzP1I9mSiyrDgRs2YbwWbbmS
 rslg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20260707; t=1792718141; x=1793322941;
 h=content-type:to:subject:message-id:date:mime-version:references
 :in-reply-to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date
 :message-id:reply-to:content-type;
 bh=j/8q6ur3tCEFzxm5yRk0n3zE3CBkwZG9EkQNox7AqaQ=;
 b=YK8/jdz6LKSaI5B/J360cE1cIRLhPJ9j2d0TvLa8pWb3Uuu7tr8ZW5hcykDVCPxU8Z
 wqOep1v0AfNR1xTNO7dj30zf7JT7ZI31Etf4lkSDD8WUmuq1ob+N8GkoX7Xi9sYOUXth
 Dqkp3QQ+KOazJ6hzEa2nDXYpiH1tT0VH+2CG7SSUQfY0J1/pNA7Y7tNEaH1BTYeM/EQ3
 +AOW/BpCcLp114wVfIbJsUHtrwJKXbamlvDCEr5P5ruosir2U3d8Ivjzg/MPoboCJIFZ
 nidrmbqv9sP6tuXgvjWMmH3l662LyPghFlqzd0dfxbFHNrNztecXcuXrfDxoKbojTw3u
 BRdQ==
X-Gm-Message-State: AFq9FYLHnWhc8iMNavQKDspvAe6Ogp/SsMBnQi+hlOG9w7S8BB2BP4Q0
 KLYug8SV/tRwH5wTSjeuPM5YIeNxOHrKU4W8X3BVtF6vFXmqzhUkxK5ejYpZiMtgVYLZnv3vzzL
 HdO2nBCH39En7gaA4AijfSo4mdqY3piltVA==
X-Gm-Gg: AYBFou3oOhMMx8eb1Raai7AFF003zfl9C8Ak//FloFx0N/5OjMQ7LP+9QW68c42BiRD
 6YdMnn2gbn7JE5PbPHwuOWh7qwXmHNim/rpxISYgf2aZNRnyZ4LD3qRCWv4H91xvI9L3uGCMAQx
 huchzwjG81BLM1952VLamjBJsJXeDJ1MhvEa9JSmYGbXNNrMPx6OATYRSrlXMucVXwt5I5gcTni
 1ewc2meQdboyg7JilQjmkj400HqTvLIV8zXpg8DuEY3HU4LPL0jQbuQzmnYEMJQZ0uIISLsLOQ4
 VN1ve7UQiPAUgwy4rt8Gso8zb9S5qzfURi3otEp4NXzy03zRm2nX1btAZCx5hAlqlmE5vxYuExN
 AjxDaxUDvHeLnng1hvoyE7EVszkRy9p6ZI2S2Q5r5YxoQyazUklIlyovW3DuvWhYqHEUTy8v405
 jdYFUkutZi/BQArt39R52BWA==
X-Received: by 2002:a05:690c:113:b0:887:f4fc:ed49 with SMTP id
 00721157ae682-8b157da3be8mr5893537b3.50.1792718141156; Thu, 22 Oct 2026
 18:15:41 -0700 (PDT)
Received: from 101988054943 named unknown by gmailapi.google.com with
 HTTPREST; Thu, 22 Oct 2026 18:15:40 -0700
Received: from 101988054943 named unknown by gmailapi.google.com with
 HTTPREST; Thu, 22 Oct 2026 18:15:40 -0700
From: Xinyang Chen <chenxinyang99@HIDDEN>
In-Reply-To: <CANnQ9XZDKpTct28_-qRR3MgoyboKSNt1jivkCoO6S2ppSwWRoA@HIDDEN>
References: <CANnQ9XZDKpTct28_-qRR3MgoyboKSNt1jivkCoO6S2ppSwWRoA@HIDDEN>
MIME-Version: 1.0
Date: Thu, 22 Oct 2026 18:15:40 -0700
X-Gm-Features: AclHuK_3B4IqElcaTFwL0vAbR5Q_oxd9pXga_MKIsMPMbaELNIO_EIq2xi_fcrs
Message-ID: <CANnQ9Xbcu+k7KNv9SFyJ1yfJrsr6PwX9WyvD1YMhbUAY594nwA@HIDDEN>
Subject: Re: bug#82091: X: x-own-selection-internal records ownership the
 server ignored; Emacs then serves a stale selection
To: 82091 <at> debbugs.gnu.org
Content-Type: multipart/alternative; boundary="000000000000fc7417065e77b741"
X-Spam-Score: 1.3 (+)
X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org",
 has NOT identified this incoming email as spam.  The original
 message has been attached to this so you can view it or label
 similar future email.  If you have any questions, see
 the administrator of that system for details.
 Content preview: This report was sent by mistake and is untested. I am still
 verifying it and will follow up. On Thu, Oct 22, 2026 08:57 PM, Xinyang Chen
 wrote: > On X, if another client takes a selection after Emacs's last input
 event, > and Emacs then claims that selection, the X server ignores Emacs's
 > SetSelectionOwner. Emacs records itself as the owner [...] 
 Content analysis details:   (1.3 points, 10.0 required)
 pts rule name              description
 ---- ---------------------- --------------------------------------------------
 -0.0 SPF_PASS               SPF: sender matches SPF record
 1.0 FORGED_GMAIL_RCVD      'From' gmail.com does not match 'Received'
 headers
 0.0 SPF_HELO_NONE          SPF: HELO does not publish an SPF Record
 0.0 FREEMAIL_FROM          Sender email is commonly abused enduser mail
 provider (chenxinyang99[at]gmail.com)
 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends
 in digit (chenxinyang99[at]gmail.com)
 -0.0 RCVD_IN_DNSWL_NONE     RBL: Sender listed at https://www.dnswl.org/,
 no trust [2607:f8b0:4864:20:0:0:0:112c listed in]
 [list.dnswl.org]
 0.0 HTML_MESSAGE           BODY: HTML included in message
 0.0 UNPARSEABLE_RELAY      Informational: message has unparseable relay
 lines
X-Debbugs-Envelope-To: 82091
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: 0.3 (/)

--000000000000fc7417065e77b741
Content-Type: text/plain; charset="UTF-8"

This report was sent by mistake and is untested. I am still verifying it
and will follow up.

On Thu, Oct 22, 2026 08:57 PM, Xinyang Chen <chenxinyang99@HIDDEN> wrote:

> On X, if another client takes a selection after Emacs's last input event,
> and Emacs then claims that selection, the X server ignores Emacs's
> SetSelectionOwner.  Emacs records itself as the owner anyway.  From then on
> it answers every request for that selection, its own yanks included, from
> its local copy.  Having never been the owner, it never gets a
> SelectionClear, so text copied in other programs stays invisible to Emacs
> until Emacs next claims the selection with a newer timestamp.
>
> Recipe
>
> X11 build (not PGTK), `emacs -Q`, `xclip` in a terminal:
>
> 1. `M-: (run-with-timer 5 nil (lambda () (kill-new "from-emacs"))) RET`
> 2. Switch to the terminal and, within the 5 seconds, run
>    `printf other | xclip -selection clipboard`.  Stay there until the
>    timer has run.
> 3. `xclip -o -selection clipboard` prints `other`: Emacs's claim did not
>    take.
> 4. `printf later | xclip -selection clipboard`
> 5. Back in Emacs, `M-: (x-selection-owner-p 'CLIPBOARD) RET` gives `t`,
>    and `C-y` inserts `from-emacs`, not `later`.  It keeps doing so.
>
> Expected: either the claim in step 1 takes effect (step 3 prints
> `from-emacs`), or Emacs knows it failed and step 5 yanks `later`.
>
> A scripted version on Xvfb gave this with GNU Emacs 30.2 (X11, Lucid;
> build details below), `emacs -Q`.  The script presses a key in the Emacs
> frame with xdotool, claims with xclip, then runs the Emacs side through
> emacsclient:
>
>     == kill-new from a timer
>       xclip -o: other2
>       emacs:    (:owner-p t :yank "from-timer" :C-y "from-timer")
>     == another program copies afterwards
>       xclip -o: later
>       emacs:    (:owner-p t :C-y "from-timer")
>
> I hit it in daily use with a 31.0.50 GTK3 build on Xwayland.
> `x_own_selection` on master (0ed4fc4646, 2026-10-22) has the same code.
>
> Cause
>
> `x_own_selection` (src/xselect.c) calls `XSetSelectionOwner` with
> `dpyinfo->last_user_time` when it is given no timestamp, then adds the
> selection to `Vselection_alist` unconditionally.  The server ignores a
> SetSelectionOwner whose time is earlier than the selection's last-change
> time.  ICCCM 2.1, "Acquiring Selection Ownership", requires this check:
>
>     SetSelectionOwner(selection=PRIMARY, owner=Window, time=timestamp)
>     owner = GetSelectionOwner(selection=PRIMARY)
>     if (owner != Window) Failure
>
> `x-get-selection-internal` consults the local value first, and
> `x-selection-owner-p` and `x-selection-exists-p` both return t, so nothing
> in Emacs can tell that it does not own the selection.
>
> The same false belief lets Emacs clear a selection it never owned:
> `x-disown-selection-internal`, called after a fresh input event (for
> example `M-: (x-disown-selection-internal 'CLIPBOARD) RET` after step 4),
> sends SetSelectionOwner(None) with that event's time.  The server accepts
> it whoever the owner is, and the other program's `later` is gone
> (`xclip -o` then reports `Error: target STRING not available`).
>
> How it happens in practice
>
> The claim needs a timestamp older than another client's claim.  That
> happens without any timer:
>
> - Any claim made without a fresh input event: `kill-new` from timers,
>   from process filters (OSC 52 handlers in terminal emulators such as
>   vterm), or from emacsclient.
> - Mouse drags.  Under XInput 2, `last_user_time` is updated on
>   XI_ButtonPress but not on XI_ButtonRelease, so a selection claimed at the
>   end of a drag carries the press time.
> - Xwayland under cosmic-comp (the COSMIC desktop).  A selection made in a
>   Wayland client while no X window has keyboard focus is held back and
>   handed to X when an X window next gains focus
>   (`xwayland_notify_focus_change` in cosmic-comp's src/xwayland.rs), and
>   Smithay's X window manager then claims it with CurrentTime.  The click
>   that focuses Emacs can therefore carry an earlier time than that claim.
>   Here a drag in a vterm running tmux ends with an OSC 52 copy that
>   `kill-new`s with the press time, and the claim is ignored.  In about 90
>   minutes of logging, the compositor handed selections to X 32 times, each
>   within 50 ms of keyboard focus moving from a Wayland window into an X
>   window.
>
> Possible fix
>
> Check ownership after `XSetSelectionOwner` in `x_own_selection`.  On
> failure, either signal an error without recording the selection, or, when
> the timestamp came from `last_user_time` rather than from the caller, retry
> once with a fresh server timestamp.  `x_get_server_time` in xterm.c,
> currently static, gets one from a PropertyNotify, much as ICCCM 2.1
> suggests, and a server timestamp is never earlier than the last-change
> time.  ICCCM asks for the triggering event's time, but a claim from Lisp
> often has no triggering event, and `last_user_time` only stands in for
> one.
>
> The workaround I use: before claiming a selection Emacs does not own, ask
> the current owner for TIMESTAMP (which owners must support, ICCCM 2.6.2)
> and raise `last_user_time` to it:
>
>     (advice-add
>      'x-own-selection-internal :before
>      (lambda (selection _value &optional frame)
>        (when (and (not (x-selection-owner-p selection frame))
>                   (x-selection-exists-p selection frame))
>          (let* ((x-selection-timeout 1000)
>                 (time (x-get-selection-internal
>                        selection 'TIMESTAMP nil frame)))
>            (when (natnump time)
>              (x-display-set-last-user-time time frame))))))
>
> With `emacs -Q` 30.2 on Xvfb, this lets the claim through against an owner
> that answers TIMESTAMP.  It does nothing against xclip 0.13, which answers
> every target other than TARGETS with its text, so a TIMESTAMP request gets
> `"other"`.  A server timestamp from Emacs's side does not depend on the
> owner, which is one reason to fix this in `x_own_selection`.
>
> Related, minor
>
> Emacs tracks foreign selection owners for `x-selection-exists-p`
> (`x_find_selection_owner`) with XFixes, but `x_term_init` selects only
> SetSelectionOwnerNotify and SelectionClientCloseNotify, not
> SelectionWindowDestroyNotify.  After an owner destroys its window while
> staying connected, `x-selection-exists-p` keeps returning t although the
> selection has no owner.  I hit this while testing the workaround above;
> it may deserve its own report.
>
> Build used for the recipe output
>
> In GNU Emacs 30.2 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
> version 1.18.4, Xaw3d scroll bars)
> System Description: Ubuntu 24.04.4 LTS
>
> Configured using:
>  'configure
>  --prefix=/nix/store/ghk7vc0l1baxsy0mjcswz2spdw8zgi0l-emacs-30.2
>  --disable-build-details --with-modules --with-x-toolkit=lucid
>  --with-cairo --without-xft --disable-gc-mark-trace
>  --with-compress-install --with-toolkit-scroll-bars
>  --with-native-compilation --without-imagemagick --with-mailutils
>  --without-small-ja-dic --with-tree-sitter --with-xinput2
>  --without-xwidgets --with-dbus --with-selinux'
>
>

--000000000000fc7417065e77b741
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<html><body><div dir=3D"auto">This report was sent by mistake and is untest=
ed. I am still verifying it and will follow up.<br></div></body></html><br>=
<div class=3D"gmail_extra"><div>On Thu, Oct 22, 2026 08:57 PM, Xinyang Chen=
 &lt;<a href=3D"mailto:chenxinyang99@HIDDEN">chenxinyang99@HIDDEN</a>=
&gt; wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex=
;border-left:1px #ccc solid;padding-left:1ex"><pre>On X, if another client =
takes a selection after Emacs&#39;s last input event,
and Emacs then claims that selection, the X server ignores Emacs&#39;s
SetSelectionOwner.  Emacs records itself as the owner anyway.  From then on
it answers every request for that selection, its own yanks included, from
its local copy.  Having never been the owner, it never gets a
SelectionClear, so text copied in other programs stays invisible to Emacs
until Emacs next claims the selection with a newer timestamp.

Recipe

X11 build (not PGTK), `emacs -Q`, `xclip` in a terminal:

1. `M-: (run-with-timer 5 nil (lambda () (kill-new &quot;from-emacs&quot;))=
) RET`
2. Switch to the terminal and, within the 5 seconds, run
   `printf other | xclip -selection clipboard`.  Stay there until the
   timer has run.
3. `xclip -o -selection clipboard` prints `other`: Emacs&#39;s claim did no=
t
   take.
4. `printf later | xclip -selection clipboard`
5. Back in Emacs, `M-: (x-selection-owner-p &#39;CLIPBOARD) RET` gives `t`,
   and `C-y` inserts `from-emacs`, not `later`.  It keeps doing so.

Expected: either the claim in step 1 takes effect (step 3 prints
`from-emacs`), or Emacs knows it failed and step 5 yanks `later`.

A scripted version on Xvfb gave this with GNU Emacs 30.2 (X11, Lucid;
build details below), `emacs -Q`.  The script presses a key in the Emacs
frame with xdotool, claims with xclip, then runs the Emacs side through
emacsclient:

    =3D=3D kill-new from a timer
      xclip -o: other2
      emacs:    (:owner-p t :yank &quot;from-timer&quot; :C-y &quot;from-ti=
mer&quot;)
    =3D=3D another program copies afterwards
      xclip -o: later
      emacs:    (:owner-p t :C-y &quot;from-timer&quot;)

I hit it in daily use with a 31.0.50 GTK3 build on Xwayland.
`x_own_selection` on master (0ed4fc4646, 2026-10-22) has the same code.

Cause

`x_own_selection` (src/xselect.c) calls `XSetSelectionOwner` with
`dpyinfo-&gt;last_user_time` when it is given no timestamp, then adds the
selection to `Vselection_alist` unconditionally.  The server ignores a
SetSelectionOwner whose time is earlier than the selection&#39;s last-chang=
e
time.  ICCCM 2.1, &quot;Acquiring Selection Ownership&quot;, requires this =
check:

    SetSelectionOwner(selection=3D<wbr>PRIMARY, owner=3DWindow, time=3Dtime=
stamp)
    owner =3D GetSelectionOwner(selection=3D<wbr>PRIMARY)
    if (owner !=3D Window) Failure

`x-get-selection-internal` consults the local value first, and
`x-selection-owner-p` and `x-selection-exists-p` both return t, so nothing
in Emacs can tell that it does not own the selection.

The same false belief lets Emacs clear a selection it never owned:
`x-disown-selection-internal`, called after a fresh input event (for
example `M-: (x-disown-selection-internal &#39;CLIPBOARD) RET` after step 4=
),
sends SetSelectionOwner(None) with that event&#39;s time.  The server accep=
ts
it whoever the owner is, and the other program&#39;s `later` is gone
(`xclip -o` then reports `Error: target STRING not available`).

How it happens in practice

The claim needs a timestamp older than another client&#39;s claim.  That
happens without any timer:

- Any claim made without a fresh input event: `kill-new` from timers,
  from process filters (OSC 52 handlers in terminal emulators such as
  vterm), or from emacsclient.
- Mouse drags.  Under XInput 2, `last_user_time` is updated on
  XI_ButtonPress but not on XI_ButtonRelease, so a selection claimed at the
  end of a drag carries the press time.
- Xwayland under cosmic-comp (the COSMIC desktop).  A selection made in a
  Wayland client while no X window has keyboard focus is held back and
  handed to X when an X window next gains focus
  (`xwayland_notify_focus_<wbr>change` in cosmic-comp&#39;s src/xwayland.rs=
), and
  Smithay&#39;s X window manager then claims it with CurrentTime.  The clic=
k
  that focuses Emacs can therefore carry an earlier time than that claim.
  Here a drag in a vterm running tmux ends with an OSC 52 copy that
  `kill-new`s with the press time, and the claim is ignored.  In about 90
  minutes of logging, the compositor handed selections to X 32 times, each
  within 50 ms of keyboard focus moving from a Wayland window into an X
  window.

Possible fix

Check ownership after `XSetSelectionOwner` in `x_own_selection`.  On
failure, either signal an error without recording the selection, or, when
the timestamp came from `last_user_time` rather than from the caller, retry
once with a fresh server timestamp.  `x_get_server_time` in xterm.c,
currently static, gets one from a PropertyNotify, much as ICCCM 2.1
suggests, and a server timestamp is never earlier than the last-change
time.  ICCCM asks for the triggering event&#39;s time, but a claim from Lis=
p
often has no triggering event, and `last_user_time` only stands in for
one.

The workaround I use: before claiming a selection Emacs does not own, ask
the current owner for TIMESTAMP (which owners must support, ICCCM 2.6.2)
and raise `last_user_time` to it:

    (advice-add
     &#39;x-own-selection-internal :before
     (lambda (selection _value &amp;optional frame)
       (when (and (not (x-selection-owner-p selection frame))
                  (x-selection-exists-p selection frame))
         (let* ((x-selection-timeout 1000)
                (time (x-get-selection-internal
                       selection &#39;TIMESTAMP nil frame)))
           (when (natnump time)
             (x-display-set-last-user-time time frame))))))

With `emacs -Q` 30.2 on Xvfb, this lets the claim through against an owner
that answers TIMESTAMP.  It does nothing against xclip 0.13, which answers
every target other than TARGETS with its text, so a TIMESTAMP request gets
`&quot;other&quot;`.  A server timestamp from Emacs&#39;s side does not dep=
end on the
owner, which is one reason to fix this in `x_own_selection`.

Related, minor

Emacs tracks foreign selection owners for `x-selection-exists-p`
(`x_find_selection_owner`) with XFixes, but `x_term_init` selects only
SetSelectionOwnerNotify and SelectionClientCloseNotify, not
SelectionWindowDestroyNotify.  After an owner destroys its window while
staying connected, `x-selection-exists-p` keeps returning t although the
selection has no owner.  I hit this while testing the workaround above;
it may deserve its own report.

Build used for the recipe output

In GNU Emacs 30.2 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
version 1.18.4, Xaw3d scroll bars)
System Description: Ubuntu 24.04.4 LTS

Configured using:
 &#39;configure
 --prefix=3D/nix/store/<wbr>ghk7vc0l1baxsy0mjcswz2spdw8zgi<wbr>0l-emacs-30.=
2
 --disable-build-details --with-modules --with-x-toolkit=3Dlucid
 --with-cairo --without-xft --disable-gc-mark-trace
 --with-compress-install --with-toolkit-scroll-bars
 --with-native-compilation --without-imagemagick --with-mailutils
 --without-small-ja-dic --with-tree-sitter --with-xinput2
 --without-xwidgets --with-dbus --with-selinux&#39;
</pre></blockquote></div></div>

--000000000000fc7417065e77b741--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#82091; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 23 Oct 2026 00:58:14 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Oct 22 20:58:13 2026
Received: from localhost ([127.0.0.1]:52787 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1xK3bi-0004br-Hc
	for submit <at> debbugs.gnu.org; Thu, 22 Oct 2026 20:58:13 -0400
Received: from lists1p.gnu.org ([2001:470:142::17]:49530)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <chenxinyang99@HIDDEN>)
 id 1xK3bZ-0004Zm-LR
 for submit <at> debbugs.gnu.org; Thu, 22 Oct 2026 20:58:04 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <chenxinyang99@HIDDEN>)
 id 1xK3bO-0006Fz-GU
 for bug-gnu-emacs@HIDDEN; Thu, 22 Oct 2026 20:57:46 -0400
Received: from mail-yw1-x1136.google.com ([2607:f8b0:4864:20::1136])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <chenxinyang99@HIDDEN>)
 id 1xK3bK-0001Ux-Lm
 for bug-gnu-emacs@HIDDEN; Thu, 22 Oct 2026 20:57:46 -0400
Received: by mail-yw1-x1136.google.com with SMTP id
 00721157ae682-8b10ac6ab7bso4648987b3.1
 for <bug-gnu-emacs@HIDDEN>; Thu, 22 Oct 2026 17:57:39 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1792717059; cv=none;
 d=google.com; s=arc-20260327;
 b=DC6ESjSpCXYnbNU2xW6xyi1jKEaae88j8G4CKqTN+ZwxZqrMh+4Ur/iwh768O/z380
 kHFNwDQzXVFVDR3MsdhLw1uKbP4STjOl5GBRVxyWOV9ZHpG4wH8O6T1cShK7xa4TLvO+
 0h50PtKEFr+TCmPzDa4/B3G9CQQw5OwgU2ljf4mCtbPkpKMCUFNxjWUzPLv42AkGZ9T6
 2oLaaR8jt4f5Ybyx/oQiwirBUr7kNGivzQik9rU8WyUdqAuqWOEafw+Ier6klb+TT5Zz
 yIq4uy8W0FxX/dz1FGwF3goQ4bT4F3uUYxdv7ks4htE80NBy8wXD8MJ92jTMsFVXBieM
 QmXA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com;
 s=arc-20260327; 
 h=to:subject:message-id:date:from:mime-version:dkim-signature;
 bh=F+So56W0H5BZ//fduTjcV41GoG3ScLDcWZaPHRln7cI=;
 fh=+R8aXOFVnuBbN1nRXJJI02Ia//JUQJmLW/Uk/VaA0ag=;
 b=Yme1oHzBaLIH1MrvrwZBiZocSL/2Ecllj20+t8XgJiSAgIvRzuNHCsI2dCFnVm6tb7
 dLPZYoHijdipM/TUqv84HzoMITDGXSUQMK8s7eYaxA2Ycsna+C72hGP6zLoPnPBHbL8j
 rSAJEbhvXFJwJwUtV5V82kWGZRbG/Te8+UWjg4gDtC1vWzThU+ihYeXkgyxRn+tA9sBV
 s/T38pNotkbxybiaYgK66mpVulCWtFes3XrbclNVr8pZ3qcjN0WKDEiL2qjViu0+7/Yw
 Vl5u2IsmuuTAtXjMsb2TLdOyJ1X5YjFtNwlQwjpoVEbFUeHiVstHJ+vf2Up2eIkrwPW7
 lahQ==; darn=gnu.org
ARC-Authentication-Results: i=1; mx.google.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20251104; t=1792717059; x=1793321859; darn=gnu.org;
 h=content-type:to:subject:message-id:date:from:mime-version:from:to
 :cc:subject:date:message-id:reply-to:content-type;
 bh=F+So56W0H5BZ//fduTjcV41GoG3ScLDcWZaPHRln7cI=;
 b=afKz3hU+N67a9hCxvYzyS396/5jCeWyFRh59JZP1HggBDhc0v1OwFT4GS3HdU+AdX8
 CPTirQZgRJ8kSO/SuI71RKfuhCKN6WUys6sHpkA76MWBYv3yn/NEvCvAPoZqws2cPjbW
 aOhxu2Nd2rGE9Mvh5+dRMJVc6c0Uc/BJSHXgWRzsP6Nr+Rx29IFC+Bc2utiTwPm+3HD2
 9hgB0bGPqm5FRG292Ud88dF5DuNZxPbyNTyPZFKJaZBOMil8TpMRafjhe00HX5kWmU9V
 pSd4ipUaTd8VyG1VJik6XWwdbd7jnY7JueHZKh9VuQpoWjnSoxymMHDztp8h5JFqsoHO
 FmWw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20260707; t=1792717059; x=1793321859;
 h=content-type:to:subject:message-id:date:from:mime-version:x-gm-gg
 :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
 :content-type;
 bh=F+So56W0H5BZ//fduTjcV41GoG3ScLDcWZaPHRln7cI=;
 b=Mza6WYJ0waj4KKZr0uASiMIhRP8W8Nrn5YlSlN93xrM0VHJ3q1Xr01DbS9PGPJa6Zb
 4IDY+1H1JGFqRtYGJ0mgRKesksue2xoZpUy/ue113orKIdum6SE4CnhQjTOtC66DNF7h
 ERcdWQh6J3u82hmclTOiDfv3FcWeWpIyX8Jv/nnatfymQxZm5TWOE+mistStn1Wzxn2F
 mO3yVkYg5ZaulpLQEoT/Kx+/mSEhNbamlJwewExtZwOvCDzB41gWRmg29m7Jnp2O2a/6
 zi6fo2vPSMq50lBTEQ7CyREaP1XhePi1nDmTkwpw19l8uFXOKp/1L2RvR1TTcz/z89R0
 M77w==
X-Gm-Message-State: AFq9FYLhDjjBQzf+ZNDmygD+Km27V9LWDkpVme7bQRP8hyPdzI+EKDUa
 uc1V7tRfD5cCnbNsJvlm/xN59IGHOii1XVPCJFlXwBn20wjppu7CGapMPXTgZ222F/PZ6pcaSpy
 MBR5urTRIfVpOSuJJQe0I524PjN2+xlEiNw==
X-Gm-Gg: AYBFou3KaMFwbns3mIVjD7mkUoIwz8/h3FBAkh9cWwGhT27jyG638j3I2FUe6XV4EpX
 wK/cmNMerzQpNGYUBhVPbQnyNlI0XVbki0MPjQje7Mx8w5gNjwkspOJckpYrtqL6qSf8es8Ata/
 jmL5jq7PG+gT7gzJabwiHnDZ24YxYgSmvsC5FB0OELQwdT5PrsmdTvvpHRj0asa95tmNHcfKNr0
 HDUEoI6dvtRnaoiW3eFGKw3M7cgkXMesE/BUdZuY5t7RLmLcwXaeUpExHPTxF4o8gpQTmmdqIFe
 I+D9Xo/ojSLmy9SdyoIyDYjlj75kIO+u1VdzJU9HRxdNrSJUOhZK5LW0yVVuo5HpvzTig5t6KXO
 kDdfFA6fVM/VWO77y58JE6jir43neKheTMGnwIo2GAalZLTzi5BaTqBX21UuUtFAz6hui3Md6pN
 W4FVhMaZZ8LKAEe3mjl1Luig==
X-Received: by 2002:a05:690c:e148:10b0:8b0:cd30:ce7e with SMTP id
 00721157ae682-8b157e6a042mr3673447b3.52.1792717058915; Thu, 22 Oct 2026
 17:57:38 -0700 (PDT)
Received: from 101988054943 named unknown by gmailapi.google.com with
 HTTPREST; Thu, 22 Oct 2026 17:57:37 -0700
Received: from 101988054943 named unknown by gmailapi.google.com with
 HTTPREST; Thu, 22 Oct 2026 20:55:14 -0400
MIME-Version: 1.0
From: Xinyang Chen <chenxinyang99@HIDDEN>
Date: Thu, 22 Oct 2026 17:57:37 -0700
X-Gm-Features: AclHuK9n7v82I8J5ufUgs-0IaI4M5RLBwn1E2pGi0cuc4Z5iy_AYZ6AEdeSlasQ
Message-ID: <CANnQ9XZDKpTct28_-qRR3MgoyboKSNt1jivkCoO6S2ppSwWRoA@HIDDEN>
Subject: X: x-own-selection-internal records ownership the server ignored;
 Emacs then serves a stale selection
To: bug-gnu-emacs@HIDDEN
Content-Type: multipart/alternative; boundary="0000000000007abfe1065e777759"
Received-SPF: pass client-ip=2607:f8b0:4864:20::1136;
 envelope-from=chenxinyang99@HIDDEN; helo=mail-yw1-x1136.google.com
X-Spam_score_int: -17
X-Spam_score: -1.8
X-Spam_bar: -
X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 2.2 (++)
X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org",
 has NOT identified this incoming email as spam.  The original
 message has been attached to this so you can view it or label
 similar future email.  If you have any questions, see
 the administrator of that system for details.
 Content preview:  On X, if another client takes a selection after Emacs's last
 input event, and Emacs then claims that selection, the X server ignores Emacs's
 SetSelectionOwner. Emacs records itself as the owner anyway [...] 
 Content analysis details:   (2.2 points, 10.0 required)
 pts rule name              description
 ---- ---------------------- --------------------------------------------------
 -0.0 RCVD_IN_DNSWL_NONE     RBL: Sender listed at https://www.dnswl.org/,
 no trust [2001:470:142:0:0:0:0:17 listed in] [list.dnswl.org]
 1.0 SPF_SOFTFAIL           SPF: sender does not match SPF record (softfail)
 1.0 FORGED_GMAIL_RCVD      'From' gmail.com does not match 'Received'
 headers -0.0 SPF_HELO_PASS          SPF: HELO matches SPF record
 0.0 FREEMAIL_FROM          Sender email is commonly abused enduser mail
 provider (chenxinyang99[at]gmail.com)
 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends
 in digit (chenxinyang99[at]gmail.com)
 0.0 HTML_MESSAGE           BODY: HTML included in message
 0.0 UNPARSEABLE_RELAY      Informational: message has unparseable relay
 lines
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: 1.2 (+)
X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org",
 has NOT identified this incoming email as spam.  The original
 message has been attached to this so you can view it or label
 similar future email.  If you have any questions, see
 the administrator of that system for details.
 
 Content preview:  On X, if another client takes a selection after Emacs's last
    input event, and Emacs then claims that selection, the X server ignores Emacs's
    SetSelectionOwner. Emacs records itself as the owner anyway [...] 
 
 Content analysis details:   (1.2 points, 10.0 required)
 
  pts rule name              description
 ---- ---------------------- --------------------------------------------------
 -0.0 RCVD_IN_DNSWL_NONE     RBL: Sender listed at https://www.dnswl.org/,
                              no trust
                             [2001:470:142:0:0:0:0:17 listed in]
                             [list.dnswl.org]
  1.0 SPF_SOFTFAIL           SPF: sender does not match SPF record (softfail)
  1.0 FORGED_GMAIL_RCVD      'From' gmail.com does not match 'Received'
                             headers
 -0.0 SPF_HELO_PASS          SPF: HELO matches SPF record
  0.0 FREEMAIL_FROM          Sender email is commonly abused enduser mail
                             provider (chenxinyang99[at]gmail.com)
  0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends
                             in digit (chenxinyang99[at]gmail.com)
  0.0 HTML_MESSAGE           BODY: HTML included in message
  0.0 UNPARSEABLE_RELAY      Informational: message has unparseable relay
                             lines
 -1.0 MAILING_LIST_MULTI     Multiple indicators imply a widely-seen list
                             manager

--0000000000007abfe1065e777759
Content-Type: text/plain; charset="UTF-8"

On X, if another client takes a selection after Emacs's last input event,
and Emacs then claims that selection, the X server ignores Emacs's
SetSelectionOwner.  Emacs records itself as the owner anyway.  From then on
it answers every request for that selection, its own yanks included, from
its local copy.  Having never been the owner, it never gets a
SelectionClear, so text copied in other programs stays invisible to Emacs
until Emacs next claims the selection with a newer timestamp.

Recipe

X11 build (not PGTK), `emacs -Q`, `xclip` in a terminal:

1. `M-: (run-with-timer 5 nil (lambda () (kill-new "from-emacs"))) RET`
2. Switch to the terminal and, within the 5 seconds, run
   `printf other | xclip -selection clipboard`.  Stay there until the
   timer has run.
3. `xclip -o -selection clipboard` prints `other`: Emacs's claim did not
   take.
4. `printf later | xclip -selection clipboard`
5. Back in Emacs, `M-: (x-selection-owner-p 'CLIPBOARD) RET` gives `t`,
   and `C-y` inserts `from-emacs`, not `later`.  It keeps doing so.

Expected: either the claim in step 1 takes effect (step 3 prints
`from-emacs`), or Emacs knows it failed and step 5 yanks `later`.

A scripted version on Xvfb gave this with GNU Emacs 30.2 (X11, Lucid;
build details below), `emacs -Q`.  The script presses a key in the Emacs
frame with xdotool, claims with xclip, then runs the Emacs side through
emacsclient:

    == kill-new from a timer
      xclip -o: other2
      emacs:    (:owner-p t :yank "from-timer" :C-y "from-timer")
    == another program copies afterwards
      xclip -o: later
      emacs:    (:owner-p t :C-y "from-timer")

I hit it in daily use with a 31.0.50 GTK3 build on Xwayland.
`x_own_selection` on master (0ed4fc4646, 2026-10-22) has the same code.

Cause

`x_own_selection` (src/xselect.c) calls `XSetSelectionOwner` with
`dpyinfo->last_user_time` when it is given no timestamp, then adds the
selection to `Vselection_alist` unconditionally.  The server ignores a
SetSelectionOwner whose time is earlier than the selection's last-change
time.  ICCCM 2.1, "Acquiring Selection Ownership", requires this check:

    SetSelectionOwner(selection=PRIMARY, owner=Window, time=timestamp)
    owner = GetSelectionOwner(selection=PRIMARY)
    if (owner != Window) Failure

`x-get-selection-internal` consults the local value first, and
`x-selection-owner-p` and `x-selection-exists-p` both return t, so nothing
in Emacs can tell that it does not own the selection.

The same false belief lets Emacs clear a selection it never owned:
`x-disown-selection-internal`, called after a fresh input event (for
example `M-: (x-disown-selection-internal 'CLIPBOARD) RET` after step 4),
sends SetSelectionOwner(None) with that event's time.  The server accepts
it whoever the owner is, and the other program's `later` is gone
(`xclip -o` then reports `Error: target STRING not available`).

How it happens in practice

The claim needs a timestamp older than another client's claim.  That
happens without any timer:

- Any claim made without a fresh input event: `kill-new` from timers,
  from process filters (OSC 52 handlers in terminal emulators such as
  vterm), or from emacsclient.
- Mouse drags.  Under XInput 2, `last_user_time` is updated on
  XI_ButtonPress but not on XI_ButtonRelease, so a selection claimed at the
  end of a drag carries the press time.
- Xwayland under cosmic-comp (the COSMIC desktop).  A selection made in a
  Wayland client while no X window has keyboard focus is held back and
  handed to X when an X window next gains focus
  (`xwayland_notify_focus_change` in cosmic-comp's src/xwayland.rs), and
  Smithay's X window manager then claims it with CurrentTime.  The click
  that focuses Emacs can therefore carry an earlier time than that claim.
  Here a drag in a vterm running tmux ends with an OSC 52 copy that
  `kill-new`s with the press time, and the claim is ignored.  In about 90
  minutes of logging, the compositor handed selections to X 32 times, each
  within 50 ms of keyboard focus moving from a Wayland window into an X
  window.

Possible fix

Check ownership after `XSetSelectionOwner` in `x_own_selection`.  On
failure, either signal an error without recording the selection, or, when
the timestamp came from `last_user_time` rather than from the caller, retry
once with a fresh server timestamp.  `x_get_server_time` in xterm.c,
currently static, gets one from a PropertyNotify, much as ICCCM 2.1
suggests, and a server timestamp is never earlier than the last-change
time.  ICCCM asks for the triggering event's time, but a claim from Lisp
often has no triggering event, and `last_user_time` only stands in for
one.

The workaround I use: before claiming a selection Emacs does not own, ask
the current owner for TIMESTAMP (which owners must support, ICCCM 2.6.2)
and raise `last_user_time` to it:

    (advice-add
     'x-own-selection-internal :before
     (lambda (selection _value &optional frame)
       (when (and (not (x-selection-owner-p selection frame))
                  (x-selection-exists-p selection frame))
         (let* ((x-selection-timeout 1000)
                (time (x-get-selection-internal
                       selection 'TIMESTAMP nil frame)))
           (when (natnump time)
             (x-display-set-last-user-time time frame))))))

With `emacs -Q` 30.2 on Xvfb, this lets the claim through against an owner
that answers TIMESTAMP.  It does nothing against xclip 0.13, which answers
every target other than TARGETS with its text, so a TIMESTAMP request gets
`"other"`.  A server timestamp from Emacs's side does not depend on the
owner, which is one reason to fix this in `x_own_selection`.

Related, minor

Emacs tracks foreign selection owners for `x-selection-exists-p`
(`x_find_selection_owner`) with XFixes, but `x_term_init` selects only
SetSelectionOwnerNotify and SelectionClientCloseNotify, not
SelectionWindowDestroyNotify.  After an owner destroys its window while
staying connected, `x-selection-exists-p` keeps returning t although the
selection has no owner.  I hit this while testing the workaround above;
it may deserve its own report.

Build used for the recipe output

In GNU Emacs 30.2 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
version 1.18.4, Xaw3d scroll bars)
System Description: Ubuntu 24.04.4 LTS

Configured using:
 'configure
 --prefix=/nix/store/ghk7vc0l1baxsy0mjcswz2spdw8zgi0l-emacs-30.2
 --disable-build-details --with-modules --with-x-toolkit=lucid
 --with-cairo --without-xft --disable-gc-mark-trace
 --with-compress-install --with-toolkit-scroll-bars
 --with-native-compilation --without-imagemagick --with-mailutils
 --without-small-ja-dic --with-tree-sitter --with-xinput2
 --without-xwidgets --with-dbus --with-selinux'

--0000000000007abfe1065e777759
Content-Type: text/html; charset="UTF-8"

<pre>On X, if another client takes a selection after Emacs&#39;s last input event,
and Emacs then claims that selection, the X server ignores Emacs&#39;s
SetSelectionOwner.  Emacs records itself as the owner anyway.  From then on
it answers every request for that selection, its own yanks included, from
its local copy.  Having never been the owner, it never gets a
SelectionClear, so text copied in other programs stays invisible to Emacs
until Emacs next claims the selection with a newer timestamp.

Recipe

X11 build (not PGTK), `emacs -Q`, `xclip` in a terminal:

1. `M-: (run-with-timer 5 nil (lambda () (kill-new &quot;from-emacs&quot;))) RET`
2. Switch to the terminal and, within the 5 seconds, run
   `printf other | xclip -selection clipboard`.  Stay there until the
   timer has run.
3. `xclip -o -selection clipboard` prints `other`: Emacs&#39;s claim did not
   take.
4. `printf later | xclip -selection clipboard`
5. Back in Emacs, `M-: (x-selection-owner-p &#39;CLIPBOARD) RET` gives `t`,
   and `C-y` inserts `from-emacs`, not `later`.  It keeps doing so.

Expected: either the claim in step 1 takes effect (step 3 prints
`from-emacs`), or Emacs knows it failed and step 5 yanks `later`.

A scripted version on Xvfb gave this with GNU Emacs 30.2 (X11, Lucid;
build details below), `emacs -Q`.  The script presses a key in the Emacs
frame with xdotool, claims with xclip, then runs the Emacs side through
emacsclient:

    == kill-new from a timer
      xclip -o: other2
      emacs:    (:owner-p t :yank &quot;from-timer&quot; :C-y &quot;from-timer&quot;)
    == another program copies afterwards
      xclip -o: later
      emacs:    (:owner-p t :C-y &quot;from-timer&quot;)

I hit it in daily use with a 31.0.50 GTK3 build on Xwayland.
`x_own_selection` on master (0ed4fc4646, 2026-10-22) has the same code.

Cause

`x_own_selection` (src/xselect.c) calls `XSetSelectionOwner` with
`dpyinfo-&gt;last_user_time` when it is given no timestamp, then adds the
selection to `Vselection_alist` unconditionally.  The server ignores a
SetSelectionOwner whose time is earlier than the selection&#39;s last-change
time.  ICCCM 2.1, &quot;Acquiring Selection Ownership&quot;, requires this check:

    SetSelectionOwner(selection=PRIMARY, owner=Window, time=timestamp)
    owner = GetSelectionOwner(selection=PRIMARY)
    if (owner != Window) Failure

`x-get-selection-internal` consults the local value first, and
`x-selection-owner-p` and `x-selection-exists-p` both return t, so nothing
in Emacs can tell that it does not own the selection.

The same false belief lets Emacs clear a selection it never owned:
`x-disown-selection-internal`, called after a fresh input event (for
example `M-: (x-disown-selection-internal &#39;CLIPBOARD) RET` after step 4),
sends SetSelectionOwner(None) with that event&#39;s time.  The server accepts
it whoever the owner is, and the other program&#39;s `later` is gone
(`xclip -o` then reports `Error: target STRING not available`).

How it happens in practice

The claim needs a timestamp older than another client&#39;s claim.  That
happens without any timer:

- Any claim made without a fresh input event: `kill-new` from timers,
  from process filters (OSC 52 handlers in terminal emulators such as
  vterm), or from emacsclient.
- Mouse drags.  Under XInput 2, `last_user_time` is updated on
  XI_ButtonPress but not on XI_ButtonRelease, so a selection claimed at the
  end of a drag carries the press time.
- Xwayland under cosmic-comp (the COSMIC desktop).  A selection made in a
  Wayland client while no X window has keyboard focus is held back and
  handed to X when an X window next gains focus
  (`xwayland_notify_focus_change` in cosmic-comp&#39;s src/xwayland.rs), and
  Smithay&#39;s X window manager then claims it with CurrentTime.  The click
  that focuses Emacs can therefore carry an earlier time than that claim.
  Here a drag in a vterm running tmux ends with an OSC 52 copy that
  `kill-new`s with the press time, and the claim is ignored.  In about 90
  minutes of logging, the compositor handed selections to X 32 times, each
  within 50 ms of keyboard focus moving from a Wayland window into an X
  window.

Possible fix

Check ownership after `XSetSelectionOwner` in `x_own_selection`.  On
failure, either signal an error without recording the selection, or, when
the timestamp came from `last_user_time` rather than from the caller, retry
once with a fresh server timestamp.  `x_get_server_time` in xterm.c,
currently static, gets one from a PropertyNotify, much as ICCCM 2.1
suggests, and a server timestamp is never earlier than the last-change
time.  ICCCM asks for the triggering event&#39;s time, but a claim from Lisp
often has no triggering event, and `last_user_time` only stands in for
one.

The workaround I use: before claiming a selection Emacs does not own, ask
the current owner for TIMESTAMP (which owners must support, ICCCM 2.6.2)
and raise `last_user_time` to it:

    (advice-add
     &#39;x-own-selection-internal :before
     (lambda (selection _value &amp;optional frame)
       (when (and (not (x-selection-owner-p selection frame))
                  (x-selection-exists-p selection frame))
         (let* ((x-selection-timeout 1000)
                (time (x-get-selection-internal
                       selection &#39;TIMESTAMP nil frame)))
           (when (natnump time)
             (x-display-set-last-user-time time frame))))))

With `emacs -Q` 30.2 on Xvfb, this lets the claim through against an owner
that answers TIMESTAMP.  It does nothing against xclip 0.13, which answers
every target other than TARGETS with its text, so a TIMESTAMP request gets
`&quot;other&quot;`.  A server timestamp from Emacs&#39;s side does not depend on the
owner, which is one reason to fix this in `x_own_selection`.

Related, minor

Emacs tracks foreign selection owners for `x-selection-exists-p`
(`x_find_selection_owner`) with XFixes, but `x_term_init` selects only
SetSelectionOwnerNotify and SelectionClientCloseNotify, not
SelectionWindowDestroyNotify.  After an owner destroys its window while
staying connected, `x-selection-exists-p` keeps returning t although the
selection has no owner.  I hit this while testing the workaround above;
it may deserve its own report.

Build used for the recipe output

In GNU Emacs 30.2 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo
version 1.18.4, Xaw3d scroll bars)
System Description: Ubuntu 24.04.4 LTS

Configured using:
 &#39;configure
 --prefix=/nix/store/ghk7vc0l1baxsy0mjcswz2spdw8zgi0l-emacs-30.2
 --disable-build-details --with-modules --with-x-toolkit=lucid
 --with-cairo --without-xft --disable-gc-mark-trace
 --with-compress-install --with-toolkit-scroll-bars
 --with-native-compilation --without-imagemagick --with-mailutils
 --without-small-ja-dic --with-tree-sitter --with-xinput2
 --without-xwidgets --with-dbus --with-selinux&#39;
</pre>

--0000000000007abfe1065e777759--




Acknowledgement sent to Xinyang Chen <chenxinyang99@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#82091; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Sat, 7 Nov 2026 10:45:03 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.