Skip to content

Conversation

@fly602
Copy link
Contributor

@fly602 fly602 commented Jul 9, 2025

dde-session-daemon part2 load after org.deepin.dde.SessionManager1 running

Log:
pms: BUG-323803

Summary by Sourcery

Defer part2 module initialization until the DDE SessionManager1 D-Bus service is available, remove the outdated cookie check, and add robust error handling for part1 module loading.

Bug Fixes:

  • Ensure part2 modules load correctly by waiting for "org.deepin.dde.SessionManager1" to own its D-Bus name.

Enhancements:

  • Always enable part1 modules with error checking and exit on failure.
  • Introduce a D-Bus session loop and NameOwnerChanged listener to trigger part2 startup once SessionManager1 is running.
  • Remove the DDE_SESSION_PROCESS_COOKIE_ID environment variable check from StartPart2 to eliminate dependency on the cookie.

dde-session-daemon part2 load after org.deepin.dde.SessionManager1 running

Log:
pms: BUG-323803
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 @fly602 - I've reviewed your changes - here's some feedback:

  • You’re ignoring the error from dbus.SessionBus()—make sure to handle or log it before using sessionBus to avoid nil-pointer issues.
  • The signal loop started via sessionSigLoop.Start() is never stopped—add proper cleanup (e.g. sessionSigLoop.Stop()) after removing the handler to prevent resource leaks.
  • Dropping the DDE_SESSION_PROCESS_COOKIE_ID check in StartPart2 may introduce unexpected states—verify that removing this validation is safe for all launch scenarios.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- You’re ignoring the error from dbus.SessionBus()—make sure to handle or log it before using sessionBus to avoid nil-pointer issues.
- The signal loop started via sessionSigLoop.Start() is never stopped—add proper cleanup (e.g. sessionSigLoop.Stop()) after removing the handler to prevent resource leaks.
- Dropping the DDE_SESSION_PROCESS_COOKIE_ID check in StartPart2 may introduce unexpected states—verify that removing this validation is safe for all launch scenarios.

## Individual Comments

