Skip to content

Conversation

@calsys456
Copy link
Collaborator

@calsys456 calsys456 commented Oct 27, 2025

Delete current session when user logged out, remove all surfaces belongs to the session, and delete WSocket & WXWayland instances.

Summary by Sourcery

Ensure proper cleanup of user sessions by deleting the session, its surfaces, sockets, and XWayland instances upon logout and when sessions are removed.

New Features:

  • Expose activeSession and add removeSession method in Helper to manage session lifecycle
  • Invoke session removal on user logout in GreeterProxy

Enhancements:

  • Implement custom shared_ptr deleter for Session to clean up surfaces, sockets, and XWayland instances
  • Simplify XWayland removal by detaching from server and deleting the instance
  • Update active session handling to use setActivatedSurface for consistent state updates

@deepin-ci-robot
Copy link

Hi @calsys456. 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
Copy link

sourcery-ai bot commented Oct 27, 2025

Reviewer's Guide

This PR refactors session cleanup on logout by centralizing session removal in Helper, enhancing automatic resource cleanup via a custom shared_ptr deleter, updating interface declarations, and ensuring WSocket, WXWayland, and associated surfaces are properly destroyed when a session ends.

Sequence diagram for session cleanup on user logout

sequenceDiagram
actor User
participant GreeterProxy
participant Helper
participant "ShellHandler"
participant "RootSurfaceContainer"
participant "WSocket"
participant "WXWayland"
User->>GreeterProxy: Initiate logout()
GreeterProxy->>Helper: removeSession(activeSession)
Helper->>"RootSurfaceContainer": Destroy surfaces for session
Helper->>"ShellHandler": removeXWayland(xwayland)
Helper->>WSocket: Delete WSocket
Helper->>WXWayland: Delete WXWayland
Helper->>Helper: Remove session from session list
Helper->>Helper: Reset activeSession
Helper->>GreeterProxy: Logout cleanup complete
Loading

Class diagram for updated Helper session management

classDiagram
class Helper {
  +removeSession(std::shared_ptr<Session> session)
  +activeSession() const : std::weak_ptr<Session>
  +sessionForUid(uid_t uid) const : std::shared_ptr<Session>
  +sessionForXWayland(WXWayland *xwayland) const : std::shared_ptr<Session>
  +sessionForSocket(WSocket *socket) const : std::shared_ptr<Session>
  -ensureSession(uid_t uid) : std::shared_ptr<Session>
  -m_activeSession : std::weak_ptr<Session>
  -m_sessions : QList<std::shared_ptr<Session>>
}
class Session {
  +uid : uid_t
  +socket : WSocket*
  +xwayland : WXWayland*
}
Helper "1" *-- "*" Session : manages
Loading

Class diagram for ShellHandler XWayland removal

classDiagram
class ShellHandler {
  +removeXWayland(WXWayland *xwayland)
  -m_xwaylands : QList<WXWayland*>
}
class WXWayland {
}
class WServer {
  +detach(WXWayland *xwayland)
  +from(WXWayland *xwayland) : WServer*
}
ShellHandler "*" o-- "*" WXWayland : manages
WXWayland "1" --o "1" WServer : attached to
Loading

File-Level Changes

Change Details Files
Session lifecycle management centralized in Helper
  • Added activeSession() for accessing the current session
  • Introduced removeSession() to reset active state and drop session from the list
  • Refactored ensureSession() to use shared_ptr with custom deleter cleaning surfaces, XWayland, and socket
src/seat/helper.cpp
src/seat/helper.h
Helper API updated
  • Declared new methods: sessionForUid, sessionForXWayland, sessionForSocket, removeSession, activeSession
  • Removed duplicate declarations of sessionFor… methods
src/seat/helper.h
ShellHandler XWayland cleanup changed
  • Replaced safeDeleteLater() with immediate detach from server
  • Switched to direct delete of WXWayland instance
