Skip to content

Clear Zstandard prefixes and restore dictionaries on reset - #136209

Merged
rzikm merged 2 commits into
dotnet:mainfrom
rzikm:rzikm/zstandard-prefix-reset
Oct 22, 2026
Merged

rzikm merged 2 commits into
dotnet:mainfrom
rzikm:rzikm/zstandard-prefix-reset

Conversation

@rzikm

@rzikm rzikm commented Oct 21, 2026

Copy link
Copy Markdown
Member

Zstandard's session-only reset preserves an unused prefix, but the managed encoder and decoder release its pinned memory. This leaves the native context referencing memory that is no longer pinned. Setting a prefix also replaces the constructor-supplied dictionary without restoring it on reset.

Update both reset paths to restore the original dictionary, or explicitly clear the native prefix when no dictionary was supplied, before releasing the pinned memory. Document the prefix override and dictionary restoration behavior.

Extend the existing reset test with dictionary/no-dictionary and empty/nonempty prefix cases. Cover reset before prefix use and after a completed frame, compare reset encoder output with a fresh encoder, and independently verify decoder dictionary restoration.

Validation

Ran from src\libraries\System.IO.Compression using the repository SDK and a short subst path:

  • U:\dotnet.cmd build - passed.
  • U:\dotnet.cmd build /t:Test tests\System.IO.Compression.Tests.csproj - passed, 2,655 tests, zero failures (outer-loop tests excluded).
  • U:\dotnet.cmd build /t:Test tests\System.IO.Compression.Tests.csproj /p:XunitMethodName=System.IO.Compression.ZstandardEncoderDecoderTests.SetPrefix_AfterReset_SetsSuccessfully - passed, all four cases.

Confirmed the expanded test fails without encoder dictionary restoration and, with only the encoder fixed, catches both decoder prefix clearing and dictionary restoration failures.

Note

This pull request description was generated by GitHub Copilot.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

@rzikm
rzikm requested a review from a team October 21, 2026 08:17

@alinpahontu2912 alinpahontu2912 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

@rzikm
rzikm merged commit c871829 into dotnet:main Oct 22, 2026
88 checks passed
@rzikm

rzikm commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
svick pushed a commit that referenced this pull request Oct 27, 2026
…eset (#136307)

Backport of #136209 to release/11.0

/cc @rzikm

## Customer Impact

In rare scenarios when reusing ZstandardDecoder in scenarios with and
without prefixes, the decoder could reuse the previous prefix after
reset, potentially causing unexpected/corrupted output.

## Regression

No, Zstandard is a new feature in 11.0

## Testing

New tests added to run as part of CI.

## Risk

Low, fix is well understood and small.

Co-authored-by: Radek Zikmund <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants