Return the pooled buffer when encoding fails - #505
Merged
Merged
Conversation
em3s
approved these changes
Oct 2, 2026
Contributor
There was a problem hiding this comment.
Thanks @shin4141 — the one-buffer test pins it exactly. Fails on main, passes with the patch.
I'll tweak the title and merge. One thing left over: borrow() is pool.poll(), so past poolSize concurrent encodes it returns null and reset() NPEs. Falling back to new EdgeBuffer() would cover it. Happy to take a follow-up PR if you're interested.
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.
Summary
AbstractEdgeEncoder.useAsByteArrayreturned a borrowedEdgeBufferonly after the encoding callback and byte copy succeeded. If the callback threw, that buffer was permanently removed from the shared pool. With a one-buffer pool, a later valid encoding then had no buffer to borrow.Return the buffer in
finally. The original exception still reaches the caller, and the next use resets the returned buffer before writing. This change does not alter successful encodings.Regression
The new test uses a one-buffer pool, writes one value, then asks the encoder to serialize an unsupported object. On
mainat0cd8e45a4bd8f383bf58175e05fe51ba64f0c06b, it fails because the pool size is0instead of1. With the patch, it checks that the original exception is preserved, the same buffer returns to the pool, and a subsequent valid encoding matches a fresh unpooled encoder.Tests
./gradlew :codec-java:test— 67 passed.:core:test— 8,918 passed, 5 skipped;:core-java:test— 313 passed../gradlew :server:test— 745 passed with a test-runtime Byte Buddy agent because the isolated macOS build disallows self-attachment../gradlew spotlessCheck— passed../gradlew test— not fully verified: the unrelated engine test fixture'sBlockHound.install()cannot self-attach in the isolated macOS environment (Operation not permitted). The fiveDefaultBufferCapacityTestcases themselves passed.AI Assistance