src/core/shellhandler.cpp
Logout integration in greeter
  • Called Helper::removeSession() on logout using activeSession()
  • Ensured session cleanup is triggered when user logs out
src/greeter/greeterproxy.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

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 there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `src/seat/helper.cpp:2137-2146` </location>
<code_context>
-    // Session does not exist, create new session
-    auto session = std::make_shared<Session>();
+    // Session does not exist, create new session with deleter
+    std::shared_ptr<Session> session(new Session, [this](Session *s) {
+        qCDebug(treelandCore) << "Deleting session for uid:" << s->uid << s->socket;
+
+        const auto surfaces = m_rootSurfaceContainer->surfaces();
+        for (auto surface : surfaces) {
+            auto client = surface->shellSurface()->waylandClient();
+            if (client && client->socket()->rootSocket() == s->socket) {
+                onSurfaceWrapperAboutToRemove(surface);
+                m_rootSurfaceContainer->destroyForSurface(surface);
+            }
+        }
+
+        m_shellHandler->removeXWayland(s->xwayland);
+        delete s->socket;
+        delete s;
+    });
     session->uid = uid;
</code_context>

<issue_to_address>
**issue (bug_risk):** Custom deleter logic may lead to double deletion if session is removed elsewhere.

If other code paths, such as removeSession, also delete s or s->socket, this could cause double deletion. Please clarify and document ownership to prevent undefined behavior.
</issue_to_address>

### Comment 2
<location> `src/seat/helper.cpp:2156-2023` </location>
<code_context>

     session->socket = createWSocket();
-    if (!session->socket) {
+    if (!session->socket)
         return nullptr;
-    }

</code_context>

<issue_to_address>
**issue (bug_risk):** Returning nullptr without cleanup may leak the Session object.

If socket creation fails, Session is not deleted, leading to a memory leak. Use a unique_ptr for Session until all allocations succeed, then transfer ownership as needed.
</issue_to_address>

### Comment 3
<location> `src/seat/helper.cpp:2160-2023` </location>
<code_context>
     session->xwayland = createXWayland(session->socket);
-    if (!session->xwayland) {
-        delete session->socket;
+    if (!session->xwayland)
         return nullptr;
-    }
+
</code_context>

<issue_to_address>
**issue (bug_risk):** Potential memory leak if xwayland creation fails after socket allocation.

Ensure session->socket is deleted if createXWayland fails to prevent a memory leak.
</issue_to_address>

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.

@deepin-bot
Copy link

deepin-bot bot commented Oct 30, 2025

TAG Bot

New tag: 0.7.6
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #611

@calsys456 calsys456 force-pushed the multiuser branch 2 times, most recently from a53a339 to 232deb3 Compare November 7, 2025 06:00
@calsys456
Copy link
Collaborator Author

Related with linuxdeepin/ddm#47

@deepin-bot
Copy link

deepin-bot bot commented Nov 7, 2025

TAG Bot

New tag: 0.7.7
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #619

Delete current session when user logged out, remove all surfaces belongs to the session, and delete WSocket & WXWayland instances.
wl_client should be destroyed together with WClient, to prevent potential error when bare wl_client is applied to globalFilter. It can be done by calling wl_client_destroy in WSocket::removeClient.

However, for wl_client created by wlr_xwayland_server, their life cycle is managed by wlroots. Calling wl_client_destroy ourselves will leads to an error, when wlroots trying to destroy it again laterly. Thus we add a new argument noAutoDestroy, which is used to mark WClient created by wlr_xwayland_server, and exclude them in destruction.
In this commit we connect Helper::Session with corresponding org.freedesktop.login1.Session

And we fixed some misbehaviour btw.
@calsys456 calsys456 force-pushed the multiuser branch 3 times, most recently from 9f71948 to eb03273 Compare November 12, 2025 08:52
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: calsys456, 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 merged commit d3a4d88 into linuxdeepin:master Nov 12, 2025
8 of 9 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