Commit 758a720
fix: prevent crash on boot in airplane mode (#218)
* fix: prevent crash on boot in airplane mode
Strimma crashed on every boot when the device had no network. The foreground
service entered an infinite restart loop because Ktor's CIO engine surfaces
DNS lookup failures as `java.nio.channels.UnresolvedAddressException`, which
extends `IllegalArgumentException` — NOT `IOException`. The 14 catch blocks
across the five HTTP-touching clients only caught `IOException`, so the
exception escaped the coroutine launched by `SyncOrchestrator.start()` on
`Dispatchers.Main` and killed the process.
Widen the catch in every HTTP-touching client to `Exception` (with
`@Suppress("TooGenericExceptionCaught")` and a rationale comment), preserving
the more specific `SerializationException` / `SQLiteException` catches above
it for differentiated error logging. Affects:
- NightscoutClient.kt (4 sites)
- NightscoutPuller.kt (1 site)
- TreatmentSyncer.kt (2 sites)
- LibreLinkUpClient.kt (4 sites)
- TidepoolClient.kt (3 sites)
Add regression tests using Ktor `MockEngine` to throw
`UnresolvedAddressException` directly from the request handler and assert
each client surfaces the failure as a return value (or, for `fetchTreatments`,
rethrows for the caller to catch) rather than propagating uncaught.
Verified on device: with airplane mode on, Strimma now boots cleanly into
the foreground notification instead of looping fatal restarts.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
* refactor: address review feedback for airplane-mode crash fix
Extract `withNetworkBoundary` helper consolidating the catch policy across
12 of 14 sites. The two exceptions stay manual: TreatmentSyncer.syncSince
keeps per-type IntegrationStatus.Error differentiation (Sync failed / DB
error / network), and NightscoutClient.fetchTreatments keeps the rethrow
contract that callers depend on.
Add CoroutineExceptionHandler to StrimmaService.scope, NightscoutPusher.scope,
and TidepoolUploader.scope as defense in depth — anything that escapes the
leaf catches now logs and is swallowed instead of crashing the foreground
service. The leaf catches stay as primary mechanism; the handlers are the belt.
Include the exception class name in every catch log so a real programming bug
(NPE, ISE, IAE) is distinguishable from a transient network failure when
triaging from DebugLog.
Fix LibreLinkUpClient and TidepoolClient test seams: both now close the
production CIO HttpClient before swapping in the MockEngine-backed test client
(consistent with NightscoutClient — previously TidepoolClient leaked one CIO
engine per test instance).
Add airplane-mode regression tests:
- LibreLinkUpClientAirplaneModeTest (4 tests covering all widened catches)
- NightscoutPullerAirplaneModeTest (1 test, throwing FakeClient)
- TreatmentSyncerAirplaneModeTest (1 test using airplane-mode NightscoutClient)
- TidepoolClientAirplaneModeTest.createDataset (3rd test, closes the matrix)
Replace nested runBlocking + assertThrows with try/catch + fail() in the
fetchTreatments rethrow test (assertFailsWith would need kotlin-test on the
classpath).
Net diff: +146 / -269 lines.
Punted to follow-up: moving SyncOrchestrator off Dispatchers.Main onto a
dedicated IO scope. Independent of the changes here.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>1 parent 58c2a32 commit 758a720
14 files changed
Lines changed: 589 additions & 209 deletions
File tree
- app/src
- main/java/com/psjostrom/strimma
- network
- service
- tidepool
- test/java/com/psjostrom/strimma
- network
- tidepool
Lines changed: 19 additions & 43 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | 14 | | |
16 | | - | |
17 | 15 | | |
18 | | - | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
| |||
117 | 114 | | |
118 | 115 | | |
119 | 116 | | |
120 | | - | |
| 117 | + | |
121 | 118 | | |
122 | 119 | | |
123 | 120 | | |
| |||
127 | 124 | | |
128 | 125 | | |
129 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
130 | 137 | | |
131 | 138 | | |
132 | 139 | | |
| |||
146 | 153 | | |
147 | 154 | | |
148 | 155 | | |
149 | | - | |
| 156 | + | |
150 | 157 | | |
151 | 158 | | |
152 | 159 | | |
153 | 160 | | |
154 | 161 | | |
155 | 162 | | |
156 | 163 | | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | 164 | | |
166 | 165 | | |
167 | 166 | | |
| |||
203 | 202 | | |
204 | 203 | | |
205 | 204 | | |
206 | | - | |
| 205 | + | |
207 | 206 | | |
208 | 207 | | |
209 | 208 | | |
210 | 209 | | |
211 | 210 | | |
212 | | - | |
| 211 | + | |
213 | 212 | | |
214 | 213 | | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | 214 | | |
224 | 215 | | |
225 | 216 | | |
226 | 217 | | |
227 | | - | |
| 218 | + | |
228 | 219 | | |
229 | 220 | | |
230 | 221 | | |
231 | 222 | | |
232 | 223 | | |
233 | | - | |
| 224 | + | |
234 | 225 | | |
235 | 226 | | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | 227 | | |
245 | 228 | | |
246 | 229 | | |
247 | 230 | | |
248 | | - | |
249 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
250 | 234 | | |
251 | 235 | | |
252 | 236 | | |
| |||
266 | 250 | | |
267 | 251 | | |
268 | 252 | | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | 253 | | |
278 | 254 | | |
279 | 255 | | |
| |||
Lines changed: 41 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 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
Lines changed: 42 additions & 88 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
| |||
131 | 130 | | |
132 | 131 | | |
133 | 132 | | |
134 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
135 | 137 | | |
136 | 138 | | |
137 | 139 | | |
| |||
140 | 142 | | |
141 | 143 | | |
142 | 144 | | |
143 | | - | |
| 145 | + | |
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
147 | 149 | | |
148 | 150 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | 151 | | |
162 | 152 | | |
163 | 153 | | |
| |||
179 | 169 | | |
180 | 170 | | |
181 | 171 | | |
182 | | - | |
| 172 | + | |
183 | 173 | | |
184 | 174 | | |
185 | 175 | | |
| |||
192 | 182 | | |
193 | 183 | | |
194 | 184 | | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | 185 | | |
208 | 186 | | |
209 | 187 | | |
| |||
219 | 197 | | |
220 | 198 | | |
221 | 199 | | |
222 | | - | |
| 200 | + | |
223 | 201 | | |
224 | 202 | | |
225 | 203 | | |
| |||
231 | 209 | | |
232 | 210 | | |
233 | 211 | | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | 212 | | |
247 | 213 | | |
248 | 214 | | |
| |||
258 | 224 | | |
259 | 225 | | |
260 | 226 | | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
| 227 | + | |
| 228 | + | |
281 | 229 | | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
287 | 244 | | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
305 | 264 | | |
306 | 265 | | |
307 | 266 | | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | 267 | | |
314 | 268 | | |
315 | 269 | | |
| |||
0 commit comments