Linux: Fix OSC 99 close requests being dropped if they arrive before the notification is created

Fixes #10695
This commit is contained in:
Kovid Goyal 2026-10-19 07:27:08 +05:30
parent 4cb9765743
commit 74d3477c9d
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 76 additions and 5 deletions

View file

@ -260,6 +260,10 @@ 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`)
- diff kitten: Fix comparing remote directories over SSH failing when they
contain read-only directories (:pull:`10676`)

View file

@ -699,6 +699,9 @@ class FreeDesktopIntegration(DesktopIntegration):
# desktop_notification_id we use for the notification
self.dbus_to_desktop: 'OrderedDict[int, int]' = OrderedDict()
self.desktop_to_dbus: dict[int, int] = {}
# notifications for which a close was requested before the
# notification daemon replied with the dbus id
self.pending_closes: set[int] = set()
def query_live_notifications(self, channel_id: int, identifier: str) -> None:
self.notification_manager.send_live_response(channel_id, identifier, tuple(self.desktop_to_dbus))
@ -711,6 +714,14 @@ 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')
return close_succeeded
def get_desktop_notification_id(self, dbus_notification_id: int, event: str) -> int | None:
@ -727,13 +738,23 @@ class FreeDesktopIntegration(DesktopIntegration):
log_error(f'Could not find dbus_notification_id for {desktop_notification_id=} for event {event}')
return q
def created(self, dbus_notification_id: int, desktop_notification_id: int) -> None:
self.dbus_to_desktop[desktop_notification_id] = dbus_notification_id
self.desktop_to_dbus[dbus_notification_id] = desktop_notification_id
def created(self, desktop_notification_id: int, dbus_notification_id: int) -> None:
self.dbus_to_desktop[dbus_notification_id] = desktop_notification_id
self.desktop_to_dbus[desktop_notification_id] = dbus_notification_id
if len(self.dbus_to_desktop) > 128:
k, v = self.dbus_to_desktop.popitem(False)
self.desktop_to_dbus.pop(v, None)
if n := self.notification_manager.notification_created(dbus_notification_id):
if desktop_notification_id in self.pending_closes:
self.pending_closes.discard(desktop_notification_id)
from .fast_data_types import dbus_close_notification
close_succeeded = dbus_close_notification(dbus_notification_id)
if debug_desktop_integration:
log_error(f'Deferred close request for {desktop_notification_id=} {"succeeded" if close_succeeded else "failed"}')
if not close_succeeded:
self.notification_manager.notification_closed(desktop_notification_id)
return
if n := self.notification_manager.notification_created(desktop_notification_id):
# self.supports_sound does not tell us if the notification server
# supports named sounds or not so we play the named sound
# ourselves and tell the server to mute any sound it might play.
@ -756,6 +777,7 @@ class FreeDesktopIntegration(DesktopIntegration):
if debug_desktop_integration:
log_error(f'Got notification event from desktop: {event_type=} {dbus_notification_id=} {extra=}')
if event_type == 'created':
# for this event the first argument is the desktop_notification_id and extra is the dbus id
self.created(dbus_notification_id, int(extra))
return
if desktop_notification_id := self.get_desktop_notification_id(dbus_notification_id, event_type):

View file

@ -6,8 +6,9 @@ import os
import re
import tempfile
from base64 import standard_b64encode
from unittest.mock import patch
from kitty.notifications import Channel, DesktopIntegration, IconDataCache, NotificationManager, UIState, Urgency
from kitty.notifications import Channel, DesktopIntegration, FreeDesktopIntegration, IconDataCache, NotificationManager, UIState, Urgency
from .base import BaseTest
@ -362,3 +363,47 @@ class TestNotifications(BaseTest):
def test_desktop_notify(self):
with tempfile.TemporaryDirectory() as tdir:
do_test(self, tdir)
def test_freedesktop_close_before_created(self):
import kitty.fast_data_types as fdt
sent: list[int] = []
closed: list[int] = []
def send_notification(**kw: object) -> int:
sent.append(len(sent) + 1)
return sent[-1]
def close_notification(dbus_id: int) -> bool:
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,
):
di = FreeDesktopIntegration(None)
ch = Channel()
nm = NotificationManager(di, ch, lambda *a, **kw: None, base_cache_dir=tdir)
di.notification_manager = nm
# close requested before the notification daemon replies to Notify
nm.handle_notification_cmd(1, 99, 'i=x:c=1;title')
nm.handle_notification_cmd(1, 99, 'i=x:p=close;')
self.ae(closed, [])
self.assertIn('x', nm.in_progress_notification_commands_by_client_id)
self.ae(ch.responses, [])
di.dispatch_event_from_desktop('created', sent[-1], 101)
self.ae(closed, [101])
di.dispatch_event_from_desktop('closed', 101, 0)
self.assertNotIn('x', nm.in_progress_notification_commands_by_client_id)
self.ae(ch.responses, ['99;i=x:p=close;'])
self.ae(di.pending_closes, set())
# normal close after creation
closed.clear(), ch.reset()
nm.handle_notification_cmd(1, 99, 'i=y;title')
di.dispatch_event_from_desktop('created', sent[-1], 102)
self.ae(closed, [])
nm.handle_notification_cmd(1, 99, 'i=y:p=close;')
self.ae(closed, [102])