### Comment 1
<location> `bin/dde-session-daemon/daemon.go:255` </location>
<code_context>
-	} else {
-		err = loader.EnableModules(s.getAllDefaultEnabledModules(),
-			s.getAllDefaultDisabledModules(), getEnableFlag(s.flags))
+	err = loader.EnableModules(s.part1EnabledModules, s.part1DisabledModules, 0)
+	if err != nil {
+		logger.Warning("Failed to enable part1 modules:", err)
</code_context>

<issue_to_address>
Consider extracting the DBus and signal loop logic from execDefaultAction into a dedicated helper to keep the function focused and readable.

```suggestion
The new DBus/signal‐loop logic in `execDefaultAction` is adding a lot of noise. You can pull it into a small helper (e.g. `sessionWatcher`) and keep `execDefaultAction` focused:

--- session_watcher.go ---
```go
package main

import (
    "sync"

    "github.com/godbus/dbus/v5"
    dbusmgr "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.dbus"
    "github.com/linuxdeepin/go-lib/dbusutil"
)

type sessionWatcher struct {
    once sync.Once
    mgr  *dbusmgr.DBus
    loop *dbusutil.SignalLoop
}

func newSessionWatcher(conn *dbus.Conn) *sessionWatcher {
    loop := dbusutil.NewSignalLoop(conn, 10)
    loop.Start()
    mgr := dbusmgr.NewDBus(conn)
    mgr.InitSignalExt(loop, true)
    return &sessionWatcher{mgr: mgr, loop: loop}
}

// Start invokes `onStarted` once when the name appears (or already exists).
func (w *sessionWatcher) Start(onStarted func()) error {
    handler, err := w.mgr.ConnectNameOwnerChanged(func(name, _, newOwner string) {
        if name == "org.deepin.dde.SessionManager1" && newOwner != "" {
            w.once.Do(func() {
                onStarted()
                w.mgr.RemoveHandler(handler)
            })
        }
    })
    if err != nil {
        return err
    }
    ok, err := w.mgr.NameHasOwner(0, "org.deepin.dde.SessionManager1")
    if err != nil {
        return err
    }
    if ok {
        w.once.Do(func() {
            onStarted()
            w.mgr.RemoveHandler(handler)
        })
    }
    return nil
}
```

Then simplify `execDefaultAction`:

```go
func (s *SessionDaemon) execDefaultAction() {
    if err := loader.EnableModules(s.part1EnabledModules, s.part1DisabledModules, 0); err != nil {
        logger.Warning("Failed to enable part1 modules:", err)
        os.Exit(3)
    }

    conn, _ := dbus.SessionBus()
    watcher := newSessionWatcher(conn)
    if err := watcher.Start(s.StartPart2); err != nil {
        logger.Warning("session watcher setup failed:", err)
        os.Exit(3)
    }
}
```

This keeps all functionality, reduces nesting in `execDefaultAction`, and moves DBus details into a focused helper.
</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.

} else {
err = loader.EnableModules(s.getAllDefaultEnabledModules(),
s.getAllDefaultDisabledModules(), getEnableFlag(s.flags))
err = loader.EnableModules(s.part1EnabledModules, s.part1DisabledModules, 0)
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the DBus and signal loop logic from execDefaultAction into a dedicated helper to keep the function focused and readable.

Suggested change
err = loader.EnableModules(s.part1EnabledModules, s.part1DisabledModules, 0)
The new DBus/signalloop logic in `execDefaultAction` is adding a lot of noise. You can pull it into a small helper (e.g. `sessionWatcher`) and keep `execDefaultAction` focused:
--- session_watcher.go ---
```go
package main
import (
"sync"
"github.com/godbus/dbus/v5"
dbusmgr "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.dbus"
"github.com/linuxdeepin/go-lib/dbusutil"
)
type sessionWatcher struct {
once sync.Once
mgr *dbusmgr.DBus
loop *dbusutil.SignalLoop
}
func newSessionWatcher(conn *dbus.Conn) *sessionWatcher {
loop := dbusutil.NewSignalLoop(conn, 10)
loop.Start()
mgr := dbusmgr.NewDBus(conn)
mgr.InitSignalExt(loop, true)
return &sessionWatcher{mgr: mgr, loop: loop}
}
// Start invokes `onStarted` once when the name appears (or already exists).
func (w *sessionWatcher) Start(onStarted func()) error {
handler, err := w.mgr.ConnectNameOwnerChanged(func(name, _, newOwner string) {
if name == "org.deepin.dde.SessionManager1" && newOwner != "" {
w.once.Do(func() {
onStarted()
w.mgr.RemoveHandler(handler)
})
}
})
if err != nil {
return err
}
ok, err := w.mgr.NameHasOwner(0, "org.deepin.dde.SessionManager1")
if err != nil {
return err
}
if ok {
w.once.Do(func() {
onStarted()
w.mgr.RemoveHandler(handler)
})
}
return nil
}

Then simplify execDefaultAction:

func (s *SessionDaemon) execDefaultAction() {
    if err := loader.EnableModules(s.part1EnabledModules, s.part1DisabledModules, 0); err != nil {
        logger.Warning("Failed to enable part1 modules:", err)
        os.Exit(3)
    }

    conn, _ := dbus.SessionBus()
    watcher := newSessionWatcher(conn)
    if err := watcher.Start(s.StartPart2); err != nil {
        logger.Warning("session watcher setup failed:", err)
        os.Exit(3)
    }
}

This keeps all functionality, reduces nesting in execDefaultAction, and moves DBus details into a focused helper.

@sourcery-ai
Copy link

sourcery-ai bot commented Jul 9, 2025

Reviewer's Guide

Unify the session daemon startup by removing the cookie-based gating, always enabling part1 modules with robust error handling, and implementing a DBus-based signal listener (with immediate owner check) to trigger part2 when the SessionManager is available.

File-Level Changes

Change Details Files
Unify and harden part1 module startup
  • Removed hasDDECookie conditional branch
  • Always invoke loader.EnableModules for part1 modules
  • Added logger.Warning and os.Exit(3) on EnableModules errors
bin/dde-session-daemon/daemon.go
Integrate DBus signal loop to trigger part2
  • Initialized sessionBus and dbusDaemon
  • Started a session signal loop and called InitSignalExt
  • Connected NameOwnerChanged handler to invoke StartPart2 once and remove itself
bin/dde-session-daemon/daemon.go
Immediately trigger part2 if SessionManager already running
  • Called NameHasOwner for org.deepin.dde.SessionManager1
  • Logged warnings and exit on check failure
  • Used once.Do to run StartPart2 and remove the handler
bin/dde-session-daemon/daemon.go
Remove DDE cookie check from StartPart2
  • Deleted early return when DDE_SESSION_PROCESS_COOKIE_ID is empty
bin/dde-session-daemon/daemon.go

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

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Jul 10, 2025

TAG Bot

New tag: 6.1.43
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #834

@deepin-ci-robot
Copy link

deepin pr auto review

关键摘要:

  • execDefaultAction函数中,session.Register()调用被移除,需要确认这是否是有意为之。
  • os.Exit(3)loader.EnableModules失败时被调用,这会导致程序立即退出,可能不是最佳处理方式,建议改为返回错误或抛出异常。
  • 使用sync.Once确保StartPart2只被调用一次,这是一个好的做法。
  • dbusDaemon.ConnectNameOwnerChanged的回调函数中,handler被添加后没有在错误处理中移除,可能会导致资源泄露。
  • dbusDaemon.NameHasOwner检查后,如果org.deepin.dde.SessionManager1有所有者,则立即调用StartPart2,这可能会导致StartPart2org.deepin.dde.SessionManager1完全启动之前就被调用。

是否建议立即修改:

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602, robertkill

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

@fly602 fly602 merged commit 611e19d into linuxdeepin:master Jul 15, 2025
16 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