Skip to content

Read IPC messages in full and fail on a cut-off reply - #218

Open
cfsmp3 wants to merge 1 commit into
altdesktop:masterfrom
cfsmp3:fix/ipc-recv-exact
Open

cfsmp3 wants to merge 1 commit into
altdesktop:masterfrom
cfsmp3:fix/ipc-recv-exact

Conversation

@cfsmp3

@cfsmp3 cfsmp3 commented Nov 3, 2026

Copy link
Copy Markdown

Summary

A Unix stream socket can hand over a message in any number of pieces, and a recv(n) may return fewer than n bytes. The synchronous Connection._ipc_recv() assumes otherwise, and goes wrong in three ways:

  • The header is read with a single recv(14). If the 14-byte header arrives in more than one piece, struct.unpack() raises struct.error: unpack requires a buffer of 14 bytes on a valid message.
  • The payload loop asks for the full payload length on every read, not for what is still missing. If the payload arrives in pieces and the next message follows right behind it, the loop reads into the next message and the extra bytes are dropped, so the next read starts in the middle of a message.
  • The payload loop never ends if the connection closes partway through a message. recv() then returns b'' forever and the loop spins at full CPU. The call never returns, so auto_reconnect never gets to run. This can happen when i3 or sway restarts or exits while it is sending a reply or an event.

The asyncio connection already reads the whole reply payload since #189, but _message() still reads the reply header with a single sock_recv(). A reply cut off by EOF is logged and returned truncated, so the caller fails with a JSONDecodeError instead of a connection error.

Change

  • Sync Connection: a new _recv_exact(sock, size) keeps calling recv(size - len(data)) until it has size bytes or the peer closes the connection. _ipc_recv() uses it for the header and for the payload.
    • A clean EOF before a new message still returns ('', 0), as before; the event loop relies on that to emit ipc_shutdown.
    • EOF partway through a header or a payload raises ConnectionError. _message() and main() already handle that error: they reconnect if auto_reconnect is set, and otherwise raise it to the caller.
  • asyncio Connection._message(): the reply header is read in full too, with a new _sock_recv_exact(), which also replaces the payload loop. A reply whose header or payload is cut off by EOF raises ConnectionError. When auto_reconnect is set, that schedules a reconnect, as the existing handler already did for connection errors. An empty read before the header still returns an empty reply, as before.
  • The asyncio event reader (_read_message()) has the same single-recv() problem. This PR leaves it alone because Read full json message from socket in i3ipc.aio.connection (#1) #212 already fixes it (it also fixes Large JSON messages over i3ipc socket are truncated #213). The two PRs touch different methods and merge without conflicts.
  • New tests run a small fake server on a Unix socket, so they don't need i3:
    • test/test_ipc_recv.py (sync): a header split across three reads; a split payload followed by the next message in the same read; EOF in the middle of a payload; EOF in the middle of a header; main() returning with ConnectionError when the event socket closes in the middle of an event.
    • test/aio/test_ipc_recv.py (asyncio): the same four reply cases.

Testing

  • The new tests on Ubuntu 26.04 (Python 3.14, pytest 9, pytest-asyncio 1.3) and 24.04 (Python 3.12, pytest 7.4, pytest-asyncio 0.20):
    • On master, 8 of the 9 fail. In the sync tests, the split header raises struct.error, the split payload makes the second read hang, and both EOF cases either never return or raise struct.error. In the asyncio tests, the split header raises struct.error, a reply cut off mid-payload raises JSONDecodeError, and one cut off mid-header raises struct.error. The asyncio split-payload test already passes on master thanks to aio: Read all message_length bytes from message response #189, and stays as a regression test.
    • With this change, all 9 pass.
  • The existing suite (run-tests.py, i3 on Xvfb) gives the same results with and without this change on both releases: on 26.04, 4 failed, 9 passed and 17 errors; on 24.04, 21 failed and 9 passed. With the change, the 9 new tests pass on top of that. These failures happen on master too, in these environments:
    • four sync tests fail with NameError: name 'display' is not defined (i3ipc/_private/sync.py);
    • the asyncio suite doesn't run with these pytest-asyncio versions: with 1.3 every test errors at setup (the suite overrides the event_loop fixture, which 1.x removed), and with 0.20 they fail, mostly with AttributeError.
  • flake8 is clean on the changed files. yapf -d (0.43 and 0.33) suggests nothing in the changed code; its only suggestions are blank lines elsewhere in these files, which it suggests on master too.

A Unix stream socket may hand over a message in any number of pieces,
but the sync Connection._ipc_recv() read the 14-byte header with a single
recv(), so a header that arrived in more than one piece raised
struct.error. Its payload loop asked for the full payload length on every
read, which could swallow the start of the next message, and it never
ended if the socket closed before the announced length arrived: recv()
kept returning b'' and the loop spun at full CPU, so auto_reconnect never
got a chance to run.

Read the header and the payload with a helper that keeps reading until it
has exactly the bytes it needs. A clean EOF before a new message still
returns ('', 0) as before; EOF partway through a header or payload now
raises ConnectionError, which _message() and main() already handle
(reconnect if auto_reconnect is set, otherwise raise to the caller).

The asyncio Connection._message() already reads the whole reply payload
(altdesktop#189), but it read the reply header with a single sock_recv() too, and a
reply cut off by EOF was returned truncated, so the caller failed with a
JSONDecodeError. Read the header in full as well, and raise
ConnectionError for a cut-off header or reply, which schedules a
reconnect when auto_reconnect is set. The asyncio event reader is left to
altdesktop#212.

Add tests with a small fake server on a Unix socket, so they run
without i3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Large JSON messages over i3ipc socket are truncated

2 participants