You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2022/11/07 13:33:16 UTC

[tomcat] branch 9.0.x updated: NIO writes don't return -1 so neither should CLOSED_NIO_CHANNEL

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 4917824b2e NIO writes don't return -1 so neither should CLOSED_NIO_CHANNEL
4917824b2e is described below

commit 4917824b2e612031db395d918b8d19950cb0bd33
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Nov 7 13:32:20 2022 +0000

    NIO writes don't return -1 so neither should CLOSED_NIO_CHANNEL
---
 java/org/apache/catalina/tribes/transport/nio/NioSender.java | 4 ----
 java/org/apache/tomcat/util/net/NioChannel.java              | 5 +++--
 java/org/apache/tomcat/util/net/NioEndpoint.java             | 7 +------
 webapps/docs/changelog.xml                                   | 5 +++++
 4 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/java/org/apache/catalina/tribes/transport/nio/NioSender.java b/java/org/apache/catalina/tribes/transport/nio/NioSender.java
index 252dc4d183..82e7149222 100644
--- a/java/org/apache/catalina/tribes/transport/nio/NioSender.java
+++ b/java/org/apache/catalina/tribes/transport/nio/NioSender.java
@@ -16,7 +16,6 @@
  */
 package org.apache.catalina.tribes.transport.nio;
 
-import java.io.EOFException;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.nio.ByteBuffer;
@@ -206,9 +205,6 @@ public class NioSender extends AbstractSender {
                 //we have written everything, or we are starting a new package
                 //protect against buffer overwrite
                 int byteswritten = isUdpBased()?dataChannel.write(writebuf) : socketChannel.write(writebuf);
-                if (byteswritten == -1 ) {
-                    throw new EOFException();
-                }
                 remaining -= byteswritten;
                 //if the entire message was written from the buffer
                 //reset the position counter
diff --git a/java/org/apache/tomcat/util/net/NioChannel.java b/java/org/apache/tomcat/util/net/NioChannel.java
index ac46d76753..8e3cb4f0e1 100644
--- a/java/org/apache/tomcat/util/net/NioChannel.java
+++ b/java/org/apache/tomcat/util/net/NioChannel.java
@@ -19,6 +19,7 @@ package org.apache.tomcat.util.net;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.ByteChannel;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.GatheringByteChannel;
 import java.nio.channels.ScatteringByteChannel;
 import java.nio.channels.Selector;
@@ -278,12 +279,12 @@ public class NioChannel implements ByteChannel, ScatteringByteChannel, Gathering
         @Override
         public int write(ByteBuffer src) throws IOException {
             checkInterruptStatus();
-            return -1;
+            throw new ClosedChannelException();
         }
         @Override
         public long write(ByteBuffer[] srcs, int offset, int length)
                 throws IOException {
-            return -1L;
+            throw new ClosedChannelException();
         }
         @Override
         public String toString() {
diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java
index 0889e334fa..e30f4a7c2d 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -1422,9 +1422,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                         }
                     }
                     n = getSocket().write(buffer);
-                    if (n == -1) {
-                        throw new EOFException();
-                    } else if (n == 0 && (buffer.hasRemaining() || getSocket().getOutboundRemaining() > 0)) {
+                    if (n == 0 && (buffer.hasRemaining() || getSocket().getOutboundRemaining() > 0)) {
                         // n == 0 could be an incomplete write but it could also
                         // indicate that a previous incomplete write of the
                         // outbound buffer (for TLS) has now completed. Only
@@ -1455,9 +1453,6 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
             } else {
                 do {
                     n = getSocket().write(buffer);
-                    if (n == -1) {
-                        throw new EOFException();
-                    }
                 } while (n > 0 && buffer.hasRemaining());
                 // If there is data left in the buffer the socket will be registered for
                 // write further up the stack. This is to ensure the socket is only
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index d6991ccbee..346f510c47 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -146,6 +146,11 @@
         stream is cancelled due to an attempt to write to the stream when it is
         in a state that does not permit writes. (markt)
       </add>
+      <scode>
+        NIO writes never return -1 so refactor <code>CLOSED_NIO_CHANNEL</code>
+        not to do so and remove checks for this return value. Based on
+        <pr>562</pr> by tianshuang. (markt)
+      </scode>
     </changelog>
   </subsection>
   <subsection name="Jasper">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org