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 2020/09/17 06:03:33 UTC

[GitHub] [mina-sshd] lgoldstein commented on a change in pull request #166: [SSHD-1070] Limit the amount of data that is kept in memory for forwa…

lgoldstein commented on a change in pull request #166:
URL: https://github.com/apache/mina-sshd/pull/166#discussion_r489990336



##########
File path: sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
##########
@@ -382,7 +385,32 @@ protected void handleReadCycleFailure(ByteBuffer buffer, Readable bufReader, Thr
         exceptionCaught(exc);
     }
 
+    @Override
+    public void suspendRead() {
+        log.trace("suspendRead({})", this);
+        if (suspendRead.compareAndSet(false, true)) {
+            log.debug("suspendRead({}) requesting read suspension", this);
+        }
+    }
+
+    @Override
+    public void resumeRead() {
+        log.trace("resumeRead({})", this);
+        Runnable runnable = readRunnable.getAndSet(null);
+        suspendRead.set(false);
+        if (runnable != null) {
+            log.debug("resumeRead({}) resuming read", this);
+            runnable.run();
+        }
+    }
+
     protected void doReadCycle(ByteBuffer buffer, Nio2CompletionHandler<Integer, Object> completion) {
+        if (suspendRead.get()) {
+            log.debug("doReadCycle({}) suspending reading", this);
+            readRunnable.set(() -> doReadCycle(buffer, completion));
+            return;
+        }
+

Review comment:
       Seems to me that there is a chance of a race condition as follows:
   
   * Thread A calls `suspendRead` and marks `suspendedRead` as _true_
   * Thread A is pre-empted by thread B that enters `doReadCycle`
   * Thread B sees that `suspendRead` is _true_ and enters the `if` block, but is preempted by thread A before calling`readRunnable.set(...)`
   * Thread A calls `resumeRead` - calls `readRunnable.getAndSet(null);` and gets _null_ - so thinks there is nothing to do
   * Thread B resumes its execution and calls `.set(....)`
   
   Result: reading is not resumed.
   
   I think the suspend/resume should have some mutual exclusion mechanism to prevent this.




----------------------------------------------------------------
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.

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