Skip to content

daemon: Allow clients to cancel requests - #246

Open
dangerousplay wants to merge 2 commits into
linux-credentials:mainfrom
dangerousplay:handler-request-close
Open

dangerousplay wants to merge 2 commits into
linux-credentials:mainfrom
dangerousplay:handler-request-close

Conversation

@dangerousplay

@dangerousplay dangerousplay commented Oct 17, 2026 •

Copy link
Copy Markdown

Hello I hope you are having a good day ^^

This pull request fixes #90.

When a client closes its org.freedesktop.portal.Request or disconnects, xdg-desktop-portal needs a way to tell credentialsd to cancel the ceremony. Until now credentialsd exported nothing it could call, so client cancellation was a no-op and the UI stayed open.

This adds an optional request_handle (o) option to the handler's CreateCredential / GetCredential. When it is present, credentialsd exports org.freedesktop.handler.portal.experimental.Request at that path for the lifetime of the ceremony. This is an internal interface between xdg-desktop-portal and credentialsd. Clients keep calling Close() on the org.freedesktop.portal.Request exported by xdg-desktop-portal.

  • Close() is only accepted from the bus name that issued the handler call (others get AccessDenied).
  • It cancels a CancellationToken that is threaded down to the flow controller. It never takes the gateway mutex, so it cannot deadlock with the running ceremony. A request still queued behind another one is cancelled too.
  • The ceremony then calls cancel_request() and sends ErrorCancelled, which dismisses the UI through the existing path, and the handler call returns response code 1 (Cancelled).
  • The object is removed when the ceremony ends, whatever the outcome.
  • The path must live under /org/freedesktop/portal/desktop/request/.

Without the option, behaviour is unchanged. The matching xdg-desktop-portal change is linux-credentials/xdg-desktop-portal#1. It passes request_handle = request->id and calls the handler's Close() from the new XdpRequest::closed signal.

Tested with unit tests (option parsing, response codes, invalid handles), private-bus integration tests (gio::TestDBus: foreign Close -> AccessDenied, caller Close -> code 1, object removed) and flow-control tests.

xdg-desktop-portal calls org.freedesktop.impl.portal.Request.Close() on
the handler when a client closes its request or disconnects, but the
daemon never exported such an object, so requests could not be
cancelled.

Accept an optional `request_handle` object path in the CreateCredential
and GetCredential options. For the duration of the ceremony, export an
org.freedesktop.impl.portal.Request object at that path. Close() is only
accepted from the connection that started the ceremony; it cancels a
token that is passed down to the flow controller, which cancels the
request and dismisses the UI. The cancellation does not need the gateway
service lock, which is held for the whole ceremony. The method call then
returns response code 1 (cancelled).

Fixes linux-credentials#90
@iinuwa

iinuwa commented Oct 22, 2026

Copy link
Copy Markdown
Member

Thanks for this! I will take a look at this next week :)

@iinuwa iinuwa 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.

Sorry for the delay. I finished reading through the code, and it's looking pretty good! I need to run some tests locally and then I think we can merge this in

@iinuwa iinuwa 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.

Hi @dangerousplay, sorry this took so long to get to!

I quite like the ceremony and cancellation token changes you've made.

The handler interface is supposed to be an internal interface between xdg-desktop-portal and credentialsd; it's also deprecated as we shift architecture in xdg-desktop-portal to share have x-d-p call the backend directly.
So I think I would prefer to export a separate org.freedesktop.handler.portal.experimental.Request interface with a Close() method. That would make it more clear that this is not a normal backend request that clients are supposed to call; they should call the Close() method exported by xdg-desktop-portal instead.

Then on the xdg-desktop-portal side, we should hook into the GObject closed signal on the request object, and call the handler's Close() method in the signal handler.

Comment thread ARCHITECTURE.md Resolved
Comment thread doc/api.md Resolved
@kpanuragh

Copy link
Copy Markdown
Contributor

A real-world case for the queued-request cancellation here: testing the browser extension on main, a few quick duplicate create() calls queued up in the flow controller. Once the first finished, the next queued (already abandoned) request opened the UI and held the slot, and every following get() waited behind it until the extension's 60 s timeout. The page just showed "Authentication failed". Cancelling queued requests when the portal calls Close() should fix exactly this.

The handler interface is internal to xdg-desktop-portal and credentialsd,
so the object exported at `request_handle` should not look like a regular
backend request that clients are meant to call. Export it as
org.freedesktop.handler.portal.experimental.Request instead of
org.freedesktop.impl.portal.Request. Clients keep cancelling through the
org.freedesktop.portal.Request object exported by xdg-desktop-portal,
which forwards the call to the handler.

Drop the API and architecture documentation for now, until the
interface settles.

Assisted-by: Claude:claude-opus-5-5
@dangerousplay

Copy link
Copy Markdown
Author

Hi @iinuwa, thanks for the review, no worries about the delay ^^

I've pushed the changes:

  • The handler now exports org.freedesktop.handler.portal.experimental.Request with a Close() method at request_handle, instead of org.freedesktop.impl.portal.Request. The ceremony and cancellation token changes are untouched.
  • I dropped the documentation from ARCHITECTURE.md and doc/api.md. The CHANGELOG entry no longer mentions the request_handle option.

On the xdg-desktop-portal side (linux-credentials/xdg-desktop-portal#1):

  • XdpRequest had no closed signal yet, so I added one. It is emitted when the client calls Close() on its org.freedesktop.portal.Request and when the client disconnects.
  • credential.c connects to it and calls Close() on the handler's new interface. It no longer sets an impl request on the XdpRequest.
  • The new interface has its own XML (org.freedesktop.handler.portal.experimental.Request.xml), and request_handle is documented in the handler Credential XML.

Tested with meson test in both repos. I also ran the built xdg-desktop-portal against a mock handler on a private bus: both Request.Close() and a client disconnect reach the handler as org.freedesktop.handler.portal.experimental.Request.Close(), sent from the connection that made the handler call.

@dangerousplay
dangerousplay requested a review from iinuwa November 14, 2026 03:03
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.

Allow clients to cancel requests

3 participants