Skip to content

fix(waylib): guard text-input-v3 focus transitions to avoid crash#1066

Merged
zccrs merged 1 commit into
linuxdeepin:masterfrom
LFRon:fix/crashed-by-text-input
Jul 1, 2026
Merged

fix(waylib): guard text-input-v3 focus transitions to avoid crash#1066
zccrs merged 1 commit into
linuxdeepin:masterfrom
LFRon:fix/crashed-by-text-input

Conversation

@LFRon

@LFRon LFRon commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

该treeland崩溃在我使用Android Studio时遇到

wlroots requires wlr_text_input_v3_send_enter() to be called only when the text input has no focused surface. WInputMethodHelper could resend keyboard focus while wlroots still kept a raw focused_surface, causing the focused_surface == NULL assertion to abort.

Make WTextInputV3::sendEnter() synchronize with the wlroots focus state by no-oping when the target surface is already focused and sending leave before entering a different surface. Also make sendLeave() check the raw wlroots focused_surface directly, and let notifyLeave() clear all currently focused text inputs instead of only the first wrapper-visible one.

This keeps text-input-v3 focus transitions idempotent and avoids crashes when focus is resent for Xwayland/Wayland surfaces.

Summary by Sourcery

Guard text-input-v3 focus transitions to align with wlroots expectations and prevent crashes when focus is resent.

Bug Fixes:

  • Prevent text-input-v3 crashes by avoiding send_enter when the target surface is already focused and ensuring leave is sent before entering a different surface.
  • Ensure sendLeave uses the raw wlroots focused_surface state and notifyLeave clears all focused text inputs instead of only the first one.

@deepin-ci-robot

Copy link
Copy Markdown

Hi @LFRon. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@sourcery-ai

sourcery-ai Bot commented Jun 27, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Guard and synchronize text-input-v3 focus transitions with wlroots’ raw focus state to avoid assertion failures when focus is resent, and ensure all tracked text inputs are properly left on notifyLeave().

Sequence diagram for guarded text-input-v3 focus transitions

sequenceDiagram
    actor Client
    participant WInputMethodHelper
    participant WTextInputV3
    participant WSurface
    participant wlroots_text_input_v3

    Client->>WInputMethodHelper: notifyLeave()
    WInputMethodHelper->>WTextInputV3: focusedSurface()
    alt any_text_input_has_focused_surface
        WInputMethodHelper->>WTextInputV3: sendLeave()
        WTextInputV3->>wlroots_text_input_v3: send_leave()
    end

    Client->>WTextInputV3: sendEnter(surface)
    WTextInputV3->>WSurface: handle()
    WTextInputV3->>wlroots_text_input_v3: read focused_surface
    alt focused_surface == targetSurface
        WTextInputV3-->>Client: no-op
    else focused_surface != targetSurface
        opt focused_surface_non_null
            WTextInputV3->>wlroots_text_input_v3: send_leave()
        end
        WTextInputV3->>wlroots_text_input_v3: send_enter(targetSurface)
    end
Loading

File-Level Changes

Change Details Files
Make text-input-v3 enter/leave operations idempotent and consistent with wlroots’ focused_surface state to prevent crashes.
  • Add a null check and Q_ASSERT for the target surface before sending enter.
  • Skip sending enter when the target surface is already focused according to wlroots’ focused_surface field.
  • Send leave via wlroots before entering a different surface if some surface is already focused.
  • Update sendLeave() to check wlroots’ raw focused_surface directly before issuing leave.
waylib/src/server/protocols/private/wtextinputv3.cpp
Ensure notifyLeave() clears focus from all text-input instances instead of only the first visible one.
  • Introduce access to the helper’s internal textInputs collection via the private data pointer.
  • Iterate over all tracked textInputs and call sendLeave() on each that currently has a focused surface.
  • Remove reliance on focusedTextInput() returning a single focused instance.
waylib/src/server/protocols/winputmethodhelper.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In WTextInputV3::sendEnter/sendLeave, consider using the existing focusedSurface() accessor (or refactoring it) instead of directly accessing handle()->handle()->focused_surface so the focus-state logic is centralized in one place.
  • In WInputMethodHelper::notifyLeave, now that you iterate over all textInputs, it may be clearer and safer to iterate over a copy or a stable container if sendLeave() can indirectly mutate d->textInputs, to avoid potential invalidation during the loop.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `WTextInputV3::sendEnter`/`sendLeave`, consider using the existing `focusedSurface()` accessor (or refactoring it) instead of directly accessing `handle()->handle()->focused_surface` so the focus-state logic is centralized in one place.
- In `WInputMethodHelper::notifyLeave`, now that you iterate over all `textInputs`, it may be clearer and safer to iterate over a copy or a stable container if `sendLeave()` can indirectly mutate `d->textInputs`, to avoid potential invalidation during the loop.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@LFRon LFRon force-pushed the fix/crashed-by-text-input branch from ece45ba to 9aba4b2 Compare June 27, 2026 10:23
@LFRon

LFRon commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

附上崩溃堆栈:
1.txt

@LFRon LFRon force-pushed the fix/crashed-by-text-input branch from 9aba4b2 to cfcb9ce Compare June 29, 2026 08:23
wlroots requires wlr_text_input_v3_send_enter() to be called only
when the text input has no focused surface. WInputMethodHelper could
resend keyboard focus while wlroots still kept a raw focused_surface,
causing the focused_surface == NULL assertion to abort.

Make WTextInputV3::sendEnter() synchronize with the wlroots focus
state by no-oping when the target surface is already focused and
sending leave before entering a different surface. Also make
sendLeave() check the raw wlroots focused_surface directly, and let
notifyLeave() clear all currently focused text inputs instead of only
the first wrapper-visible one.

This keeps text-input-v3 focus transitions idempotent and avoids
crashes when focus is resent for Xwayland/Wayland surfaces.
@LFRon LFRon force-pushed the fix/crashed-by-text-input branch from cfcb9ce to e6ef117 Compare July 1, 2026 02:38
@zccrs zccrs requested a review from asterwyx July 1, 2026 08:15
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LFRon, zccrs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@zccrs

zccrs commented Jul 1, 2026

Copy link
Copy Markdown
Member

此PR我看着ok, 感谢你的贡献。

@zccrs zccrs merged commit 3a23d73 into linuxdeepin:master Jul 1, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants