Skip to content

Port5.0 IoUring:Refactor io_uring write lifecycle management - #18154

Merged
normanmaurer merged 1 commit into
netty:5.0from
dreamlike-ocean:trace_write_ops_5.0
Oct 30, 2026
Merged

normanmaurer merged 1 commit into
netty:5.0from
dreamlike-ocean:trace_write_ops_5.0

Conversation

@dreamlike-ocean

Copy link
Copy Markdown
Member

Motivation:

This is not a 1:1 port of #17757.

In 4.2, a user-called shutdownOutput() clears the outbound buffer synchronously while writes may still be in flight, so 4.2 retains the in-flight buffers in doShutdownOutput(). The 5.0 core does not have this problem:

  • The public shutdown(Outbound) never touches the outbound buffer.
  • The private shutdownOutput() is only reached from handleWriteError, either before a write is submitted or from the CQE of the last in-flight write, so the kernel no longer uses any of the buffers.
  • close() waits for in-flight writes before clearing the outbound buffer.

So retaining on shutdown, as both 4.2 and the previous WriteOperationTracker did, protects nothing in 5.0.

The only place where 5.0 releases buffers that are still in flight is the inactive flush: writeFlushedNow() calls failFlushed() on an inactive channel. This happens during a TCP Fast Open connect, and after a datagram disconnect() while sendmsgs are in flight.

Modification:

  • Port IoUring:Refactor io_uring write lifecycle management #17757:
    • WriteOpsSnapshot replaces WriteOperationTracker.
    • PendingZeroCopyWrites holds zero-copy buffers until the notification arrives.
    • The TFO write lifecycle and connectId changes.
    • The UDS sendfd fd copy.
    • A per-channel SENDMSG_ZC MsgHdrMemory.
  • Retain in-flight buffers in isWriteFlushedScheduled(), the 5.0 counterpart of the 4.2 flush0() override.
  • doShutdown(Outbound) no longer retains anything.
  • Adapt to 5.0: no Channel.Unsafe, and stream channels are merged into IoUringSocketChannel.
  • Tests:
    • Remove the tracker tests.
    • IoUringShutdownOutputDuringInFlight{Write,Splice}Test now expect refCnt == 1 after shutdown.
    • Add datagram retry and duplicate-send tests.

Result:

In-flight write buffers are retained only where 5.0 actually releases them early, and ordinary writes only copy a few primitive fields into WriteOpsSnapshot instead of recording references.

@testlens-app

testlens-app Bot commented Oct 29, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 277d1ab
▶️ Tests: 41519 executed
⚪️ Checks: 20/20 completed


Learn more about TestLens at testlens.app/docs.

@normanmaurer

Copy link
Copy Markdown
Member

@dreamlike-ocean some stuff to address:

- Zero-copy buffers leak on unregister (IoUringSocketChannel.unregistered()):
  - The old code released everything in unregistered() through writeTracker.releaseAll(). The new unregistered() releases nothing from PendingZeroCopyWrites, which has no releaseAll().
  - ioState doesn't track pending zero-copy notifications. After the primary completion carrying F_MORE, numOutstandingWrites goes to 0 and WRITE_SCHEDULED is cleared. A close at that point cancels the registration, and the later notification is never delivered. The retained buffers then never reach refCnt 0.
  - 4.2 has the same PendingZeroCopyWrites design, so this looks inherited from #17757 and not introduced by the port. I didn't check whether 4.2 handles it elsewhere.

@dreamlike-ocean

Copy link
Copy Markdown
Member Author

@dreamlike-ocean some stuff to address:

- Zero-copy buffers leak on unregister (IoUringSocketChannel.unregistered()):
  - The old code released everything in unregistered() through writeTracker.releaseAll(). The new unregistered() releases nothing from PendingZeroCopyWrites, which has no releaseAll().
  - ioState doesn't track pending zero-copy notifications. After the primary completion carrying F_MORE, numOutstandingWrites goes to 0 and WRITE_SCHEDULED is cleared. A close at that point cancels the registration, and the later notification is never delivered. The retained buffers then never reach refCnt 0.
  - 4.2 has the same PendingZeroCopyWrites design, so this looks inherited from #17757 and not introduced by the port. I didn't check whether 4.2 handles it elsewhere.

This doesn't leak. The io.netty.channel.uring.IoUringIoHandler.DefaultIoUringIoRegistration#handle only drops a completion on a CQE without IORING_CQE_F_MORE, so a zero-copy write stays counted until its NOTIF arrives:

if ((flags & Native.IORING_CQE_F_MORE) == 0 && --outstandingCompletions == 0 && removeLater) {
    remove();
}

A close after the primary CQE only sets removeLater. The notification is still delivered and releases the buffers, and unregistered() runs only after that. 4.2 works the same way.

@normanmaurer
normanmaurer merged commit e22834e into netty:5.0 Oct 30, 2026
21 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