Skip to content

Commit 06b1945

Browse files
committed
crypto: fix moving read head
Fix various possible stalls of read head (i.e. try moving it after every write head update). NOTE: This is actually backported from `bud`.
1 parent 03747f6 commit 06b1945

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/node_crypto_bio.cc

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,17 @@ void NodeBIO::TryMoveReadHead() {
223223
// inside the buffer, respectively. When they're equal - its safe to reset
224224
// them, because both reader and writer will continue doing their stuff
225225
// from new (zero) positions.
226-
if (read_head_->read_pos_ != read_head_->write_pos_)
227-
return;
228-
229-
// Reset positions
230-
read_head_->read_pos_ = 0;
231-
read_head_->write_pos_ = 0;
226+
while (read_head_->read_pos_ != 0 &&
227+
read_head_->read_pos_ == read_head_->write_pos_) {
228+
// Reset positions
229+
read_head_->read_pos_ = 0;
230+
read_head_->write_pos_ = 0;
232231

233-
// Move read_head_ forward, just in case if there're still some data to
234-
// read in the next buffer.
235-
if (read_head_ != write_head_)
236-
read_head_ = read_head_->next_;
232+
// Move read_head_ forward, just in case if there're still some data to
233+
// read in the next buffer.
234+
if (read_head_ != write_head_)
235+
read_head_ = read_head_->next_;
236+
}
237237
}
238238

239239

@@ -397,8 +397,13 @@ void NodeBIO::Commit(size_t size) {
397397
// Allocate new buffer if write head is full,
398398
// and there're no other place to go
399399
TryAllocateForWrite();
400-
if (write_head_->write_pos_ == kBufferLength)
400+
if (write_head_->write_pos_ == kBufferLength) {
401401
write_head_ = write_head_->next_;
402+
403+
// Additionally, since we're moved to the next buffer, read head
404+
// may be moved as well.
405+
TryMoveReadHead();
406+
}
402407
}
403408

404409

0 commit comments

Comments
 (0)