Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
Nov 07, 2011
Richard Mortimer Flush HTTP input and error streams after searching for the X-Hudson-C…
…LI-Port

property. This releases the HTTP connection in a friendly way and allows the
webserver to recycle the connection a little quicker.
87995e9
Richard Mortimer [FIXED JENKINS-11130] SEVERE: I/O error in channel Chunked connection…
… when using jenkins-cli.jar

When using HTTP full duplex connections the upstream channel can be idle for
long periods of time and the webserver will time the connection out. Jenkins
sees this and terminates the downstream channel causing the unexpected closure
of the read channel in the CLI client.

Timeout of 15 seconds is based on the 20 second default timeout as seen in
Ubuntu Oneiric Tomcat 6 installations.
92496f9
3  changelog.html
@@ -55,6 +55,9 @@
55 55
 <!-- Record your changes in the trunk here. -->
56 56
 <div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
57 57
 <ul class=image>
  58
+  <li class=bug>
  59
+  java.io.IOException: Unexpected termination of the channel - SEVERE: I/O error in channel Chunked connection when using jenkins-cli.jar (works on older Hudson version)
  60
+  (<a href="https://issues.jenkins-ci.org/browse/JENKINS-11130">issue 11130</a>)
58 61
   <li class=>
59 62
 </ul>
60 63
 </div><!--=TRUNK-END=-->
30  cli/src/main/java/hudson/cli/CLI.java
@@ -47,6 +47,7 @@
47 47
 import java.io.IOException;
48 48
 import java.io.InputStream;
49 49
 import java.io.OutputStream;
  50
+import java.net.HttpURLConnection;
50 51
 import java.net.Socket;
51 52
 import java.net.URL;
52 53
 import java.net.URLConnection;
@@ -121,7 +122,7 @@ private Channel connectViaHttp(String url) throws IOException {
121 122
         FullDuplexHttpStream con = new FullDuplexHttpStream(jenkins);
122 123
         Channel ch = new Channel("Chunked connection to "+jenkins,
123 124
                 pool,con.getInputStream(),con.getOutputStream());
124  
-        new PingThread(ch,30*1000) {
  125
+        new PingThread(ch,15*1000) {
125 126
             protected void onDead() {
126 127
                 // noop. the point of ping is to keep the connection alive
127 128
                 // as most HTTP servers have a rather short read time out
@@ -153,11 +154,38 @@ private int getCliTcpPort(String url) throws IOException {
153 154
             throw (IOException)new IOException("Failed to connect to "+url).initCause(e);
154 155
         }
155 156
         String p = head.getHeaderField("X-Hudson-CLI-Port");
  157
+        flushURLConnection(head);
156 158
         if(p==null) return -1;
157 159
         return Integer.parseInt(p);
158 160
     }
159 161
 
160 162
     /**
  163
+     * Flush the supplied {@link URLConnection} input and close the
  164
+     * connection nicely.
  165
+     * @param conn the connection to flush/close
  166
+     */
  167
+    private void flushURLConnection(URLConnection conn) {
  168
+        byte[] buf = new byte[1024];
  169
+        try {
  170
+            InputStream is = conn.getInputStream();
  171
+            while (is.read(buf) > 0) {
  172
+                // Ignore
  173
+            }
  174
+            is.close();
  175
+        } catch (IOException e) {
  176
+            try {
  177
+                InputStream es = ((HttpURLConnection)conn).getErrorStream();
  178
+                while (es.read(buf) > 0) {
  179
+                    // Ignore
  180
+                }
  181
+                es.close();
  182
+            } catch (IOException ex) {
  183
+                // Ignore
  184
+            }
  185
+        }
  186
+    }
  187
+
  188
+    /**
161 189
      * Shuts down the channel and closes the underlying connection.
162 190
      */
163 191
     public void close() throws IOException, InterruptedException {

No commit comments for this range

Something went wrong with that request. Please try again.