Skip to content

fix(power): restart idle tasks from now#1169

Open
mhduiy wants to merge 1 commit into
linuxdeepin:masterfrom
mhduiy:fix/power-idle-restart-timer
Open

fix(power): restart idle tasks from now#1169
mhduiy wants to merge 1 commit into
linuxdeepin:masterfrom
mhduiy:fix/power-idle-restart-timer

Conversation

@mhduiy

@mhduiy mhduiy commented Jul 7, 2026

Copy link
Copy Markdown
Contributor
  1. Reset the power save plan from the current time when the battery state changes.
  2. Add a reset path that keeps full task delays while the session is already idle.
  3. Preserve elapsed-idle checks only for the normal idle entry path.

Log: Restart idle power save tasks from the current time after power source changes.

fix(power): 修复 idle 任务重置

  1. 电池状态变化时从当前时间重新重置节能计划。
  2. 会话已处于 idle 状态时保留任务完整延迟重新调度。
  3. 仅在正常进入 idle 流程时保留已 idle 时长检查。

Log: 电源状态变化后从当前时间重新调度 idle 节能任务。

Summary by Sourcery

Restart idle power save tasks from the current time when power source changes, while preserving elapsed idle checks only for normal idle entry.

Bug Fixes:

  • Fix power save task scheduling so that battery state changes restart idle tasks from the current time instead of reusing previous timing.
  • Ensure idle tasks keep their full configured delays when rescheduled while the session is already idle.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy

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

1. Reset the power save plan from the current time when the battery state changes.
2. Add a reset path that keeps full task delays while the session is already idle.
3. Preserve elapsed-idle checks only for the normal idle entry path.

Log: Restart idle power save tasks from the current time after power source changes.

fix(power): 修复 idle 任务重置

1. 电池状态变化时从当前时间重新重置节能计划。
2. 会话已处于 idle 状态时保留任务完整延迟重新调度。
3. 仅在正常进入 idle 流程时保留已 idle 时长检查。

Log: 电源状态变化后从当前时间重新调度 idle 节能任务。
@sourcery-ai

sourcery-ai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR changes how idle power save tasks are reset when the power source (battery vs line power) changes: it adds a ResetFromNow path that recomputes delays from the current time, differentiates behavior when the session is already idle vs entering idle normally, and threads a new flag through the update and idle-task startup logic to control whether elapsed-idle checks are applied.

Sequence diagram for updated idle power save reset behavior

sequenceDiagram
    participant PowerOnBattery as power.OnBattery
    participant PowerSavePlan as powerSavePlan
    participant MetaTasks as metaTasks

    PowerOnBattery->>PowerSavePlan: ResetFromNow()
    PowerSavePlan->>PowerSavePlan: doUpdate(delays, shortIdleDelay, true)
    alt psp.isIdle
        PowerSavePlan->>MetaTasks: setRealDelayFromNow()
        PowerSavePlan->>PowerSavePlan: startIdleTasksLocked(false)
    else psp not idle
        PowerSavePlan->>MetaTasks: setRealDelay(min)
    end

    actor IdleDetector
    IdleDetector->>PowerSavePlan: HandleIdleOn()
    PowerSavePlan->>PowerSavePlan: startIdleTasksLocked(true)
    opt [useElapsedIdle && !UseWayland]
        PowerSavePlan->>PowerSavePlan: shouldPreventIdle()
    end
Loading

File-Level Changes

Change Details Files
Reset power save plan from current time when power source changes
  • Replaced OnBattery().ConnectChanged callback to call ResetFromNow instead of Reset
  • Introduced ResetFromNow method to reload power-save delays based on current power source and request a from-now reset path
  • Internally routes ResetFromNow through the new doUpdate helper with a resetFromNow flag to share logic with Update
session/power1/power_save_plan.go
Add from-now delay recomputation and idle-aware restart behavior for meta tasks
  • Added metaTasks.setRealDelayFromNow to recompute realDelay purely from configured delay durations
  • Split Update into public Update and internal doUpdate with resetFromNow boolean to select between normal min-based delay adjustment and from-now scheduling
  • When already idle, optionally recompute meta task real delays and restart idle tasks while choosing whether to preserve elapsed-idle checks based on resetFromNow
session/power1/power_save_plan.go
Conditionally preserve elapsed idle checks only on normal idle entry
  • Changed startIdleTasksLocked to accept a useElapsedIdle flag that gates the shouldPreventIdle check
  • Updated HandleIdleOn and the idle reset path to call startIdleTasksLocked with explicit useElapsedIdle semantics so elapsed-idle logic applies only on normal idle entry, not on power source change resets
session/power1/power_save_plan.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

@mhduiy mhduiy force-pushed the fix/power-idle-restart-timer branch from 7b7b1f3 to 3440075 Compare July 7, 2026 07:46

@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:

  • Consider renaming doUpdate and its resetFromNow boolean flag to something more explicit (e.g., updateWithResetMode(..., resetDelaysFromNow bool)), and/or adding a short comment, to clarify the behavioral difference for future maintainers.
  • The new startIdleTasksLocked(useElapsedIdle bool) signature changes behavior significantly depending on the flag; adding a brief comment on the function or parameter explaining when useElapsedIdle should be true vs false would make the intent clearer and reduce misuse.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider renaming `doUpdate` and its `resetFromNow` boolean flag to something more explicit (e.g., `updateWithResetMode(..., resetDelaysFromNow bool)`), and/or adding a short comment, to clarify the behavioral difference for future maintainers.
- The new `startIdleTasksLocked(useElapsedIdle bool)` signature changes behavior significantly depending on the flag; adding a brief comment on the function or parameter explaining when `useElapsedIdle` should be true vs false would make the intent clearer and reduce misuse.

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-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码重构了电源节能计划的重置逻辑,消除了代码重复
逻辑正确且无安全漏洞,符合优秀代码标准

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

代码正确提取了doUpdate方法处理共用逻辑,新增ResetFromNow方法处理电池状态切换时的重置,调整了startIdleTasksLocked的参数以区分是否使用已经过的时间,导入顺序调整符合Go语言规范

建议:无需修改

  • 2.代码质量(良好)✓

通过提取doUpdate方法有效避免了Update和ResetFromNow之间的代码重复,变量命名如useElapsedIdle准确表达了业务意图,逻辑分层清晰

建议:可为setRealDelayFromNow方法添加简短注释,说明其直接使用delay而非基于系统空闲时间的计算逻辑

  • 3.代码性能(无性能问题)✓

仅涉及方法拆分和条件判断的增加,没有引入额外的系统调用、循环或资源消耗

建议:无需修改

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码仅涉及内部状态管理和时间计算,未引入外部输入、命令执行或文件操作,无安全风险

  • 建议:保持当前的内部状态访问模式

■ 【改进建议代码示例】

// setRealDelayFromNow 设置从当前时刻开始的实际延迟,而非从系统空闲开始计算
func (mts metaTasks) setRealDelayFromNow() {
	for idx := range mts {
		t := &mts[idx]
		t.realDelay = time.Second * time.Duration(t.delay)
	}
}

@deepin-bot

deepin-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.1.100
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1173

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.

2 participants