Skip to content

daemon: Allow clients to cancel requests - #246

Merged
iinuwa merged 1 commit into
linux-credentials:mainfrom
dangerousplay:handler-request-close
Nov 16, 2026
Merged

iinuwa merged 1 commit into
linux-credentials:mainfrom
dangerousplay:handler-request-close

Conversation

@dangerousplay

@dangerousplay dangerousplay commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

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.

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

@dangerousplay

Copy link
Copy Markdown
Contributor 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
@iinuwa
iinuwa force-pushed the handler-request-close branch from 5721ec8 to 84937eb Compare November 16, 2026 21:54
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.handler.portal.experimental.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).
@iinuwa
iinuwa force-pushed the handler-request-close branch from 84937eb to 02262d4 Compare November 16, 2026 22:00

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

Thanks! This works well, just a follow-up comment.

Comment thread credentialsd/src/gateway/dbus.rs Resolved
client_cancellation: CancellationToken,
) -> Result<CredentialResponse, CredentialServiceError> {
if client_cancellation.is_cancelled() {
return Err(CredentialServiceError::Internal(

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.

This is a follow-up note, not blocking for this PR: returning Internal here instead of an explicit cancellation error is . Part of the awkwardness is that FlowControlService returns a CredentialServiceError instead of its own error.

@iinuwa
iinuwa enabled auto-merge November 16, 2026 22:01
@iinuwa
iinuwa merged commit 0acdafe into linux-credentials:main Nov 16, 2026
1 check passed
iinuwa pushed a commit that referenced this pull request Nov 20, 2026
With #246, a client can cancel a request explicitly with Close(), but a
request whose caller leaves the bus unexpectedly, e.g. because it crashed,
stays open until the user dismisses the UI.

Watch the caller's unique name for the duration of the ceremony and cancel
the request once it has no owner. NameHasOwner is checked after subscribing
to NameOwnerChanged, so a caller that left before the watch was set up is
caught too. This works with and without a request handle. If setting up the
watch fails, the request goes on as before.

In the portal setup the caller is xdg-desktop-portal, which already closes
the handler request when the app that made it goes away. This covers the
portal itself going away, and callers that talk to the daemon directly.

Fixes #274
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