From 663bab82270d8e89f333325c6a0982f0b52fcec0 Mon Sep 17 00:00:00 2001 From: konstin <6826232+konstin@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:02:10 +0100 Subject: [PATCH 1/2] Run CSS queries on the calling thread's stack The :has() descendant walk is iterative. Drop stacker and its build dependencies rather than switching stacks to accommodate artificially small workers. Keep testing the selector limits on a default worker stack. --- Cargo.lock | 85 ------------------------------- Cargo.toml | 1 - docs/parsing.md | 7 ++- src/selector.rs | 16 ------ tests/test_selector_robustness.py | 14 ++--- 5 files changed, 8 insertions(+), 115 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f1b914..2fbc9c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "ar_archive_writer" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73cd58deff2140a0a8eae87e417bd01db68a33e148aa93d1e8cd837e55e312b6" -dependencies = [ - "object", -] - [[package]] name = "astral-html" version = "0.0.3" @@ -29,7 +20,6 @@ dependencies = [ "precomputed-hash", "pyo3", "selectors", - "stacker", ] [[package]] @@ -38,22 +28,6 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" -[[package]] -name = "cc" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f74872d07caf508b30a21f6836e7d7016a2eaf7d9ff4f48deaa58cd8a0407630" -dependencies = [ - "find-msvc-tools", - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" - [[package]] name = "cssparser" version = "0.38.0" @@ -119,12 +93,6 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" -[[package]] -name = "find-msvc-tools" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" - [[package]] name = "heck" version = "0.5.0" @@ -161,15 +129,6 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" -[[package]] -name = "object" -version = "0.39.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e5a6c098c7a3b6547378093f5cc30bc54fd361ce711e05293a5cc589562739b" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.21.4" @@ -240,16 +199,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "psm" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd034599e63b970727f70d79e02d62390a4a84f7c6b827c27c46d5ac3fa622" -dependencies = [ - "ar_archive_writer", - "cc", -] - [[package]] name = "pyo3" version = "0.29.3" @@ -392,12 +341,6 @@ dependencies = [ "stable_deref_trait", ] -[[package]] -name = "shlex" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" - [[package]] name = "siphasher" version = "1.0.4" @@ -416,19 +359,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" -[[package]] -name = "stacker" -version = "0.1.25" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707f49d46706bacf8a2b00d51dace3f9de527c13eec3778f570c411f89e69967" -dependencies = [ - "cc", - "cfg-if", - "libc", - "psm", - "windows-sys", -] - [[package]] name = "syn" version = "2.0.119" @@ -462,18 +392,3 @@ name = "unicode-ident" version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" - -[[package]] -name = "windows-link" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" - -[[package]] -name = "windows-sys" -version = "0.61.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" -dependencies = [ - "windows-link", -] diff --git a/Cargo.toml b/Cargo.toml index a4b3a52..0e288c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,6 @@ pyo3 = "0.29" selectors = "0.41" cssparser = "0.38" precomputed-hash = "0.1" -stacker = "0.1.25" [profile.release] lto = "thin" diff --git a/docs/parsing.md b/docs/parsing.md index 0884462..a7e7001 100644 --- a/docs/parsing.md +++ b/docs/parsing.md @@ -127,8 +127,11 @@ and `InvalidSelectorError` inherit from `ValueError`. Servo parses CSS, but its matching callbacks cannot return an error. The extension walks the parsed selectors itself so it can propagate a limit error with Rust’s `Result`. Builders can choose `panic = "abort"` or `panic = "unwind"` -and get the same Python exception. On small Python worker stacks, `stacker` -provides room for recursive selector matching. +and get the same Python exception. + +CSS parsing and matching recurse on the calling thread's stack. Avoid shrinking +Python worker stacks: on a 64 KiB stack, a selector within the depth limits +can still overflow the stack and crash Python. Applications processing untrusted pages should also bound response size and concurrent parses. The tokenizer reads a start tag and its attributes even diff --git a/src/selector.rs b/src/selector.rs index 1c6307b..636f44e 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -128,22 +128,6 @@ pub fn select( root: Option, css: &str, first: bool, -) -> Result, SelectError> { - // Reuse the caller's stack when at least 1 MiB remains. Otherwise request - // 2 MiB so parsing, matching and dropping nested selectors have headroom - // even on the small Python worker stacks in test_selector_robustness.py. - stacker::maybe_grow(1024 * 1024, 2 * 1024 * 1024, || { - select_with_stack(tree, root, css, first) - }) -} - -// Keep the matcher's frame inside the guarded closure even under optimization. -#[inline(never)] -fn select_with_stack( - tree: &Tree, - root: Option, - css: &str, - first: bool, ) -> Result, SelectError> { if css.len() > MAX_SELECTOR_BYTES { return Err(SelectError::ResourceLimit(SELECTOR_SIZE_ERROR.to_owned())); diff --git a/tests/test_selector_robustness.py b/tests/test_selector_robustness.py index aeda4f8..68867b9 100644 --- a/tests/test_selector_robustness.py +++ b/tests/test_selector_robustness.py @@ -1,15 +1,14 @@ import subprocess import sys -import threading from concurrent.futures import ThreadPoolExecutor import pytest from astral_html import ResourceLimitError, parse -def test_selector_limits_on_a_small_worker_stack() -> None: - # A native stack overflow aborts Python. Run the boundary cases together - # on a small worker stack, including the ones expected to succeed. +def test_selector_limits_on_a_worker_thread() -> None: + # Isolate native stack overflows from pytest. Exercise the platform's + # default worker stack, which can be smaller than the main thread's. subprocess.run( [sys.executable, "-X", "faulthandler", __file__], timeout=30, check=True ) @@ -298,12 +297,5 @@ def test_selector_cache_memory( if __name__ == "__main__": - # If a platform disallows both small sizes, still test its default stack. - for stack_size in (65536, 131072): - try: - threading.stack_size(stack_size) - except RuntimeError, ValueError: - continue - break with ThreadPoolExecutor(max_workers=1) as worker: worker.submit(exercise_stack_limits).result() From 43e5bd3283df8c41e8c64599a8aeb02a11cec0c8 Mon Sep 17 00:00:00 2001 From: konstin <6826232+konstin@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:02:11 +0100 Subject: [PATCH 2/2] Prepare astral-html 0.0.2 --- CHANGELOG.md | 7 +++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- pyproject.toml | 2 +- uv.lock | 2 +- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d6d8dc..fc65f7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.0.2 + +- Remove `stacker` and nine transitive Rust dependencies from source builds. + CSS queries now use the calling thread's stack. Deeply nested selectors can + crash Python on unusually small worker stacks (e.g. 64 KiB). +- Pin the benchmark charts on PyPI to the release commit. + ## 0.0.1 - Parse HTML from Python and query elements with CSS selectors. diff --git a/Cargo.lock b/Cargo.lock index 2fbc9c1..4175ce8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,7 +13,7 @@ dependencies = [ [[package]] name = "astral-html-python" -version = "0.0.1" +version = "0.0.2" dependencies = [ "astral-html", "cssparser", diff --git a/Cargo.toml b/Cargo.toml index 0e288c9..3fb4ccd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "astral-html-python" -version = "0.0.1" +version = "0.0.2" edition = "2024" rust-version = "1.97" description = "A Rust-backed HTML parser and CSS selector engine for Python" diff --git a/pyproject.toml b/pyproject.toml index 179a42a..a85c845 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "astral-html" -version = "0.0.1" +version = "0.0.2" description = "A Rust-backed HTML parser and CSS selector engine for Python" requires-python = ">=3.14" license = "MIT OR Apache-2.0" diff --git a/uv.lock b/uv.lock index fedcb85..e112481 100644 --- a/uv.lock +++ b/uv.lock @@ -8,7 +8,7 @@ exclude-newer-span = "P7D" [[package]] name = "astral-html" -version = "0.0.1" +version = "0.0.2" source = { editable = "." } [package.dev-dependencies]