GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — io_uring stream channel does not support generic FileRegion, causing ClassCastException · Issue #16570 · netty/netty

io_uring stream channel does not support generic FileRegion, causing ClassCastException #16570

New issue
Copy link
New issue
Copy link
Closed
Closed
io_uring stream channel does not support generic FileRegion, causing ClassCastException#16570
Copy link

Description

@LuciferYang
LuciferYang
opened on Mar 27, 2026
Issue body actions

Summary

AbstractIoUringStreamChannel only handles DefaultFileRegion in its write path. Writing any other FileRegion implementation to an io_uring channel causes an UnsupportedOperationException from filterOutboundMessage, or a ClassCastException in scheduleWriteSingle. This is inconsistent with both EPOLL and NIO, which accept any FileRegion.

Expected behavior

Any FileRegion implementation should be writable to an io_uring channel, consistent with the FileRegion interface contract and the behavior of EPOLL and NIO transports.

Actual behavior

Writing a custom FileRegion (that is not DefaultFileRegion) to an IoUringSocketChannel fails with:

java.lang.UnsupportedOperationException: unsupported message type: MyFileRegion
    at io.netty.channel.uring.AbstractIoUringChannel.filterOutboundMessage(AbstractIoUringChannel.java:1248)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:877)

Additionally, scheduleWriteSingle has no FileRegion branch -- it only handles IoUringFileRegion and ByteBuf with a hard cast:

} else {
    ByteBuf buf = (ByteBuf) msg;  // would ClassCastException if a FileRegion reached here

Root cause

In AbstractIoUringStreamChannel.filterOutboundMessage():

protected Object filterOutboundMessage(Object msg) {
    if (IoUring.isSpliceSupported() && msg instanceof DefaultFileRegion) {
        return new IoUringFileRegion((DefaultFileRegion) msg);
    }
    // Any non-DefaultFileRegion FileRegion falls through to super,
    // which only accepts ByteBuf and throws UnsupportedOperationException.
    return super.filterOutboundMessage(msg);
}

Compare with EPOLL's AbstractEpollStreamChannel.filterOutboundMessage(), which accepts any FileRegion:

protected Object filterOutboundMessage(Object msg) {
    if (msg instanceof ByteBuf) { ... }
    if (msg instanceof FileRegion || msg instanceof SpliceOutTask) {
        return msg;  // accepts ANY FileRegion
    }
    throw new UnsupportedOperationException(...);
}

And EPOLL's doWriteSingle() has a two-tier fallback:

if (msg instanceof DefaultFileRegion) {
    return writeDefaultFileRegion(in, (DefaultFileRegion) msg);  // sendfile
} else if (msg instanceof FileRegion) {
    return writeFileRegion(in, (FileRegion) msg);                // transferTo fallback
}

io_uring has no such fallback.

Affected use case

Apache Spark wraps DefaultFileRegion inside a composite FileRegion implementation (MessageWithHeader) that prepends a frame header before the file body. This is a common pattern for protocols that need framing around file transfers. The composite FileRegion delegates transferTo() to the inner DefaultFileRegion, so it works correctly with NIO and EPOLL but crashes with io_uring.

I am currently evaluating Apache Spark's support for io_uring transport. This issue blocks io_uring adoption for any framework that wraps DefaultFileRegion in a custom FileRegion.

Proposed fix

Add a generic FileRegion fallback in filterOutboundMessage. Since io_uring cannot use synchronous transferTo in its async write path, convert the FileRegion to a direct ByteBuf eagerly:

protected Object filterOutboundMessage(Object msg) {
    if (IoUring.isSpliceSupported() && msg instanceof DefaultFileRegion) {
        return new IoUringFileRegion((DefaultFileRegion) msg);
    }

    if (msg instanceof FileRegion) {
        FileRegion region = (FileRegion) msg;
        long remaining = region.count() - region.transferred();
        ByteBuf buf = alloc().directBuffer((int) remaining);
        try {
            ByteBufWritableByteChannel channel = new ByteBufWritableByteChannel(buf);
            while (region.transferred() < region.count()) {
                long transferred = region.transferTo(channel, region.transferred());
                if (transferred <= 0) {
                    break;
                }
            }
        } catch (IOException e) {
            buf.release();
            throw new RuntimeException("Failed to convert FileRegion to ByteBuf", e);
        } finally {
            region.release();
        }
        return buf;
    }

    return super.filterOutboundMessage(msg);
}

This is not zero-copy but is functionally correct and consistent with EPOLL's writeFileRegion fallback. DefaultFileRegion still uses the optimized splice path via IoUringFileRegion.

Comparison across transports

Transport DefaultFileRegion Generic FileRegion
NIO FileChannel.transferTo(SocketChannel) -- JDK sendfile region.transferTo(SocketChannel) -- works
EPOLL socket.sendFile() -- native sendfile region.transferTo(byteChannel) -- fallback, works
io_uring (current) splice() via IoUringFileRegion -- works crashes
io_uring (proposed) splice() via IoUringFileRegion -- unchanged convert to ByteBuf, send via async path -- works

Environment

  • Netty version: 4.2.x
  • Discovered while evaluating io_uring for Apache Spark's shuffle file transfer

Activity

  1. normanmaurer commented on Mar 27, 2026

    @normanmaurer
    normanmaurer
    on Mar 27, 2026
    Member
    More actions

    We love PRs... would be happy to review

  2. LuciferYang commented on Mar 27, 2026

    @LuciferYang
    LuciferYang
    on Mar 27, 2026
    ContributorAuthor
    More actions

    We love PRs... would be happy to review

    Attempting to fix

  3. dreamlike-ocean commented on Mar 31, 2026

    @dreamlike-ocean
    dreamlike-ocean
    on Mar 31, 2026
    Member
    More actions

    In that case it may block the event loop thread, which makes it acceptable only as a fallback, not as a primary strategy.

    For sending large amounts of data from a FileRegion to a socket, a better approach would be to use IoUringIoHandle to perform true asynchronous file reads, combined with ChunkedWriteHandler for incremental writes.
    This keeps both file I/O and socket writes fully asynchronous and avoids blocking the event loop when zero-copy is not available.

    https://github.com/dreamlike-ocean/NettyAsyncFile
    fyi

  4. LuciferYang commented on Mar 31, 2026

    @LuciferYang
    LuciferYang
    on Mar 31, 2026
    ContributorAuthor
    More actions

    Thanks for the feedback, @dreamlike-ocean.

    You're right that FileRegion.transferTo() is synchronous and could block the event loop for large files. However, the current PR already addresses this — it does not eagerly convert the entire FileRegion into a single ByteBuf. Instead, it reads in 64 KB chunks (configurable via io.netty.iouring.fileRegionChunkSize) and submits each chunk as an async io_uring send. Only one chunk is materialized at a time, and the next chunk is read only after the previous send completes. So the blocking window per event loop tick is bounded to a single 64 KB transferTo call, not the full file size.

    This is comparable to EPOLL's existing writeFileRegion fallback in AbstractEpollStreamChannel, which also calls region.transferTo() synchronously on the event loop — and that has been shipping in production for years.

    The fully async approach using IoUringIoHandle for native async reads + ChunkedWriteHandler is a great optimization for dedicated file-serving scenarios. But it would be a significantly larger architectural change and doesn't belong in this PR, which is a bug fix for API contract compliance — generic FileRegion currently crashes with UnsupportedOperationException on io_uring, breaking frameworks like Apache Spark that wrap DefaultFileRegion in composite implementations.

    The async file read optimization could be a valuable follow-up enhancement. The NettyAsyncFile project is an interesting reference for that. I will take a deeper look into this project to use it as a reference for further optimizations later on.

    Reacted by Mengyang Li
  5. normanmaurer
    added 2 commits that reference this issue on Apr 21, 2026

    Add generic FileRegion support in io_uring stream channel (#16571)

    8204af4

    Add generic FileRegion support in io_uring stream channel (#16571) (#…

    abc68df
  6. chrisvest commented on Apr 25, 2026

    @chrisvest
    chrisvest
    on Apr 25, 2026
    Member
    More actions

    This was fixed. Closing.

    Reacted by YangJie
  7. chrisvest
    closed this as completedon Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.