|
74 | 74 | abstract class AbstractIoUringChannel extends AbstractChannel implements UnixChannel { |
75 | 75 | private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractIoUringChannel.class); |
76 | 76 | final LinuxSocket socket; |
| 77 | + // Owns every in-flight write operation this channel is tracking -- the pooled slot array, the overflow map, |
| 78 | + // the foreign slot array, and the single stream slot. See WriteOperationTracker for the four namespaces. |
| 79 | + final WriteOperationTracker writeTracker = new WriteOperationTracker(); |
77 | 80 | protected volatile boolean active; |
78 | 81 |
|
79 | 82 | // Different masks for outstanding I/O operations. |
@@ -314,6 +317,24 @@ protected void doClose() throws Exception { |
314 | 317 | } |
315 | 318 | } |
316 | 319 |
|
| 320 | + /** |
| 321 | + * Retains every in-flight write's references before handing off to {@link #doShutdownOutput0()}, so a write |
| 322 | + * completion that races the shutdown still finds a live reference to release instead of one the outbound buffer |
| 323 | + * already dropped. |
| 324 | + */ |
| 325 | + @Override |
| 326 | + protected final void doShutdownOutput() throws Exception { |
| 327 | + writeTracker.retainAll(); |
| 328 | + doShutdownOutput0(); |
| 329 | + } |
| 330 | + |
| 331 | + /** |
| 332 | + * Performs the actual output shutdown. Overridden by subclasses that support it. |
| 333 | + */ |
| 334 | + protected void doShutdownOutput0() throws Exception { |
| 335 | + super.doShutdownOutput(); |
| 336 | + } |
| 337 | + |
317 | 338 | @Override |
318 | 339 | protected final void doBeginRead() { |
319 | 340 | if (inputClosedSeenErrorOnRead) { |
@@ -385,17 +406,23 @@ private int scheduleWrite(ChannelOutboundBuffer in) { |
385 | 406 | } |
386 | 407 | Object msg = in.current(); |
387 | 408 |
|
| 409 | + int scheduled; |
388 | 410 | if (msgCount > 1 && in.current() instanceof ByteBuf) { |
389 | | - numOutstandingWrites = (short) ioUringUnsafe().scheduleWriteMultiple(in); |
| 411 | + scheduled = ioUringUnsafe().scheduleWriteMultiple(in); |
390 | 412 | } else if (msg instanceof ByteBuf && ((ByteBuf) msg).nioBufferCount() > 1 || |
391 | 413 | (msg instanceof ByteBufHolder && ((ByteBufHolder) msg).content().nioBufferCount() > 1)) { |
392 | 414 | // We also need some special handling for CompositeByteBuf |
393 | | - numOutstandingWrites = (short) ioUringUnsafe().scheduleWriteMultiple(in); |
| 415 | + scheduled = ioUringUnsafe().scheduleWriteMultiple(in); |
394 | 416 | } else { |
395 | | - numOutstandingWrites = (short) ioUringUnsafe().scheduleWriteSingle(msg); |
396 | | - } |
397 | | - // Ensure we never overflow |
398 | | - assert numOutstandingWrites > 0; |
| 417 | + scheduled = ioUringUnsafe().scheduleWriteSingle(msg); |
| 418 | + } |
| 419 | + // A zero return means the write could not be scheduled: registration.submit(...) failed because the |
| 420 | + // registration is no longer valid, a FileRegion's open() threw, or transferTo(...) produced no bytes or |
| 421 | + // threw. numOutstandingWrites is a short, so guard the narrowing before it happens: a future |
| 422 | + // scheduleWriteSingle/scheduleWriteMultiple override that batches more writes than a short can hold must |
| 423 | + // not silently wrap around. |
| 424 | + assert scheduled <= Short.MAX_VALUE; |
| 425 | + numOutstandingWrites = (short) scheduled; |
399 | 426 | return numOutstandingWrites; |
400 | 427 | } |
401 | 428 |
|
@@ -432,6 +459,15 @@ final void resetCachedAddresses() { |
432 | 459 | remote = socket.remoteAddress(); |
433 | 460 | } |
434 | 461 |
|
| 462 | + // Write completions may carry an id that fell back out of the short range, so they get the untruncated |
| 463 | + // user_data. Connect completions always submit a short id, but connectComplete(...) takes a long to stay |
| 464 | + // consistent with writeComplete(...), so it is also handed the untruncated value. The remaining completions |
| 465 | + // (read, poll, cancel) only ever submit short ids, so narrowUserData(...) below asserts and narrows those. |
| 466 | + private static short narrowUserData(long userData) { |
| 467 | + assert userData == (short) userData : "user_data does not fit a short: " + userData; |
| 468 | + return (short) userData; |
| 469 | + } |
| 470 | + |
435 | 471 | protected abstract class AbstractUringUnsafe extends AbstractUnsafe implements IoUringIoHandle { |
436 | 472 | private IoUringRecvByteAllocatorHandle allocHandle; |
437 | 473 | private boolean closed; |
@@ -440,13 +476,13 @@ protected abstract class AbstractUringUnsafe extends AbstractUnsafe implements I |
440 | 476 |
|
441 | 477 | /** |
442 | 478 | * Schedule the write of multiple messages in the {@link ChannelOutboundBuffer} and returns the number of |
443 | | - * {@link #writeComplete(byte, int, int, short)} calls that are expected because of the scheduled write. |
| 479 | + * {@link #writeComplete(byte, int, int, long)} calls that are expected because of the scheduled write. |
444 | 480 | */ |
445 | 481 | protected abstract int scheduleWriteMultiple(ChannelOutboundBuffer in); |
446 | 482 |
|
447 | 483 | /** |
448 | 484 | * Schedule the write of a single message and returns the number of |
449 | | - * {@link #writeComplete(byte, int, int, short)} calls that are expected because of the scheduled write. |
| 485 | + * {@link #writeComplete(byte, int, int, long)} calls that are expected because of the scheduled write. |
450 | 486 | */ |
451 | 487 | protected abstract int scheduleWriteSingle(Object msg); |
452 | 488 |
|
@@ -456,13 +492,13 @@ public final void handle(IoRegistration registration, IoEvent ioEvent) { |
456 | 492 | byte op = event.opcode(); |
457 | 493 | int res = event.res(); |
458 | 494 | int flags = event.flags(); |
459 | | - short data = (short) event.userData(); |
| 495 | + long userData = event.userData(); |
460 | 496 | switch (op) { |
461 | 497 | case Native.IORING_OP_RECV: |
462 | 498 | case Native.IORING_OP_ACCEPT: |
463 | 499 | case Native.IORING_OP_RECVMSG: |
464 | 500 | case Native.IORING_OP_READ: |
465 | | - readComplete(op, res, flags, data); |
| 501 | + readComplete(op, res, flags, narrowUserData(userData)); |
466 | 502 | break; |
467 | 503 | case Native.IORING_OP_WRITEV: |
468 | 504 | case Native.IORING_OP_SEND: |
@@ -471,16 +507,16 @@ public final void handle(IoRegistration registration, IoEvent ioEvent) { |
471 | 507 | case Native.IORING_OP_SPLICE: |
472 | 508 | case Native.IORING_OP_SEND_ZC: |
473 | 509 | case Native.IORING_OP_SENDMSG_ZC: |
474 | | - writeComplete(op, res, flags, data); |
| 510 | + writeComplete(op, res, flags, userData); |
475 | 511 | break; |
476 | 512 | case Native.IORING_OP_POLL_ADD: |
477 | | - pollAddComplete(res, flags, data); |
| 513 | + pollAddComplete(res, flags, narrowUserData(userData)); |
478 | 514 | break; |
479 | 515 | case Native.IORING_OP_ASYNC_CANCEL: |
480 | | - cancelComplete0(op, res, flags, data); |
| 516 | + cancelComplete0(op, res, flags, narrowUserData(userData)); |
481 | 517 | break; |
482 | 518 | case Native.IORING_OP_CONNECT: |
483 | | - connectComplete(op, res, flags, data); |
| 519 | + connectComplete(op, res, flags, userData); |
484 | 520 |
|
485 | 521 | // once the connect was completed we can also free some resources that are not needed anymore. |
486 | 522 | freeMsgHdrArray(); |
@@ -512,6 +548,7 @@ public final void handle(IoRegistration registration, IoEvent ioEvent) { |
512 | 548 | public void unregistered() { |
513 | 549 | freeMsgHdrArray(); |
514 | 550 | freeRemoteAddressMemory(); |
| 551 | + writeTracker.releaseAll(); |
515 | 552 |
|
516 | 553 | // Check if we need to notify about the deregistration. |
517 | 554 | if (deregisterPromise != null) { |
@@ -971,14 +1008,20 @@ private void pollOut(int res) { |
971 | 1008 | * @param flags the flags. |
972 | 1009 | * @param data the data that was passed when submitting the op. |
973 | 1010 | */ |
974 | | - private void writeComplete(byte op, int res, int flags, short data) { |
| 1011 | + private void writeComplete(byte op, int res, int flags, long data) { |
| 1012 | + writeTracker.complete(data, op, flags); |
975 | 1013 | if ((ioState & CONNECT_SCHEDULED) != 0) { |
976 | 1014 | // The writeComplete(...) callback was called because of a sendmsg(...) result that was used for |
977 | 1015 | // TCP_FASTOPEN_CONNECT. |
978 | 1016 | freeMsgHdrArray(); |
979 | 1017 | if (res > 0) { |
980 | 1018 | // Connect complete! |
981 | | - outboundBuffer().removeBytes(res); |
| 1019 | + // The completion may arrive after close() or shutdownOutput() already dropped the |
| 1020 | + // outbound buffer, in which case there is nothing left to remove. |
| 1021 | + ChannelOutboundBuffer channelOutboundBuffer = outboundBuffer(); |
| 1022 | + if (channelOutboundBuffer != null) { |
| 1023 | + channelOutboundBuffer.removeBytes(res); |
| 1024 | + } |
982 | 1025 |
|
983 | 1026 | // Explicit pass in 0 as this is returned by a connect(...) call when it was successful. |
984 | 1027 | connectComplete(op, 0, flags, data); |
@@ -1027,7 +1070,7 @@ private void writeComplete(byte op, int res, int flags, short data) { |
1027 | 1070 | * @param data the data that was passed when submitting the op. |
1028 | 1071 | * @param outstanding the outstanding write completions. |
1029 | 1072 | */ |
1030 | | - abstract boolean writeComplete0(byte op, int res, int flags, short data, int outstanding); |
| 1073 | + abstract boolean writeComplete0(byte op, int res, int flags, long data, int outstanding); |
1031 | 1074 |
|
1032 | 1075 | /** |
1033 | 1076 | * Called once a cancel was completed. |
@@ -1048,7 +1091,7 @@ void cancelComplete0(byte op, int res, int flags, short data) { |
1048 | 1091 | * @param flags the flags. |
1049 | 1092 | * @param data the data that was passed when submitting the op. |
1050 | 1093 | */ |
1051 | | - void connectComplete(byte op, int res, int flags, short data) { |
| 1094 | + void connectComplete(byte op, int res, int flags, long data) { |
1052 | 1095 | ioState &= ~CONNECT_SCHEDULED; |
1053 | 1096 | freeRemoteAddressMemory(); |
1054 | 1097 |
|
@@ -1134,12 +1177,20 @@ public void connect( |
1134 | 1177 |
|
1135 | 1178 | int fd = fd().intValue(); |
1136 | 1179 | IoRegistration registration = registration(); |
1137 | | - IoUringIoOps ops = IoUringIoOps.newSendmsg(fd, (byte) 0, Native.MSG_FASTOPEN, |
1138 | | - hdr.address(), hdr.idx()); |
1139 | | - connectId = registration.submit(ops); |
1140 | | - if (connectId == 0) { |
1141 | | - // Directly release the memory if submitting failed. |
| 1180 | + short opsId = writeTracker.nextId(); |
| 1181 | + if (opsId == 0) { |
1142 | 1182 | freeMsgHdrArray(); |
| 1183 | + submitConnect(inetSocketAddress); |
| 1184 | + } else { |
| 1185 | + IoUringIoOps ops = IoUringIoOps.newSendmsg(fd, (byte) 0, Native.MSG_FASTOPEN, |
| 1186 | + hdr.address(), opsId); |
| 1187 | + writeTracker.record(opsId, ops.opcode(), initialData); |
| 1188 | + connectId = registration.submit(ops); |
| 1189 | + if (connectId == 0) { |
| 1190 | + writeTracker.abandon(opsId, ops.opcode()); |
| 1191 | + // Directly release the memory if submitting failed. |
| 1192 | + freeMsgHdrArray(); |
| 1193 | + } |
1143 | 1194 | } |
1144 | 1195 | } else { |
1145 | 1196 | submitConnect(inetSocketAddress); |
|