Skip to content

Cap metadata string field lengths - #355

Merged
maximmaxim345 merged 1 commit into
mainfrom
metadata-string-limits
Nov 11, 2026
Merged

maximmaxim345 merged 1 commit into
mainfrom
metadata-string-limits

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The metadata string 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_artist and album at 255 bytes in UTF-8, and artwork_url at 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

@maximmaxim345 maximmaxim345 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@maximmaxim345
maximmaxim345 merged commit 4e787fb into main Nov 11, 2026
1 check passed
@maximmaxim345
maximmaxim345 deleted the metadata-string-limits branch November 11, 2026 09:33
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.
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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potentially set a maximum string length for metadata fields

2 participants