@@ -1754,11 +1754,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
17541754
17551755 statistics_.data_received += nread;
17561756
1757- if (LIKELY (stream_buf_offset_ == 0 )) {
1758- // Shrink to the actual amount of used data.
1759- buf.Resize (nread);
1760- IncrementCurrentSessionMemory (nread);
1761- } else {
1757+ if (UNLIKELY (stream_buf_offset_ > 0 )) {
17621758 // This is a very unlikely case, and should only happen if the ReadStart()
17631759 // call in OnStreamAfterWrite() immediately provides data. If that does
17641760 // happen, we concatenate the data we received with the already-stored
@@ -1769,20 +1765,20 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
17691765 memcpy (new_buf.data (), stream_buf_.base + stream_buf_offset_, pending_len);
17701766 memcpy (new_buf.data () + pending_len, buf.data (), nread);
17711767
1772- // The data in stream_buf_ is already accounted for, add nread received
1773- // bytes to session memory but remove the already processed
1774- // stream_buf_offset_ bytes.
1775- // TODO(@jasnell): There are some cases where nread is < stream_buf_offset_
1776- // here but things still work. Those need to be investigated.
1777- // CHECK_GE(nread, stream_buf_offset_);
1778- IncrementCurrentSessionMemory (nread - stream_buf_offset_);
1779-
17801768 buf = std::move (new_buf);
17811769 nread = buf.size ();
17821770 stream_buf_offset_ = 0 ;
17831771 stream_buf_ab_.Reset ();
1772+
1773+ // We have now fully processed the stream_buf_ input chunk (by moving the
1774+ // remaining part into buf, which will be accounted for below).
1775+ DecrementCurrentSessionMemory (stream_buf_.len );
17841776 }
17851777
1778+ // Shrink to the actual amount of used data.
1779+ buf.Resize (nread);
1780+ IncrementCurrentSessionMemory (nread);
1781+
17861782 // Remember the current buffer, so that OnDataChunkReceived knows the
17871783 // offset of a DATA frame's data into the socket read buffer.
17881784 stream_buf_ = uv_buf_init (buf.data (), static_cast <unsigned int >(nread));
0 commit comments