Repository navigation
Cap metadata string field lengths - #355
Merged
Merged
Conversation
kahrendt
marked this pull request as ready for review
November 10, 2026 16:13
maximmaxim345
approved these changes
Nov 11, 2026
maximmaxim345
left a comment
Member
There was a problem hiding this comment.
LGTM.
I think 2048 bytes for artwork_url is fine, since a client that doesn't use it can skip it while parsing and never has to buffer it.
kahrendt
added a commit
to Sendspin/sendspin-cpp
that referenced
this pull request
Nov 11, 2026
… fields (#258) ## Summary `server/state` metadata still copied every string onto the heap through `operator new`, which aborts on ESPHome. Sendspin/spec#355 caps `title`, `artist`, `album_artist` and `album` at 255 bytes and `artwork_url` at 2048, so the text now lives in fixed blocks allocated at `start()` and a metadata state allocates nothing. Part of Sendspin/backlog#92. **Behaviour change:** a value over its cap is cut at a UTF-8 boundary, with one warning per connection. `METADATA_STRICT` (in `protocol.cpp`, `false` for now) later turns that into a protocol error that closes the connection, as #256 staged for `group_id`. ## Change - **Field opt-in:** `MetadataRoleConfig` has one bool per text field (all `true` by default) and a `buffer_location`; `add_metadata(MetadataRoleConfig config = {})` keeps the no-argument call. An unkept field is dropped at parse and takes no storage, e.g. `{.artwork_url = false}` saves 2,048 B per block. - **Three text blocks** (9,204 B with every field, 3,060 B without the URL), PSRAM-preferring, allocated at `start()`: the slot's two places and the main loop's current state. A `start()` that cannot allocate them fails like artwork's. - **Hand-off:** the protocol task copies an arrival's kept text into its slot place under the Inbox mutex (at most 3,068 B, the largest Inbox critical section). The main loop peeks at the timestamps, decides what is due with no lock held, and commits only if nothing arrived since; the public strings are filled from the current block outside the lock through `ReservedText`, reserved per kept field when the role is built. - **Scheduled states wait in the slot** instead of on the main loop. `ScheduledStateSlot` (`src/scheduled_state_slot.h`) holds that protocol, and the color role now uses it too, with unchanged behaviour. One interleaving shifts: an arrival during a callback replaces a pending state that falls due in that same callback, the same outcome as the arrival landing just before the drain. ## Tests - Parse tables at both caps with `Control:` rows (cap, cap+1, 2/3/4-byte characters straddling it, lenient and strict) and a kept-fields table; an end-to-end check of kept and dropped fields. - `MetadataSlot.ThreeBlocksCarryEveryStateToTheListenerInOrder`: a white-box table asserting after every step that the three blocks are a permutation, plus the delivered and pending titles, including a re-peek, cleanup, re-entrant `stop()` and restart. `ScheduledStateSlot` tests cover the decision table, generations, deciding with the Inbox mutex free and the slot's dirty bit. - Alloc window for metadata states (an over-cap one, a scheduled one delivered later, one without text): 0. Mutants caught include a commit ignoring the sequence, a deadline decided under the lock, block aliasing, a missing re-peek, the same-tick loop removed and a settle/commit/reset polarity flip. - Gaps: the strict close path and a failed block allocation at `start()` need a production seam. Stack (ESP-IDF 5.5.4, esp32): protocol task 6,512 B -Os / 6,624 B -Og (was 6,544 / 6,688), within its 7,168 B default; every other task unchanged. `tools/stack_usage/README.md` updated.
1 of 9 tasks
maximmaxim345
added a commit
to Sendspin/aiosendspin
that referenced
this pull request
Nov 23, 2026
# What does this implement/fix? <!-- Quick description and explanation of changes. --> A track title, artist, album artist or album over 255 bytes in UTF-8, or an artwork URL over 2048 bytes, made a conforming client close the connection. The server now cuts the text fields at a character boundary and leaves out an over-long artwork URL, since a cut URL would point nowhere. Applications still get the full values in their state and events, and a debug log names each capped field. **Related issue or spec PR (if applicable):** - Sendspin/spec#355 ## Types of changes <!-- Tick exactly one box. CI (.github/workflows/pr-labels.yaml) applies the matching label, and the release notes use it to sort this change. --> - [x] Bugfix (non-breaking change which fixes an issue): `bugfix` - [ ] New feature (non-breaking change which adds functionality): `new-feature` - [ ] Enhancement to an existing feature: `enhancement` - [ ] Breaking change (changes the public API or the protocol in a way that is not backwards compatible): `breaking-change` - [ ] Refactor (no behavior change): `refactor` - [ ] Documentation only: `documentation` - [ ] Maintenance / chore: `maintenance` - [ ] CI / workflow change: `ci` - [ ] Dependencies bump: `dependencies`
53 of 58 tasks
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
metadatastring fields have no length limit, so a constrained client can't size its buffers ahead of time and has to allocate on the heap at runtime.This caps
title,artist,album_artistandalbumat 255 bytes in UTF-8, andartwork_urlat 2048 bytes. 255 bytes is too short for some artwork URLs, such as image-proxy URLs that wrap a percent-encoded source URL, or signed CDN URLs. A value over its cap falls under the out-of-range rule from #347, so the client closes the connection.I'm not entirely sure if 2048 bytes is a good limit for the artwork URL. It is definitely on the safe side, but it does take more space than all the other fields combined. It's not a big deal in general, though, because a memory conscious client could be careful about this and pre-register which fields they want and discard the rest, so, for example, ESPHome could just say I want title and it would only reserve 255 bytes ahead of time and everything else in metadata is thrown away (this is not a spec idea, this is just a library implementation detail).
Closes #341