Send EOF on channel Close (Fixes #143, #496, #553, #554)

Signed-off-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
Jeroen van Erp
2021-09-27 22:58:12 +02:00
parent 53d241e4e3
commit eb09a16aef
2 changed files with 24 additions and 14 deletions

View File

@@ -22,6 +22,7 @@ import net.schmizz.sshj.transport.TransportException;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* {@link OutputStream} for channels. Buffers data upto the remote window's maximum packet size. Data can also be * {@link OutputStream} for channels. Buffers data upto the remote window's maximum packet size. Data can also be
@@ -36,7 +37,7 @@ public final class ChannelOutputStream extends OutputStream implements ErrorNoti
private final DataBuffer buffer = new DataBuffer(); private final DataBuffer buffer = new DataBuffer();
private final byte[] b = new byte[1]; private final byte[] b = new byte[1];
private boolean closed; private AtomicBoolean closed;
private SSHException error; private SSHException error;
private final class DataBuffer { private final class DataBuffer {
@@ -122,6 +123,7 @@ public final class ChannelOutputStream extends OutputStream implements ErrorNoti
this.chan = chan; this.chan = chan;
this.trans = trans; this.trans = trans;
this.win = win; this.win = win;
this.closed = new AtomicBoolean(false);
} }
@Override @Override
@@ -151,24 +153,21 @@ public final class ChannelOutputStream extends OutputStream implements ErrorNoti
private void checkClose() throws SSHException { private void checkClose() throws SSHException {
// Check whether either the Stream is closed, or the underlying channel is closed // Check whether either the Stream is closed, or the underlying channel is closed
if (closed || !chan.isOpen()) { if (closed.get() || !chan.isOpen()) {
if (error != null) if (error != null) {
throw error; throw error;
else } else {
throw new ConnectionException("Stream closed"); throw new ConnectionException("Stream closed");
} }
} }
}
@Override @Override
public synchronized void close() throws IOException { public synchronized void close() throws IOException {
// Not closed yet, and underlying channel is open to flush the data to. // Not closed yet, and underlying channel is open to flush the data to.
if (!closed && chan.isOpen()) { if (!closed.getAndSet(true) && chan.isOpen()) {
try {
buffer.flush(false); buffer.flush(false);
// trans.write(new SSHPacket(Message.CHANNEL_EOF).putUInt32(chan.getRecipient())); trans.write(new SSHPacket(Message.CHANNEL_EOF).putUInt32(chan.getRecipient()));
} finally {
closed = true;
}
} }
} }

View File

@@ -20,6 +20,9 @@ import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public class LoadsOfConnects { public class LoadsOfConnects {
@@ -31,15 +34,23 @@ public class LoadsOfConnects {
@Test @Test
public void loadsOfConnects() { public void loadsOfConnects() {
try { try {
fixture.start();
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
log.info("Try " + i); log.info("Try " + i);
fixture.start(); SSHClient client = fixture.setupConnectedDefaultClient();
fixture.setupConnectedDefaultClient(); client.authPassword("test", "test");
Session s = client.startSession();
Session.Command c = s.exec("ls");
IOUtils.readFully(c.getErrorStream());
IOUtils.readFully(c.getInputStream());
c.close();
s.close();
fixture.stopClient(); fixture.stopClient();
fixture.stopServer();
} }
} catch (Exception e) { } catch (Exception e) {
fail(e.getMessage()); fail(e.getMessage());
} finally {
fixture.stopServer();
} }
} }