[Bug]: _recv_line() can block on partial TCP receive #1

Closed
opened 2026-04-17 11:27:59 +00:00 by tgohle · 0 comments
Owner

Problem

_recv_line() can block indefinitely if the newline does not arrive at the end of a single recv() chunk.

Current code

def _recv_line(self) -> str:
    chunks = []
    while True:
        b = self.sock.recv(4096)
        if not b:
            break
        chunks.append(b)
        if b.endswith(b"\n"):
            break

Why this is a bug

recv(4096) can return partial TCP chunks.

If the instrument sends OK\n but it arrives as two calls such as OK + \n, the b.endswith(b"\n") check on the first chunk misses the line ending. That can leave the function waiting longer than expected or blocking until timeout or socket close.

Fix approach

Accumulate into a bytearray and check the joined buffer for b"\n" instead of only checking the most recent chunk.

Status

Local fix already implemented in the current v0.2 working copy.
Still needs commit, push, and hardware verification before closing.

### Problem `_recv_line()` can block indefinitely if the newline does not arrive at the end of a single `recv()` chunk. ### Current code ```python def _recv_line(self) -> str: chunks = [] while True: b = self.sock.recv(4096) if not b: break chunks.append(b) if b.endswith(b"\n"): break ``` ### Why this is a bug `recv(4096)` can return partial TCP chunks. If the instrument sends `OK\n` but it arrives as two calls such as `OK` + `\n`, the `b.endswith(b"\n")` check on the first chunk misses the line ending. That can leave the function waiting longer than expected or blocking until timeout or socket close. ### Fix approach Accumulate into a `bytearray` and check the joined buffer for `b"\n"` instead of only checking the most recent chunk. ### Status Local fix already implemented in the current v0.2 working copy. Still needs commit, push, and hardware verification before closing.
tgohle added the
kind
bug
1
area/transport
severity
high
1
state
needs-test
1
labels 2026-04-17 11:37:34 +00:00
tgohle added this to the v0.2.0 - first bugfix round finished milestone 2026-04-17 12:08:10 +00:00
tgohle self-assigned this 2026-04-17 12:09:09 +00:00
tgohle removed their assignment 2026-04-17 12:09:17 +00:00
tgohle self-assigned this 2026-04-17 12:09:31 +00:00
tgohle changed title from _recv_line() can block on partial TCP receive to [Bug]: _recv_line() can block on partial TCP receive 2026-04-18 11:22:42 +00:00
Sign in to join this conversation.
No description provided.