Skip to content

fix(updates): expedited request can't carry initial delay (crash hotfix)#552

Merged
rainxchzed merged 1 commit intomainfrom
fix/scheduler-expedited-crash
May 8, 2026
Merged

fix(updates): expedited request can't carry initial delay (crash hotfix)#552
rainxchzed merged 1 commit intomainfrom
fix/scheduler-expedited-crash

Conversation

@rainxchzed
Copy link
Copy Markdown
Member

@rainxchzed rainxchzed commented May 8, 2026

Hotfix for the two crashes from PR #545.

Crash 1 — `Expedited jobs cannot be delayed`

```
java.lang.IllegalArgumentException: Expedited jobs cannot be delayed
at androidx.work.WorkRequest$Builder.build(WorkRequest.kt:305)
at zed.rainxch.core.data.services.UpdateScheduler.schedule(UpdateScheduler.kt:59)
```

`UpdateScheduler.schedule` was building a one-time `UpdateCheckWorker` with both `.setInitialDelay(1, TimeUnit.MINUTES)` (cold-start back-off, predates this PR) and `.setExpedited(...)` (added in PR #545 for aggressive-OEM headroom). WorkManager rejects the combination at `build()` time.

Fix: dropped `setInitialDelay`. Expedited dispatch is what we actually wanted on aggressive ROMs anyway — the 1-minute backoff was a minor optimisation, not a hard requirement. Network-constraint dispatch is fast enough on a fresh boot.

Crash 2 — `Broadcast already finished`

```
java.lang.IllegalStateException: Broadcast already finished
at android.content.BroadcastReceiver$PendingResult.sendFinished(BroadcastReceiver.java:313)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:5591)
```

Caused by Crash 1's `IllegalArgumentException` escaping `BootReceiver.onReceive` after `pendingResult.finish()` had already run in the `finally` block. Android's broadcast pipeline saw the unhandled throw, called its own `sendFinished`, and tripped the already-finished guard.

Fix: added a defensive `catch (t: Throwable) { Logger.e(...) }` between `try` and `finally` in `BootReceiver`. Scheduling failures are logged and swallowed — they don't propagate to the framework. Crash 2 won't recur after Crash 1 is fixed, but the catch is the right shape for any future scheduler bug.

Test plan

  • `:composeApp:compileDebugKotlinAndroid` ✅
  • `:core:data:compileDebugKotlinAndroid` ✅
  • Manual: cold-start app on Android — no immediate crash, periodic + immediate update-check work both enqueue. Reboot device — BootReceiver schedules cleanly.

Note

`scheduleBackgroundUpdateChecks` in `GithubStoreApp` was already wrapped in `try / Logger.e` so the same throw on cold start logged but didn't crash the app. The crash report came from BootReceiver's narrower error path.

Summary by CodeRabbit

  • Bug Fixes
    • Added error handling for background task scheduling to prevent crashes during boot and system events.
    • Optimized work request scheduling to reduce delays and dispatch tasks more efficiently when network becomes available.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3c5f9d1a-b601-4385-a83c-856c3eaad1aa

📥 Commits

Reviewing files that changed from the base of the PR and between 333db97 and 28cc7fa.

📒 Files selected for processing (2)
  • core/data/src/androidMain/kotlin/zed/rainxch/core/data/services/BootReceiver.kt
  • core/data/src/androidMain/kotlin/zed/rainxch/core/data/services/UpdateScheduler.kt

Walkthrough

The pull request refactors work scheduling to improve robustness. UpdateScheduler.schedule() now builds immediate work requests without initial delay or expedited configuration. BootReceiver adds error handling to catch and log scheduling exceptions, ensuring the broadcast receiver completes without crashing.

Changes

Work Scheduling Configuration and Error Handling

Layer / File(s) Summary
Immediate Work Request Configuration
core/data/src/androidMain/kotlin/zed/rainxch/core/data/services/UpdateScheduler.kt
UpdateScheduler.schedule() constructs immediate OneTimeWorkRequest without initial delay or expedited configuration, replacing prior cold-start backoff behavior with constraint-based dispatch.
Scheduling Error Handling
core/data/src/androidMain/kotlin/zed/rainxch/core/data/services/BootReceiver.kt
BootReceiver wraps reboot-based scheduling and cancellation in catch (Throwable) to log failures as errors instead of propagating exceptions, preserving pendingResult.finish() via finally.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 The scheduler hops with newfound grace,
No expedited rush to win the race,
And when things stumble in the dark,
The Boot Receiver leaves a log-and-mark—
No crashes here, just graceful ways,
To reschedule work for brighter days!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: removing an initial delay from expedited WorkManager requests to prevent a crash, and notes it as a hotfix.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/scheduler-expedited-crash

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@rainxchzed rainxchzed merged commit 9dbce7a into main May 8, 2026
1 check passed
@rainxchzed rainxchzed deleted the fix/scheduler-expedited-crash branch May 8, 2026 14:07
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.

1 participant