crypto: fix moving read head
authorFedor Indutny <fedor.indutny@gmail.com>
Wed, 27 Nov 2013 20:11:17 +0000 (00:11 +0400)
committerFedor Indutny <fedor.indutny@gmail.com>
Fri, 6 Dec 2013 23:48:43 +0000 (03:48 +0400)
Fix various possible stalls of read head (i.e. try moving it after every
write head update).

NOTE: This is actually backported from `bud`.

src/node_crypto_bio.cc

index 84dc42b..eaf8f76 100644 (file)
@@ -223,17 +223,17 @@ void NodeBIO::TryMoveReadHead() {
   // inside the buffer, respectively. When they're equal - its safe to reset
   // them, because both reader and writer will continue doing their stuff
   // from new (zero) positions.
-  if (read_head_->read_pos_ != read_head_->write_pos_)
-    return;
-
-  // Reset positions
-  read_head_->read_pos_ = 0;
-  read_head_->write_pos_ = 0;
+  while (read_head_->read_pos_ != 0 &&
+         read_head_->read_pos_ == read_head_->write_pos_) {
+    // Reset positions
+    read_head_->read_pos_ = 0;
+    read_head_->write_pos_ = 0;
 
-  // Move read_head_ forward, just in case if there're still some data to
-  // read in the next buffer.
-  if (read_head_ != write_head_)
-    read_head_ = read_head_->next_;
+    // Move read_head_ forward, just in case if there're still some data to
+    // read in the next buffer.
+    if (read_head_ != write_head_)
+      read_head_ = read_head_->next_;
+  }
 }
 
 
@@ -397,8 +397,13 @@ void NodeBIO::Commit(size_t size) {
   // Allocate new buffer if write head is full,
   // and there're no other place to go
   TryAllocateForWrite();
-  if (write_head_->write_pos_ == kBufferLength)
+  if (write_head_->write_pos_ == kBufferLength) {
     write_head_ = write_head_->next_;
+
+    // Additionally, since we're moved to the next buffer, read head
+    // may be moved as well.
+    TryMoveReadHead();
+  }
 }