Skip to content

fix: read IPC messages in full and fail on a cut-off reply - #3

Merged
kgilmer merged 1 commit into
regolith-linux:masterfrom
cfsmp3:ubuntu/ipc-recv-exact
Nov 3, 2026
Merged

kgilmer merged 1 commit into
regolith-linux:masterfrom
cfsmp3:ubuntu/ipc-recv-exact

Conversation

@cfsmp3

@cfsmp3 cfsmp3 commented Nov 3, 2026

Copy link
Copy Markdown

Summary

Connection._ipc_recv() assumes each recv() returns a whole piece of the message. A Unix stream socket doesn't promise that, and the code 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, and the call (or the event loop) fails 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. The extra bytes are dropped, so the next _ipc_recv() starts in the middle of a message and blocks or misparses.
  • The payload loop never ends if the connection closes partway through a reply. recv() then returns b'' forever and the loop spins at full CPU. The call never returns, so auto_reconnect never gets a chance to run. This can happen when i3 or sway restarts or exits while it is sending a reply or an event.

Upstream i3ipc-python (altdesktop/i3ipc-python, v2.2.1 and current master) has the same synchronous _ipc_recv(), so there is nothing to backport. Its open PR altdesktop#212 only touches the asyncio connection. Ubuntu 26.04 builds against the Ubuntu archive's python3-i3ipc 2.2.1 (the package model sets python3-i3ipc: null for resolute), and that package has the same code, so this fix reaches the releases that build this fork: 24.04, Debian and the 3.4 stable models.

Change

  • New _recv_exact(sock, size) keeps calling recv(size - len(data)) until it has size bytes, or until 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 now raises ConnectionError. _message() and main() already handle that error: they reconnect if auto_reconnect is set, and otherwise raise it to the caller.
  • New test/test_ipc_recv.py runs a small fake server on a Unix socket, so it doesn't need i3. It covers:
    • 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.

Testing

  • The new tests in Ubuntu 26.04 and 24.04 containers: all 5 fail on master and pass with the fix. On master, 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.
  • The existing synchronous suite against real i3 on Xvfb (pytest --ignore test/aio test) on 26.04 and 24.04 gives the same results with and without the fix. test_shutdown_event_reconnect and test_window.py::test_resize fail on master too, in these containers. The asyncio tests don't run there with the distribution's pytest on either branch.
  • flake8 is clean on the changed files.
  • dpkg-buildpackage -b passes on Ubuntu 24.04, and the built package ships the new connection.py.

_ipc_recv() read the 14-byte header with a single recv(), so a header that
arrived in more than one piece raised struct.error. The 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
forever, 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).

Add tests that use a small fake server on a Unix socket, so they run
without i3.
@cfsmp3
cfsmp3 requested a review from kgilmer November 3, 2026 00:11

@kgilmer kgilmer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an upstream package, may be work posting/checking there as well.

@kgilmer
kgilmer merged commit 3f2fa73 into regolith-linux:master Nov 3, 2026
7 checks passed
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.

3 participants