From 69cde41fa33a618f9128ad5699542e2fa12f7313 Mon Sep 17 00:00:00 2001 From: Jan Kratochvil Date: Thu, 23 May 2019 11:34:25 +0200 Subject: [PATCH] Fix: assertion failed: tws-api/cpp/suppl/EPosixClientSocket.cpp:412:virtual int EPosixClientSocket::receive(char*, size_t): sz > 0 (gdb) bt %0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50 %1 0x00007ffff6131895 in __GI_abort () at abort.c:79 %2 0x000000000136a43d in fatal (fmt=0x1503d48 "ib __assert_fail: %s:%d:%s: %s") at lib/log.C:29 %3 0x0000000001072a11 in IBSTOCK__assert_fail (assertion=0x1503cf9 "sz > 0", file=0x1503bb0 "tws-api/EPosixClientSocket.cpp", line=412, function=0x1506760 "virtual int EPosixClientSocket::receive(char*, size_t)") at iblib.C:44 %4 0x00000000010715a8 in EPosixClientSocket::receive (this=0x2e3a5f0, buf=0x2e626a0 "", sz=0) at tws-api/EPosixClientSocket.cpp:412 %5 0x000000000106cace in EReaderST::onReceive (this=0x2e3adb8) at tws-api/EReaderST.cpp:132 %6 0x0000000000f2984e in IbClientBackend::tick (this=0x2e3a5f0, wait=true) at ib.C:2153 ... (gdb) frame 5 132 int nRes = m_pClientSocket->receive(m_buf->begin + m_buf->offset, sz ); (gdb) p m_buf->size $8 = 106496 (gdb) p m_buf->offset $9 = 106496 That is because I read data as long as they are available (POLLIN) and only then I start processing them. The last chunk with `newsize` is unrelated a bit and not so important, it is just the optimal size of `m_buf` which is IMO what the code tried to do. --- cpp/suppl/EReaderST.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cpp/suppl/EReaderST.cpp b/cpp/suppl/EReaderST.cpp index cf1f7ef..b064821 100644 --- a/cpp/suppl/EReaderST.cpp +++ b/cpp/suppl/EReaderST.cpp @@ -128,15 +128,23 @@ void EReaderST::select_timeout( int msec ) void EReaderST::onReceive() { const char * errmsg; - size_t sz = m_buf->size - m_buf->offset; - int nRes = m_pClientSocket->receive(m_buf->begin + m_buf->offset, sz ); - if (nRes <= 0) { - errmsg = nRes < 0 ? strerror(errno) - : "The remote host closed the connection."; - goto fail; + if (m_buf->offset == m_buf->size) { + if (resize_read_buf(m_buf, m_buf->size + IN_BLOCK_SIZE) == -1) { + errmsg = strerror(errno); + goto fail; + } } + { + size_t sz = m_buf->size - m_buf->offset; + int nRes = m_pClientSocket->receive(m_buf->begin + m_buf->offset, sz ); + if (nRes <= 0) { + errmsg = nRes < 0 ? strerror(errno) + : "The remote host closed the connection."; + goto fail; + } - m_buf->offset += nRes; + m_buf->offset += nRes; + } assert(m_pClientSocket->usingV100Plus()); if (readV100Plus() == -1) { @@ -229,7 +237,7 @@ int EReaderST::readV100Plus() /* let the last incomplete message fit into the buffer */ if( msgSize > m_buf->size) { - size_t newsize = IN_BLOCK_SIZE * (1 + msgSize/IN_BLOCK_SIZE); + size_t newsize = IN_BLOCK_SIZE * ((msgSize + IN_BLOCK_SIZE - 1)/IN_BLOCK_SIZE); if (resize_read_buf(m_buf, newsize) == -1) { goto fail; }