Stream metadata.config decoding in chunks and increase max size to 1024KB#181
Merged
Stream metadata.config decoding in chunks and increase max size to 1024KB#181
Conversation
Decode metadata.config by feeding the binary through safe_erl_term:tokens/2 in 4KB slices, parsing each dot-terminated form as it completes, instead of materializing the whole file as a char list before tokenizing. Peak transient decode memory drops from roughly 17x the binary size to binary + ~64KB. This makes it safe to bump MAX_METADATA_SIZE from 128KB to 1MB. Switches the leex dot rule to end_token so tokens/2 returns one form per call. Atom safety (list_to_existing_atom) and the restricted grammar are preserved. UTF-8 is decoded incrementally with a latin1 fallback restart on invalid bytes.
- Fix infinite loop on trailing incomplete UTF-8 bytes: when no input
bytes remain but the UTF-8 buffer is non-empty, fall back to latin1
rather than spin re-decoding the same buffer.
- Drop overly broad try/catch around erl_parse:parse_term/1; the
function only returns {ok, _} | {error, _} and shouldn't throw.
- Comment in safe_erl_term.xrl explaining why the dot rule emits
end_token instead of token.
- Cover the new chunked path in decode_metadata_test: large
multi-chunk input, UTF-8 char straddling a chunk boundary, latin1
fallback for embedded invalid UTF-8 bytes, the trailing-incomplete-
UTF-8 regression case, and 5000 terms across many chunks.
jeregrine
approved these changes
May 6, 2026
jeregrine
left a comment
There was a problem hiding this comment.
Hitting this when we bump to latest Erlang. ❤️
|
On OTP28.5 + Elixir 1.19.5 we're hitting this limit already: I'll let you know what library & size we hit. |
|
https://hex.pm/packages/phosphor_icons seems to be a package that has a large metadata file that goes over the current limit and breaks. |
Member
Author
|
I checked all existing packages and no package hits the limit raised to 1024KB, some outliers are close but I think the fix should be on the package side in that case. |
Adds a `metadata_fields` config option (default `all`) that opts callers into reading only specific top-level keys from metadata.config. When a list is given, the decoder switches to a token-by-token streaming parser: forms whose key is not in the list are discarded with only a bracket-depth counter held in state, so peak memory stays bounded by the chunk size plus one token regardless of how large unwanted forms get.
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.
No description provided.