Commit d857e20
refactor: move SyncOrchestrator off Dispatchers.Main onto a dedicated IO scope (#220)
* refactor: move SyncOrchestrator off Dispatchers.Main onto a dedicated IO scope
Previously `SyncOrchestrator` inherited the foreground service's Main scope. Two
problems with that:
- A slow Room query or HTTP retry inside a `scope.launch` block runs on Main
and blocks the UI.
- Any exception escaping a network/DB catch hits Android's default Main-thread
uncaught handler and kills the foreground service. The fix in #218 widened
every leaf catch in the network clients, but the underlying architectural
smell — sync work running on Main — remained.
`SyncOrchestrator` now owns its own `CoroutineScope(SupervisorJob() +
Dispatchers.IO + handler)`, mirroring the pattern in `NightscoutPusher` and
`TidepoolUploader`. The `start()` signature loses its `scope` parameter; the
service no longer leaks its Main scope into background work. `stop()` cancels
the internal scope so a service shutdown propagates correctly.
A `CoroutineExceptionHandler` is installed on the new scope as defense in
depth — independent from the per-scope handlers in #218 (which protect
StrimmaService/Pusher/Uploader scopes; this one protects the new sync scope).
Test plan:
- `./gradlew testDebugUnitTest` — full suite green
- `./gradlew detekt` — clean
- `./gradlew assembleRelease` — clean
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: extract scopeCrashHandler factory + idempotence guard + tests
Address review feedback for #220 (now that #218 is merged to main):
- Extract `scopeCrashHandler(name)` factory in `receiver/`. Replaces 4
near-identical `CoroutineExceptionHandler { _, t -> DebugLog.log(...) }`
blocks across StrimmaService, NightscoutPusher, TidepoolUploader, and
SyncOrchestrator. One log format, one truncation length, one source.
- Add idempotence guard to `SyncOrchestrator.start()` (mirrors UpdateChecker's
`if (checkJob != null) return` pattern). Stray double-call no longer
duplicates the periodic loops or stacks two DataStore collectors. `started`
is `@VisibleForTesting`-exposed for the new test class.
- Reorder `SyncOrchestrator.stop()` and add a comment explaining why
`updateChecker.stop()` MUST be called explicitly (its internal `checkJob`
field needs nulling — `scope.cancel()` cancels the launched coroutine but
leaves the Job reference live, which breaks the next `start()`).
- Trim redundant `treatmentSyncJob?.cancel()` / `exerciseSyncJob?.cancel()` in
`stop()` — they're children of `scope` and get cancelled transitively.
Keep the `= null` reassignments for clarity.
- Add `@Volatile` to `var scope` and `var job` so a future cross-thread caller
can't observe a torn write.
- Rewrite KDoc to stop overselling — CalendarPoller still runs on Main scope.
Comment the asymmetry at the call site in StrimmaService.
- Update `@Suppress("LongParameterList")` rationale to acknowledge the
dispatcher param is plumbing, not a collaborator.
New `SyncOrchestratorTest` covers: idempotent start, scope-rebuild re-entry
after stop, stop-without-start safety. Uses real collaborators across the
graph; `SilentNightscoutClient` returns null/empty so launches exit cleanly
without real HTTP.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>1 parent 38725e0 commit d857e20
6 files changed
Lines changed: 268 additions & 39 deletions
File tree
- app/src
- main/java/com/psjostrom/strimma
- network
- receiver
- service
- tidepool
- test/java/com/psjostrom/strimma/service
Lines changed: 3 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 33 | + | |
| 34 | + | |
38 | 35 | | |
39 | 36 | | |
40 | 37 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
Lines changed: 7 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
107 | 103 | | |
108 | 104 | | |
109 | 105 | | |
| |||
189 | 185 | | |
190 | 186 | | |
191 | 187 | | |
192 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
193 | 191 | | |
194 | 192 | | |
195 | 193 | | |
| |||
Lines changed: 60 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| 14 | + | |
12 | 15 | | |
13 | 16 | | |
14 | 17 | | |
| 18 | + | |
15 | 19 | | |
16 | 20 | | |
| 21 | + | |
| 22 | + | |
17 | 23 | | |
18 | 24 | | |
19 | 25 | | |
| |||
24 | 30 | | |
25 | 31 | | |
26 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
27 | 37 | | |
28 | | - | |
| 38 | + | |
29 | 39 | | |
30 | 40 | | |
31 | 41 | | |
| |||
36 | 46 | | |
37 | 47 | | |
38 | 48 | | |
39 | | - | |
| 49 | + | |
| 50 | + | |
40 | 51 | | |
41 | 52 | | |
42 | 53 | | |
| |||
45 | 56 | | |
46 | 57 | | |
47 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
48 | 69 | | |
49 | 70 | | |
50 | 71 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
56 | 87 | | |
57 | 88 | | |
58 | 89 | | |
| |||
62 | 93 | | |
63 | 94 | | |
64 | 95 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
70 | 103 | | |
71 | 104 | | |
72 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
73 | 116 | | |
74 | 117 | | |
75 | | - | |
| 118 | + | |
76 | 119 | | |
77 | 120 | | |
78 | 121 | | |
| |||
82 | 125 | | |
83 | 126 | | |
84 | 127 | | |
85 | | - | |
| 128 | + | |
86 | 129 | | |
87 | 130 | | |
88 | 131 | | |
| |||
92 | 135 | | |
93 | 136 | | |
94 | 137 | | |
95 | | - | |
| 138 | + | |
96 | 139 | | |
97 | 140 | | |
98 | 141 | | |
| |||
119 | 162 | | |
120 | 163 | | |
121 | 164 | | |
122 | | - | |
| 165 | + | |
123 | 166 | | |
124 | 167 | | |
125 | 168 | | |
| |||
Lines changed: 4 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | | - | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
76 | 73 | | |
77 | 74 | | |
78 | 75 | | |
| |||
0 commit comments