Skip to content

Commit c832d2d

Browse files
authored
Fix book/website preview crash from reused closed SassCache KV handle (#14716)
* Fix book/website preview crash from reused closed SassCache KV handle quarto preview on a book or website project crashed with "BadResource: Bad resource ID" when re-rendering pages with a theme plus custom CSS. #13955 fixed the single-file preview path by no longer tearing down the project context between re-renders, but book and website preview go through the serve/watch path, which still calls project.cleanup() (and thus temp.cleanup()) on each config refresh. The underlying defect was never addressed: SassCache.cleanup() closed the Deno.Kv handle but left the stale SassCache in the _sassCache registry, so a later resolve of the same cache path handed back an instance whose KV handle was already closed. Dropping the registry entry when the handle is closed makes every render path open a fresh handle instead. Fixes #14594. * Add manual preview test fixture for Sass cache reuse (#14594) Book preview crashing on Sass recompile after a config-refresh cleanup is an interactive serve/watch-path bug tests/unit/sass-cache.test.ts can't reach. Add sass-cache-crash-14594/ (theme + custom css book) and a Test Matrix section in tests/docs/manual/preview/README.md (T34-T36) so /quarto-preview-test can drive the regression check, matching the sibling #14593 brand-detection manual test.
1 parent bcca58a commit c832d2d

11 files changed

Lines changed: 186 additions & 0 deletions

File tree

‎news/changelog-1.10.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ All changes included in 1.10:
8383
- ([#14281](https://github.com/quarto-dev/quarto-cli/issues/14281)): Avoid creating a duplicate `.quarto_ipynb` file on preview startup for single-file Jupyter documents.
8484
- ([#14533](https://github.com/quarto-dev/quarto-cli/issues/14533)): Fix `quarto preview` not detecting a frontmatter `format:` change until the second render request. The first request after the edit now correctly restarts the preview process with the new format.
8585
- ([#14593](https://github.com/quarto-dev/quarto-cli/issues/14593)): Fix `quarto preview` ignoring a `_brand.yml` added or removed while the preview is running. The brand change is now applied on the next render instead of requiring a preview restart.
86+
- ([#14594](https://github.com/quarto-dev/quarto-cli/issues/14594)): Fix `quarto preview` of a book or website project crashing with `BadResource: Bad resource ID` when re-rendering pages.
8687

8788
### `install`
8889

‎src/core/sass/cache.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ class SassCache implements Cloneable<SassCache> {
155155
log.info(
156156
`Error occurred during sass cache cleanup for ${this.path}: ${error}`,
157157
);
158+
} finally {
159+
// Drop the registry entry so a later resolve of the same path opens a
160+
// fresh KV handle instead of reusing this now-closed one (#14594, #13955).
161+
delete _sassCache[cachePath];
158162
}
159163
});
160164
}

‎tests/docs/manual/preview/README.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,48 @@ brand signal in the kept `report.typ` is the line
275275
- **Expected:** Brand stays applied; the source-state token matches so the cache is reused (only cheap stat calls per resolve)
276276
- **Catches:** A guard that re-loads the brand on every render even when nothing changed
277277

278+
## Test Matrix: Sass Cache Reuse (#14594)
279+
280+
After every change to `SassCache.cleanup()` or the `_sassCache` registry in
281+
`src/core/sass/cache.ts`, verify that a book/website preview survives repeated
282+
re-renders instead of crashing with `BadResource: Bad resource ID`. On each
283+
re-render the serve/watch reload path runs `refreshProjectConfig`
284+
(`src/project/serve/watch.ts`, ~line 277 for HTML; also on config changes,
285+
~line 305), which calls `project.cleanup()` and closes the session Sass KV
286+
handle; the next render must open a fresh handle rather than reuse the closed
287+
one. Fixture: `sass-cache-crash-14594/` (a flat multi-chapter book with
288+
`theme: cosmo` + `css:`, which routes the theme through the session Sass cache);
289+
drive it with the `/quarto-preview-test` workflow. Deterministic unit coverage
290+
lives in `tests/unit/sass-cache.test.ts` — this matrix is the interactive
291+
regression guide that path can't cover.
292+
293+
### P1: Critical
294+
295+
#### T34: Book project preview — `_quarto.yml` edit does not crash on Sass recompile
296+
297+
- **Setup:** `sass-cache-crash-14594/` book (`theme: cosmo`, `css: styles.css`), no prior `.quarto/` or `_book/`
298+
- **Steps:** From the fixture dir, `quarto preview --no-browser --port XXXX`; after the first render, edit `_quarto.yml` (tweak the title or append a comment line) and save 3-5 times, watching the preview log
299+
- **Expected:** Every re-render completes and reloads. No `BadResource: Bad resource ID` in the log. The cosmo theme + custom CSS keep recompiling/serving.
300+
- **Catches:** `SassCache.cleanup()` closing the KV handle but leaving the stale instance in `_sassCache`, so the next resolve reuses a closed handle (#14594, #13955)
301+
302+
### P2: Important
303+
304+
#### T35: Book project preview — chapter content edit (content re-render path)
305+
306+
- **Setup:** Same fixture
307+
- **Steps:** `quarto preview --no-browser --port XXXX`; after the first render, edit a chapter body (`index.qmd` / `chapter1.qmd`) and save 3-5 times
308+
- **Expected:** Same as T34 — no crash, pages keep reloading
309+
- **Catches:** The HTML reload path (`refreshProjectConfig` at watch.ts ~277) closes the session KV on a plain content re-render too, so a content save must not reuse a closed handle either
310+
311+
### Regression Guard
312+
313+
#### T36: Single-file preview — unaffected by the registry cleanup
314+
315+
- **Setup:** `plain.qmd` (markdown-only, no project)
316+
- **Steps:** `quarto preview plain.qmd --no-browser --port XXXX`, save 3-5 times
317+
- **Expected:** No crash and no behavior change — the fix is a no-op for the common single-file preview path
318+
- **Catches:** The registry-entry deletion in cleanup accidentally breaking or churning the non-project preview path
319+
278320
## Extension Format PDF Preview (#14582)
279321

280322
### P1: Core functionality
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.quarto/
2+
/_book/
3+
**/*_files/
4+
**/*.quarto_ipynb
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Sass cache reuse during preview (#14594)
2+
3+
Manual fixture for the bug where `quarto preview` of a book (or website) project
4+
crashed with `BadResource: Bad resource ID` while recompiling the theme Sass on a
5+
re-render. `SassCache.cleanup()` closed the session `Deno.Kv` handle but left the
6+
stale instance in the module-level `_sassCache` registry, so the next
7+
`sassCache(path, temp)` resolve reused the now-closed handle.
8+
9+
On each re-render the serve/watch reload path runs `refreshProjectConfig`
10+
(`src/project/serve/watch.ts`, ~line 277 for HTML; also on any config change,
11+
~line 305), which calls `project.cleanup()` and closes that session Sass KV
12+
handle. The next render must open a fresh handle. RStudio's and Positron's live
13+
preview hit the same persistent server, so they saw the same crash.
14+
15+
Drive this with the `/quarto-preview-test` workflow. See the parent `../README.md`
16+
"Test Matrix: Sass Cache Reuse (#14594)" for the T34-T36 cases.
17+
18+
## Files
19+
20+
| File | Role |
21+
|------|------|
22+
| `_quarto.yml` | Flat multi-chapter book with `theme: cosmo` + `css: styles.css` (routes the theme through the session Sass cache) |
23+
| `index.qmd`, `chapter1.qmd`, `chapter2.qmd` | Minimal chapters; a project preview re-renders these |
24+
| `styles.css` | Custom CSS so a real Sass compile runs alongside the cosmo theme |
25+
26+
`_book/` and `.quarto/` are scratch produced by a run and are git-ignored.
27+
28+
## Crash signal
29+
30+
Before the fix, after the first re-render the preview log shows:
31+
32+
```
33+
BadResource: Bad resource ID
34+
```
35+
36+
and pages stop reloading. After the fix, every re-render completes and reloads.
37+
38+
Deterministic unit coverage lives in `tests/unit/sass-cache.test.ts`; this fixture
39+
is the interactive regression guide the unit test can't cover (live serve/watch
40+
path).
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
project:
2+
type: book
3+
4+
book:
5+
title: "Sass Cache Preview Crash (#14594)"
6+
chapters:
7+
- index.qmd
8+
- chapter1.qmd
9+
- chapter2.qmd
10+
11+
format:
12+
html:
13+
theme: cosmo
14+
css: styles.css
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Chapter 1
3+
---
4+
5+
# Chapter 1
6+
7+
Content.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Chapter 2
3+
---
4+
5+
# Chapter 2
6+
7+
Content.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Home
3+
---
4+
5+
# Welcome
6+
7+
Edit this line (or `_quarto.yml`) and save to force preview re-renders.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* Custom CSS so a real session Sass compile runs alongside the cosmo theme. */
2+
body { color: rebeccapurple; }

0 commit comments

Comments
 (0)