From 0d675fd5b59effb310bd96ef2154d426afc2ee0e Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Wed, 27 Jul 2016 14:24:16 +0300
Subject: [PATCH] iproto: fix the bug with ever growing output buffer

* don't recycle input buffer if this leads to growth of the output
buffer
---
 src/iproto.cc | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/iproto.cc b/src/iproto.cc
index da32965eaa..b0a829b52c 100644
--- a/src/iproto.cc
+++ b/src/iproto.cc
@@ -442,8 +442,21 @@ iproto_session_input_iobuf(struct iproto_session *session)
 	if (ibuf_unused(&oldbuf->in) >= to_read)
 		return oldbuf;
 
-	/** All requests are processed, reuse the buffer. */
-	if (ibuf_size(&oldbuf->in) == session->parse_size) {
+	/**
+	 * Reuse the buffer if:
+	 * - all requests are processed (in only has unparsed
+	 *   content, and out is empty, so we will not bloat
+	 *   output by reusing input
+	 * - we received a large packet, so we need to
+	 *   extend input buffer size to store a single large
+	 *   packet. In this case we need to realloc the input
+	 *   buffer, simply falling through to the subsequent
+	 *   branches will not make the buffer larger.
+	 */
+	if (ibuf_size(&oldbuf->in) == session->parse_size &&
+	    (ibuf_pos(&oldbuf->in) == session->parse_size ||
+	     obuf_size(&oldbuf->out) == 0)) {
+
 		ibuf_reserve(&oldbuf->in, to_read);
 		return oldbuf;
 	}
-- 
GitLab