Skip to content

Conversation

@zorowk
Copy link
Contributor

@zorowk zorowk commented Jan 28, 2026

Ensure the activated surface is safely cleared when logging out or switching sessions by using QPointer and unifying the reset path through setActivatedSurface(nullptr).

Log: prevent activated surface use-after-free on logout
PMS: BUG-347763
Influence: logout

Summary by Sourcery

Bug Fixes:

  • Avoid potential use-after-free of the activated surface by making it a QPointer and always clearing it via setActivatedSurface(nullptr).

Ensure the activated surface is safely cleared when logging out or
switching sessions by using QPointer and unifying the reset path through
setActivatedSurface(nullptr).

Log: prevent activated surface use-after-free on logout
PMS: BUG-347763
Influence: logout
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: zorowk

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

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 28, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors how the active/activated surface is cleared on session removal or switch by centralizing the logic through setActivatedSurface(nullptr) and making the activated-surface pointer a QPointer to avoid use-after-free when the underlying QObjects are destroyed, particularly during logout.

Sequence diagram for activated surface reset on logout or session switch

sequenceDiagram
    actor User
    participant SessionManager as Helper
    participant Session
    participant Surface as SurfaceWrapper

    User->>SessionManager: logout()
    SessionManager->>SessionManager: removeSession(session)
    alt session is active
        SessionManager->>SessionManager: m_activeSession.reset()
        SessionManager->>SessionManager: setActivatedSurface(null)
        SessionManager-->>Surface: QPointer cleared
        SessionManager-->>SessionManager: activatedSurfaceChanged signal
    else session not active
        SessionManager->>SessionManager: no changes to activated surface
    end

    User->>SessionManager: switchSession(username, id)
    SessionManager->>SessionManager: updateActiveUserSession(username, id)
    SessionManager->>SessionManager: setActivatedSurface(null)
    SessionManager-->>Surface: QPointer cleared
    SessionManager-->>SessionManager: activatedSurfaceChanged signal
    SessionManager->>SessionManager: update sockets and active session state
Loading

Updated class diagram for Helper activated surface handling

classDiagram
    class QObject

    class SurfaceWrapper {
    }
    SurfaceWrapper --|> QObject

    class Session {
    }

    class Helper {
        - std_weak_ptr_Session m_activeSession
        - QPointer~SurfaceWrapper~ m_activatedSurface
        - LockScreen* m_lockScreen
        - float m_animationSpeed
        - OutputMode m_mode
        - QList~qw_idle_inhibitor_v1*~ m_idleInhibitors
        + void removeSession(std_shared_ptr_Session session)
        + void updateActiveUserSession(QString username, int id)
        + void setActivatedSurface(SurfaceWrapper* surface)
        + void activatedSurfaceChanged()
    }

    Helper o--> Session
    Helper o--> SurfaceWrapper
    Helper o--> LockScreen

    note for Helper "m_activatedSurface is now a QPointer to SurfaceWrapper to prevent use-after-free when the SurfaceWrapper QObject is destroyed"
Loading

File-Level Changes

Change Details Files
Centralize and harden activated-surface reset logic to avoid use-after-free on logout/session switch.
  • Replace direct m_activatedSurface reset and manual activatedSurfaceChanged() emission in removeSession() with a call to setActivatedSurface(nullptr).
  • Remove redundant activatedSurfaceChanged() emission in updateActiveUserSession() and rely on setActivatedSurface(nullptr) to handle state updates and signal emission.
  • Change m_activatedSurface from a raw SurfaceWrapper* to QPointer so it is automatically nulled when the underlying QObject is destroyed, preventing dangling-pointer access.
src/seat/helper.cpp
src/seat/helper.h

Possibly linked issues

  • #BUG-347763: PR converts activatedSurface to QPointer and clears via setActivatedSurface(nullptr), fixing the Helper::activateSurface crash.

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

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:

  • Now that m_activatedSurface is a QPointer, double-check that all remaining call sites go through setActivatedSurface(...) rather than assigning to m_activatedSurface directly, so you don't bypass the unified signaling/cleanup path.
  • Given the change to QPointer<SurfaceWrapper>, confirm that setActivatedSurface(nullptr) and any comparisons to m_activatedSurface still behave as intended when the underlying SurfaceWrapper is deleted out from under you (e.g., nullification race conditions in logout/session-switch flows).
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that `m_activatedSurface` is a `QPointer`, double-check that all remaining call sites go through `setActivatedSurface(...)` rather than assigning to `m_activatedSurface` directly, so you don't bypass the unified signaling/cleanup path.
- Given the change to `QPointer<SurfaceWrapper>`, confirm that `setActivatedSurface(nullptr)` and any comparisons to `m_activatedSurface` still behave as intended when the underlying `SurfaceWrapper` is deleted out from under you (e.g., nullification race conditions in logout/session-switch flows).

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.

QPointer<QQuickItem> m_taskSwitch;
QList<qw_idle_inhibitor_v1 *> m_idleInhibitors;

SurfaceWrapper *m_activatedSurface = nullptr;
Copy link
Member

Choose a reason for hiding this comment

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

理论上窗口销毁前, SurfaceWrapper 应该 unmapped 并发送 requestInactive 信号清理焦点

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