Repository navigation
Commit 977e207
committed
fix(models): [#1052 D14 review-fix M3] don't crash on non-JSON error body + fire refresh at boot
Two consensus-review findings on the D14 commit (`248eaaf86c`):
1. Pre-existing crash: `ModelsDev.Data()`'s `Flock.withLock` branch calls
`JSON.parse(result2.text)` unconditionally after `fetchApi()` returns —
even when `result2.ok === false` and the body is an HTML 5xx error page.
That throws `SyntaxError` and blocks model initialization. Bug existed
before D14 but was rarely exposed because the eager import-time refresh()
warmed the disk cache first, so most subsequent `Data()` calls returned via
`readJson`. Post-D14 more first-calls fall through to fetch, so the crash
window widens. Fix: return `{}` when `!result2.ok` — callers already tolerate
an empty catalog (Provider.state produces no models.dev-derived entries,
same UX as `OPENCODE_DISABLE_MODELS_FETCH=1`).
2. Reintroduce a boot-time refresh without holding the event loop: 4/6
reviewers flagged the loss of "fresh at boot" as a real regression for
short-lived commands. Fix: `Promise.resolve().then(() => refresh().catch())`.
A microtask can't itself keep Bun alive, and if the fetch it schedules is
still in flight at process-exit the snapshot covers callers on the next
run. Preserves the load-bearing part of the D14 fix (no `setTimeout` keeping
the loop alive) while restoring near-immediate cache warming. If this ever
reintroduces the unshare-net sanity hang on CI, drop the Promise.then line —
the snapshot alone keeps release binaries functional offline.
Also fixes the misleading comment "always have model metadata even on a
cold-start with no network" — accurate for release binaries but false in
dev-mode where the snapshot isn't embedded. Reworded to say so.
Verified: typecheck clean; `--version` exits in ~1s cleanly; existing D12
phase-label test still passes.1 parent c237795 commit 977e207
1 file changed
Lines changed: 29 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
129 | 136 | | |
130 | 137 | | |
131 | 138 | | |
| |||
164 | 171 | | |
165 | 172 | | |
166 | 173 | | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
174 | 188 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
180 | 192 | | |
181 | 193 | | |
182 | 194 | | |
| |||
0 commit comments