You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by GitBox <gi...@apache.org> on 2022/10/22 21:41:33 UTC
[GitHub] [mina-sshd] tomaswolf opened a new pull request, #257: [SSHD-1307] Make Nio2Session.shutdownOutputStream() asynchronous
tomaswolf opened a new pull request, #257:
URL: https://github.com/apache/mina-sshd/pull/257
Nio2Session.shutdownOutputStream() can be invoked while there are still writes in progress. The output is shut down in TCP/IP port forwarding to propagate an SSH_MSG_CHANNEL_EOF from the channel to whatever is on the other side of the forwarded port; see SSHD-1055. However, writing through the socket is asynchronous, so a channel may get the following sequence of events:
1. receive last SSH_MSG_CHANNEL_DATA
2. Nio2Session.writeBuffer() enqueues the write request
3. SSH_MSG_CHANNEL_DATA handling completes
4. receive SSH_MSG_CHANNEL_EOF
5. call Nio2Session.shutdownOutputStream()
If (5) shuts down the output immediately but the write request from (2) has not been written yet, that write attempt will fail with a ClosedChannelException.
The output stream on the socket should not be shut down immediately but only once already pending writes have been done. This is already the case with the MINA and Netty transport back-ends, which only schedule a shutdown request on their write queue.
Implement the same for the NIO2 transport. A write future with a null buffer signifies a shutdown request; true write requests always have a non-null buffer. Make shutdownOutputStream() only enqueue such a shutdown request on the session's write queue; the output will be shut down once startWriting() pops this shutdown request from the write queue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org
[GitHub] [mina-sshd] AlexanderAmador commented on pull request #257: [SSHD-1307] Make Nio2Session.shutdownOutputStream() asynchronous
Posted by GitBox <gi...@apache.org>.
AlexanderAmador commented on PR #257:
URL: https://github.com/apache/mina-sshd/pull/257#issuecomment-1293857850
I am interested in acquiring this fix in a release. I saw a comment that your next release may be 2.10.0. Do you have an ETA on that release ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org
[GitHub] [mina-sshd] lgoldstein commented on a diff in pull request #257: [SSHD-1307] Make Nio2Session.shutdownOutputStream() asynchronous
Posted by GitBox <gi...@apache.org>.
lgoldstein commented on code in PR #257:
URL: https://github.com/apache/mina-sshd/pull/257#discussion_r1003563357
##########
sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java:
##########
@@ -298,24 +300,44 @@ public Nio2Service getService() {
@Override
public void shutdownOutputStream() throws IOException {
- AsynchronousSocketChannel socket = getSocket();
- if (socket.isOpen()) {
- if (log.isDebugEnabled()) {
- log.debug("shudownOutputStream({})", this);
- }
- try {
- socket.shutdownOutput();
- } catch (ClosedChannelException e) {
- // This may get called on a Channel EOF in TCP/IP port forwarding. But reading and writing run
- // asynchronously, so it is possible that the socket channel is actually closed here and the producer
- // that wrote into this channel has already disconnected.
- //
- // As this is asynchronous, there is a race condition here. The isOpen() test above does not guarantee
- // that the socket channel is indeed open when we call shutdownOutput().
- //
- // In any case it's safe here to ignore this exception as we're trying to shut down an external end
- // of a TCP/IP port forwarding.
+ if (outputShutDown.compareAndSet(false, true)) {
+ // Schedule a "shut down the output stream" fake write packet with a null buffer. Let already pending writes
+ // finish first.
+ Nio2DefaultIoWriteFuture future = new Nio2DefaultIoWriteFuture("shutdown-" + getRemoteAddress(), null, null);
+ writes.add(future);
+ startWriting();
+ }
+ }
+
+ protected void doShutdownOutputStream(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket)
+ throws IOException {
+ try {
+ if (socket.isOpen()) {
+ if (log.isDebugEnabled()) {
+ log.debug("doShutdownOutputStream({})", this);
+ }
+ try {
+ socket.shutdownOutput();
+ } catch (ClosedChannelException e) {
+ // This may get called on a Channel EOF in TCP/IP port forwarding. But reading and writing run
Review Comment:
I think we should log this exception as DEBUG or TRACE level so that we can tell if it happened.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org
[GitHub] [mina-sshd] tomaswolf commented on a diff in pull request #257: [SSHD-1307] Make Nio2Session.shutdownOutputStream() asynchronous
Posted by GitBox <gi...@apache.org>.
tomaswolf commented on code in PR #257:
URL: https://github.com/apache/mina-sshd/pull/257#discussion_r1003582623
##########
sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java:
##########
@@ -298,24 +300,44 @@ public Nio2Service getService() {
@Override
public void shutdownOutputStream() throws IOException {
- AsynchronousSocketChannel socket = getSocket();
- if (socket.isOpen()) {
- if (log.isDebugEnabled()) {
- log.debug("shudownOutputStream({})", this);
- }
- try {
- socket.shutdownOutput();
- } catch (ClosedChannelException e) {
- // This may get called on a Channel EOF in TCP/IP port forwarding. But reading and writing run
- // asynchronously, so it is possible that the socket channel is actually closed here and the producer
- // that wrote into this channel has already disconnected.
- //
- // As this is asynchronous, there is a race condition here. The isOpen() test above does not guarantee
- // that the socket channel is indeed open when we call shutdownOutput().
- //
- // In any case it's safe here to ignore this exception as we're trying to shut down an external end
- // of a TCP/IP port forwarding.
+ if (outputShutDown.compareAndSet(false, true)) {
+ // Schedule a "shut down the output stream" fake write packet with a null buffer. Let already pending writes
+ // finish first.
+ Nio2DefaultIoWriteFuture future = new Nio2DefaultIoWriteFuture("shutdown-" + getRemoteAddress(), null, null);
+ writes.add(future);
+ startWriting();
+ }
+ }
+
+ protected void doShutdownOutputStream(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket)
+ throws IOException {
+ try {
+ if (socket.isOpen()) {
+ if (log.isDebugEnabled()) {
+ log.debug("doShutdownOutputStream({})", this);
+ }
+ try {
+ socket.shutdownOutput();
+ } catch (ClosedChannelException e) {
+ // This may get called on a Channel EOF in TCP/IP port forwarding. But reading and writing run
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org
[GitHub] [mina-sshd] tomaswolf merged pull request #257: [SSHD-1307] Make Nio2Session.shutdownOutputStream() asynchronous
Posted by GitBox <gi...@apache.org>.
tomaswolf merged PR #257:
URL: https://github.com/apache/mina-sshd/pull/257
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org
[GitHub] [mina-sshd] tomaswolf commented on pull request #257: [SSHD-1307] Make Nio2Session.shutdownOutputStream() asynchronous
Posted by GitBox <gi...@apache.org>.
tomaswolf commented on PR #257:
URL: https://github.com/apache/mina-sshd/pull/257#issuecomment-1293914238
Answer is on [SSHD-1307](https://issues.apache.org/jira/browse/SSHD-1307?focusedCommentId=17625229&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-17625229), where you had asked the same.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@mina.apache.org
For additional commands, e-mail: dev-help@mina.apache.org