import logging


def read_tcp_size(conn, size):
  """Read `size` number of bytes from `conn`, retrying as needed."""
  chunks = []
  bytes_read = 0
  while bytes_read < size:
    chunk = conn.recv(size - bytes_read)
    if not chunk:
      if bytes_read > 0:
        logging.warning("Incomplete read: %s of %s.", bytes_read, size)
      return
    chunks.append(chunk)
    bytes_read += len(chunk)
  return b"".join(chunks)