Skip to content

Access Unsafe only on demand, and better protect against unavailability - #97

Merged
cowtowncoder merged 5 commits into
ning:masterfrom
Marcono1234:lazy-load-unsafe
Nov 30, 2026
Merged

cowtowncoder merged 5 commits into
ning:masterfrom
Marcono1234:lazy-load-unsafe

Conversation

@Marcono1234

@Marcono1234 Marcono1234 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

This tries to address the two problems mentioned in #86 (comment):

  • ChunkEncoderFactory#optimal... methods only catch Exception
    If in future JDK versions Unsafe methods are indeed removed, most likely a LinkageError (or subclass) would occur, which is an Error, not an Exception.

  • ChunkDecoderFactory static block currently unconditionally tries to initialize UnsafeChunkDecoder
    This leads to "A terminally deprecated method in sun.misc.Unsafe has been called" warnings (see also JEP 498), even if the user only uses 'safe' decoder methods, because UnsafeChunkDecoder during class initialization accesses Unsafe methods.

This PR tries to solve this by moving the direct Unsafe access to a new UnsafeAccess class and adding a isUnsafeSupported() method. This should hopefully:

  • Avoid any direct Unsafe access merely when UnsafeChunkDecoder or UnsafeChunkEncoder are initialized

  • Avoiding access to Unsafe if the user only uses the 'safe' API

    Note: It seems there might unfortunately be some parts of the API where the user cannot chose between 'safe' and 'optimal' (possibly Unsafe-based), so in these cases the user can likely not avoid the "terminally deprecated method ... called" warnings.

Comment thread CLAUDE.md

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Have updated this to match the code changes. I don't use Claude currently, so I don't know if this change is useful.

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.

Thanks! I'll have a look -- I do use Claude.

@Marcono1234

Copy link
Copy Markdown
Contributor Author

I hope this PR will help in case Unsafe methods will indeed be removed in the future, but I currently have no way to test this locally.

Please let me know what you think about this PR and if you want anything changed.

@Marcono1234
Marcono1234 marked this pull request as ready for review November 24, 2026 19:56
cowtowncoder and others added 4 commits November 29, 2026 18:15
- `UnsafeChunkDecoder`/`UnsafeChunkEncoder` constructors now throw a clear
  `IllegalStateException` when `Unsafe` is unavailable, instead of failing on
  first use (encoder checks before the base constructor takes recycled buffers)
- `UnsafeAccess.isUnsafeSupported()` catches `Throwable` so "optimal" factories
  always fall back to the Vanilla codecs
- Keep `ChunkDecoderFactory` constructor private (no new public API)
- Make `UnsafeAccess` final with a private constructor

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- New `UnsafeDeniedTest`, run with `--sun-misc-unsafe-memory-access=deny` by an
  extra surefire execution in a `jdk24+` profile (auto-activated on JDK 24+);
  checks factories fall back to Vanilla, direct Unsafe codec construction fails
  fast, and block/streaming round trips still work
- `UnsafeAccessTest` no longer fails when `Unsafe` is denied
- Drop explicit JDK API javadoc link (redirect is an error on JDK 25 javadoc);
  the plugin's default `detectJavaApiLink` links it offline
- Add JDK 25 to the CI build matrix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cowtowncoder
cowtowncoder merged commit fd18d51 into ning:master Nov 30, 2026
6 checks passed
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.

2 participants