Skip to content

Only run late_start service scripts once - #10252

Open
VoronFX wants to merge 1 commit into
topjohnwu:masterfrom
VoronFX:latestart-once
Open

VoronFX wants to merge 1 commit into
topjohnwu:masterfrom
VoronFX:latestart-once

Conversation

@VoronFX

@VoronFX VoronFX commented Nov 14, 2026

Copy link
Copy Markdown

Only run late_start service scripts once

Problem

boot_stage_handler serializes boot stages and sets BootState::LateStartDone after running late_start(), but never checks that flag. Every LATE_START request runs all service.d scripts and module service.sh scripts again.

LATE_START can reach the daemon more than once per boot. The injected rc (native/src/init/rootdir.rs) binds magisk --service to two init events, and an event may also fire more than once:

on property:vold.decrypt=trigger_restart_framework
    exec ... magisk --service

on nonencrypted
    exec ... magisk --service

I haven't determined which event fires twice on the device below (it uses FBE and vold.decrypt is empty after boot, so a repeated nonencrypted seems more likely), but the handler shouldn't rely on a single request either way.

Observed

Pixel 5 (redfin), Android 11 (RQ3A.211001.001), file-based encryption, Magisk 31.0. Every boot, magisk.log shows two late_start passes from different daemon threads ~70ms apart:

11-15 01:05:58.905   749   849 I : ** late_start service mode running
11-15 01:05:58.905   749   849 I : * Running service.d scripts
...
11-15 01:05:58.944   749   849 I : tricky_store: exec [service.sh]
11-15 01:05:58.975   749   751 I : ** late_start service mode running
11-15 01:05:58.975   749   751 I : * Running service.d scripts
...
11-15 01:05:59.065   749   751 I : tricky_store: exec [service.sh]
11-15 01:06:17.781   749   751 I : ** boot-complete triggered

The same pattern is present in the previous boot's log (magisk.log.bak). Modules that start a long-running daemon from service.sh without their own single-instance guard end up with duplicate daemons; here Tricky Store ran two instances (~100 MB RAM) on every boot.

Fix

Check LateStartDone before running late_start(), the same way PostFsDataDone already guards POST_FS_DATA. LateStartDone is only set after late_start() has actually run, so an early LATE_START that is skipped (e.g. before post-fs-data completed) does not block a later one.

Testing

  • Built v31.0 + this patch (release). Compared with an unmodified local v31.0 build (local builds are byte-reproducible): only libmagisk.so differs, by a single immediate in the LATE_START branch (state mask 0b1001 -> 0b1011).
  • Booted on the Pixel 5 above: magisk.log shows a single late_start service mode running, one instance of every service script, a single Tricky Store daemon, root and Zygisk modules working normally.
  • ./build.py clippy passes.

🤖 Generated with Claude Code

boot_stage_handler sets LateStartDone after running late_start() but
never checks it, so every LATE_START request runs all service.d and
module service.sh scripts again.

LATE_START can arrive more than once per boot: the injected rc binds
magisk --service to two init events (`on nonencrypted` and
`on property:vold.decrypt=trigger_restart_framework`), and an event
may also fire more than once. On a Pixel 5 (Android 11, FBE) the
daemon receives two LATE_START requests ~70ms apart on every boot, so
every service script runs twice. Modules that start a long-running
daemon from service.sh without their own single-instance guard (e.g.
Tricky Store) end up with two daemon instances.

Check LateStartDone before running late_start(), the same way
PostFsDataDone already guards POST_FS_DATA.

Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vvb2060

vvb2060 commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Running LATE_START multiple times is expected behavior; otherwise, soft reboots would be broken. Magisk only guarantees that POST_FS_DATA runs exactly once.

@vvb2060 vvb2060 closed this Nov 17, 2026
@vvb2060 vvb2060 reopened this Nov 18, 2026

This branch has not been deployed

No deployments
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