Skip to content

fix(screencast): honor Request.Close and propagate session closure - #16

Merged
pantarune merged 2 commits into
pantarune:mainfrom
ParticleG:fix/portal-request-session-lifecycle
Oct 26, 2026
Merged

pantarune merged 2 commits into
pantarune:mainfrom
ParticleG:fix/portal-request-session-lifecycle

Conversation

@ParticleG

Copy link
Copy Markdown
Contributor

Problem

On v0.2.3 and main at 8bbf3e1, basic window capture works, but the backend ignores the request handles passed to its methods. Calling the public Portal Request.Close while the chooser is open fails because the backend has no org.freedesktop.impl.portal.Request object, and the chooser stays open. This is separate from the picker-button Cancel fix in #3 / #4.

Also, closing the captured window stops the PipeWire stream but leaves the backend and frontend Session objects alive without a Closed notification. org.freedesktop.impl.portal.Session.version is missing.

Changes

  • Export per-call Request objects for CreateSession, SelectSources, and Start; remove them on success, cancellation, or error.
  • Cancel the owning chooser or in-flight capture startup on Request.Close, without cancelling another request.
  • Make picker cancellation sticky across the child-spawn boundary and reap cancelled children.
  • Expose Session version = 1, subscribe to the compositor session's Closed signal before startup, and notify/unexport the portal session when the compositor ends it.
  • Keep explicit Session.Close cleanup idempotent and preserve failed-start retry behavior.
  • Add five private-D-Bus regression tests, install dbus-daemon in CI, and document the lifecycle behavior.

Why ordered dispatch is necessary

A Request handler registered only at the start of an async ScreenCast method is insufficient: zbus normally spawns the method task, then can dispatch an immediately following Close before that task has exported its object. This race was reproduced through the real public Portal frontend during testing.

OrderedScreenCast registers the request in message order before handing the method to an async worker. It delegates argument validation, replies, properties, and introspection to the generated interface. Existing serialized ScreenCast operation behavior is retained, while Request and Session cancellation remain independently dispatchable. No sleeps, retries, or cancellation fallbacks are used to hide the race. The zbus minimum is updated to 5.18 to match the Interface API already present in the lockfile.

Automated verification

Passed locally:

cargo fmt --all -- --check
cargo test --locked                       # 24 passed
cargo test --locked --no-default-features # 16 passed
cargo clippy --locked --all-targets -- -D warnings
cargo clippy --locked --all-targets --no-default-features -- -D warnings
cargo build --locked

The new tests cover cancellation isolation/stickiness, back-to-back Start and Request.Close messages without waiting for request registration, cancellation during startup, Session.Close during startup, and compositor closure notification/unexport. Each test starts its own private bus and controlled compositor service.

Real desktop smoke verification

Environment: Arch Linux, niri 26.04 (v26.04-206-ge19df12c), xdg-desktop-portal 1.22.1-2, PipeWire 1.6.9.

The patched backend and the installed Portal frontend ran on a private D-Bus session. A temporary transport bridge forwarded only the compositor ScreenCast API to the real niri session. Request/Session objects and cancellation were handled directly by the patched backend, not simulated by the bridge. The normal desktop Portal routing was not changed.

Verified:

  • Public Request.Close succeeds and closes an already visible GTK chooser.
  • Immediate cancellation succeeds in five consecutive attempts; an additional check waits for request removal and verifies that no delayed chooser appears, without using Session.Close to make cancellation pass.
  • Clicking the chooser's Cancel button still returns response 1.
  • CreateSession, SelectSources, Start, and OpenPipeWireRemote work end to end. An actual PipeWire consumer receives changing red/blue frames from an owned single-window fixture through the Portal-provided FD.
  • Explicit Session.Close removes the compositor capture.
  • Closing the captured window produces EOS, exactly one backend Closed and one frontend Closed, removes both Session objects, and leaves no capture running.
  • All temporary services/windows were stopped; no system package or Portal preference was changed.

This does not claim coverage of browser meeting applications or XWayland windows.

Protocol references

Copilot AI balanced review requested due to automatic review settings October 26, 2026 03:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Unresolved cancellation and packaging issues remain, and the custom dispatch and concurrent teardown paths need final human review.

2 open issues
What changed in this PR

This PR extends the ScreenCast backend’s lifecycle handling beyond picker-button cancellation, adding support for application-request cancellation and compositor-driven session closure.

Changes:

  • Export per-call Request objects with ordered registration and cancellation handling.
  • Make picker cancellation sticky and propagate compositor closure to portal sessions.
  • Add private-D-Bus regression tests, CI dependencies, and lifecycle documentation.
File Description
src/​screencast/​request.rs Manage Request registration, cancellation, and removal.
src/​screencast/​pick.rs Preserve cancellation across spawning and reap cancelled children.
src/​screencast/​mod.rs Integrate cancellation and session closure handling.
src/​screencast/​lifecycle_tests.rs Add five private-D-Bus lifecycle tests.
src/​screencast/​dispatch.rs Register requests before dispatching asynchronous workers.
src/​main.rs Register ScreenCast through the ordered dispatcher.
README.md Document lifecycle behavior and test dependencies.
Cargo.toml Raise the minimum zbus version to 5.18.
.github/​workflows/​ci.yml Install D-Bus for regression tests.

Review effort: Balanced

Comment on lines +25 to +29
let mut child = Command::new("dbus-daemon")
.args(["--session", "--nofork", "--nopidfile", "--print-address=1"])
.stdout(Stdio::piped())
.spawn()
.expect("D-Bus tests require dbus-daemon");
Comment thread src/screencast/mod.rs
Comment on lines +443 to +444
let _operation = self.operations.lock().await;
let handler = self.session_handler(session_handle.as_str());
@ParticleG

Copy link
Copy Markdown
Contributor Author

Verification update: the complete CI matrix for head 9daf39d is green: https://github.com/ParticleG/niri-screenshare/actions/runs/40267086377 . Both default and --no-default-features jobs passed formatting, release build, release tests, and Clippy with -D warnings. The follow-up commit only replaces a missing-child match with the equivalent ? expression required by Clippy 1.99; it does not relax any checks or change the tested lifecycle behavior.

@pantarune
pantarune merged commit 3cc0d9d into pantarune:main Oct 26, 2026
2 checks passed
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.

3 participants