Linux: Fix updating an OSC 99 notification before it is created leaving the old notification on screen

Since the notification being replaced has no dbus id yet, it cannot be passed
as the replaces argument to Notify, so close it once the notification server
creates it.
This commit is contained in:
Kovid Goyal 2026-10-19 07:28:34 +05:30
parent 74d3477c9d
commit 47603c8a86
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 51 additions and 14 deletions

View file

@ -260,9 +260,9 @@ Detailed list of changes
to the server has silently died, for example, after a network change or
suspend. Now the kitten detects this and reconnects (:iss:`10691`)
- Linux: Desktop notifications: Fix a request to close a notification being
ignored if it arrives before the notification server has finished creating
the notification (:iss:`10695`)
- Linux: Desktop notifications: Fix a request to close or update a
notification being ignored if it arrives before the notification server has
finished creating the notification (:iss:`10695`)
- diff kitten: Fix comparing remote directories over SSH failing when they
contain read-only directories (:pull:`10676`)

View file

@ -714,16 +714,23 @@ class FreeDesktopIntegration(DesktopIntegration):
close_succeeded = dbus_close_notification(dbus_id)
if debug_desktop_integration:
log_error(f'Close request for {desktop_notification_id=} {"succeeded" if close_succeeded else "failed"}')
elif (n := self.notification_manager.in_progress_notification_commands.get(desktop_notification_id)) and not n.created_by_desktop:
# The notification daemon has not yet replied to Notify, close it once it does
if len(self.pending_closes) > 128:
self.pending_closes &= set(self.notification_manager.in_progress_notification_commands)
self.pending_closes.add(desktop_notification_id)
close_succeeded = True
if debug_desktop_integration:
log_error(f'Close request for {desktop_notification_id=} deferred till notification is created')
else:
close_succeeded = self.defer_close_till_created(desktop_notification_id)
return close_succeeded
def defer_close_till_created(self, desktop_notification_id: int) -> bool:
# If the notification daemon has not yet replied to Notify, close the
# notification once it does
n = self.notification_manager.in_progress_notification_commands.get(desktop_notification_id)
if n is None or n.created_by_desktop:
return False
if len(self.pending_closes) > 128:
self.pending_closes &= set(self.notification_manager.in_progress_notification_commands)
self.pending_closes.add(desktop_notification_id)
if debug_desktop_integration:
log_error(f'Close request for {desktop_notification_id=} deferred till notification is created')
return True
def get_desktop_notification_id(self, dbus_notification_id: int, event: str) -> int | None:
q = self.dbus_to_desktop.get(dbus_notification_id)
if q is None:
@ -816,6 +823,10 @@ class FreeDesktopIntegration(DesktopIntegration):
replaces_dbus_id = 0
if existing_desktop_notification_id:
replaces_dbus_id = self.get_dbus_notification_id(existing_desktop_notification_id, 'notify') or 0
if not replaces_dbus_id:
# The notification being replaced has not been created yet so
# it cannot be replaced, instead close it once it is created
self.defer_close_till_created(existing_desktop_notification_id)
actions = {'default': ''}
for i, b in enumerate(nc.buttons):
actions[str(i + 1)] = b

View file

@ -369,8 +369,10 @@ class TestNotifications(BaseTest):
sent: list[int] = []
closed: list[int] = []
replaces: list[object] = []
def send_notification(**kw: object) -> int:
replaces.append(kw['replaces'])
sent.append(len(sent) + 1)
return sent[-1]
@ -378,9 +380,15 @@ class TestNotifications(BaseTest):
closed.append(dbus_id)
return True
with tempfile.TemporaryDirectory() as tdir, patch.multiple(
fdt, create=True, dbus_set_notification_callback=lambda *a: None,
dbus_send_notification=send_notification, dbus_close_notification=close_notification,
with (
tempfile.TemporaryDirectory() as tdir,
patch.multiple(
fdt,
create=True,
dbus_set_notification_callback=lambda *a: None,
dbus_send_notification=send_notification,
dbus_close_notification=close_notification,
),
):
di = FreeDesktopIntegration(None)
ch = Channel()
@ -400,6 +408,24 @@ class TestNotifications(BaseTest):
self.ae(ch.responses, ['99;i=x:p=close;'])
self.ae(di.pending_closes, set())
# replacement sent before the notification being replaced is created
closed.clear(), ch.reset()
nm.handle_notification_cmd(1, 99, 'i=r;first')
first = sent[-1]
nm.handle_notification_cmd(1, 99, 'i=r;second')
second = sent[-1]
self.ae(nm.in_progress_notification_commands_by_client_id['r'].title, 'second')
di.dispatch_event_from_desktop('created', first, 201)
self.ae(closed, [201])
di.dispatch_event_from_desktop('closed', 201, 0)
di.dispatch_event_from_desktop('created', second, 202)
self.ae(closed, [201])
self.ae(nm.in_progress_notification_commands_by_client_id['r'].title, 'second')
self.ae(ch.responses, [])
nm.handle_notification_cmd(1, 99, 'i=r;third')
self.ae(closed, [201]) # replaced via the replaces argument to Notify, not closed
self.ae(replaces[-3:], [0, 0, 202])
# normal close after creation
closed.clear(), ch.reset()
nm.handle_notification_cmd(1, 99, 'i=y;title')