You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/04/20 16:12:45 UTC

[5/5] mina-sshd git commit: [SSHD-817] Added clearer indications as to failure due to channel closed in ChannelOutputStream

[SSHD-817] Added clearer indications as to failure due to channel closed in ChannelOutputStream


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/6cd387da
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/6cd387da
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/6cd387da

Branch: refs/heads/master
Commit: 6cd387daa9cd993bdd043cd897dd8f80cbf1fd89
Parents: 8a4ee99
Author: Lyor Goldstein <ly...@gmail.com>
Authored: Fri Apr 20 19:13:33 2018 +0300
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Fri Apr 20 19:16:03 2018 +0300

----------------------------------------------------------------------
 .../common/channel/ChannelOutputStream.java     | 11 ++++--
 .../exception/SshChannelClosedException.java    | 39 ++++++++++++++++++++
 .../java/org/apache/sshd/client/ClientTest.java |  4 +-
 .../sshd/client/subsystem/sftp/SftpTest.java    |  5 ++-
 4 files changed, 52 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6cd387da/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
index ecd2ac1..24c0fa0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelOutputStream.java
@@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
+import org.apache.sshd.common.channel.exception.SshChannelClosedException;
 import org.apache.sshd.common.io.PacketWriter;
 import org.apache.sshd.common.session.Session;
 import org.apache.sshd.common.util.ValidateUtils;
@@ -104,11 +105,12 @@ public class ChannelOutputStream extends OutputStream implements java.nio.channe
 
     @Override
     public synchronized void write(byte[] buf, int s, int l) throws IOException {
+        Channel channel = getChannel();
         if (!isOpen()) {
-            throw new SshException("write(" + this + ") len=" + l + " - channel already closed");
+            throw new SshChannelClosedException(channel.getId(),
+                "write(" + this + ") len=" + l + " - channel already closed");
         }
 
-        Channel channel = getChannel();
         Session session = channel.getSession();
         boolean debugEnabled = log.isDebugEnabled();
         boolean traceEnabled = log.isTraceEnabled();
@@ -167,12 +169,13 @@ public class ChannelOutputStream extends OutputStream implements java.nio.channe
 
     @Override
     public synchronized void flush() throws IOException {
+        AbstractChannel channel = getChannel();
         if (!isOpen()) {
-            throw new SshException("flush(" + this + ") length=" + bufferLength + " - stream is already closed");
+            throw new SshChannelClosedException(channel.getId(),
+                "flush(" + this + ") length=" + bufferLength + " - stream is already closed");
         }
 
         try {
-            AbstractChannel channel = getChannel();
             Session session = channel.getSession();
             boolean traceEnabled = log.isTraceEnabled();
             while (bufferLength > 0) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6cd387da/sshd-core/src/main/java/org/apache/sshd/common/channel/exception/SshChannelClosedException.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/exception/SshChannelClosedException.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/exception/SshChannelClosedException.java
new file mode 100644
index 0000000..ee4b6ff
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/exception/SshChannelClosedException.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.common.channel.exception;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class SshChannelClosedException extends SshChannelException {
+    private static final long serialVersionUID = 4201656251593797929L;
+
+    public SshChannelClosedException(int channelId, String message) {
+        this(channelId, message, null);
+    }
+
+    public SshChannelClosedException(int channelId, Throwable cause) {
+        this(channelId, cause.getMessage(), cause);
+    }
+
+    public SshChannelClosedException(int channelId, String message, Throwable cause) {
+        super(channelId, message, cause);
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6cd387da/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java
index 67a5f57..0ae2f11 100644
--- a/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java
@@ -75,6 +75,7 @@ import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.channel.Channel;
 import org.apache.sshd.common.channel.ChannelListener;
+import org.apache.sshd.common.channel.exception.SshChannelClosedException;
 import org.apache.sshd.common.config.keys.KeyUtils;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.SshFutureListener;
@@ -606,8 +607,9 @@ public class ClientTest extends BaseTestSupport {
                     invertedStream.write(data);
                     invertedStream.flush();
                 }
-            } catch (SshException e) {
+            } catch (SshException | SshChannelClosedException e) {
                 // That's ok, the channel is being closed by the other side
+                outputDebugMessage("%s - ignore %s: %s", getCurrentTestName(), e.getClass().getSimpleName(), e.getMessage());
             }
 
             Collection<ClientChannelEvent> mask = EnumSet.of(ClientChannelEvent.CLOSED);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/6cd387da/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
index 408fed2..476a18f 100644
--- a/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
+++ b/sshd-sftp/src/test/java/org/apache/sshd/client/subsystem/sftp/SftpTest.java
@@ -73,6 +73,7 @@ import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.OptionalFeature;
 import org.apache.sshd.common.PropertyResolverUtils;
 import org.apache.sshd.common.channel.WindowClosedException;
+import org.apache.sshd.common.channel.exception.SshChannelClosedException;
 import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
 import org.apache.sshd.common.random.Random;
 import org.apache.sshd.common.subsystem.sftp.SftpConstants;
@@ -1205,10 +1206,10 @@ public class SftpTest extends AbstractSftpClientTestSupport {
             session.addPasswordIdentity(getCurrentTestName());
             session.auth().verify(5L, TimeUnit.SECONDS);
 
-            PropertyResolverUtils.updateProperty(session, SftpClient.SFTP_CHANNEL_OPEN_TIMEOUT, TimeUnit.SECONDS.toMillis(4L));
+            PropertyResolverUtils.updateProperty(session, SftpClient.SFTP_CHANNEL_OPEN_TIMEOUT, TimeUnit.SECONDS.toMillis(7L));
             try (SftpClient sftp = createSftpClient(session)) {
                 fail("Unexpected SFTP client creation success");
-            } catch (SocketTimeoutException | EOFException | WindowClosedException e) {
+            } catch (SocketTimeoutException | EOFException | WindowClosedException | SshChannelClosedException e) {
                 // expected - ignored
             } finally {
                 PropertyResolverUtils.updateProperty(session, SftpClient.SFTP_CHANNEL_OPEN_TIMEOUT, SftpClient.DEFAULT_CHANNEL_OPEN_TIMEOUT);