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(....)` but no one will call 
the set _Runnable_
   
   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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to