Repository navigation
fix: read IPC messages in full and fail on a cut-off reply - #3
Merged
Merged
Conversation
_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.
kgilmer
approved these changes
Nov 3, 2026
kgilmer
left a comment
Member
There was a problem hiding this comment.
this is an upstream package, may be work posting/checking there as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Connection._ipc_recv()assumes eachrecv()returns a whole piece of the message. A Unix stream socket doesn't promise that, and the code goes wrong in three ways:recv(14). If the 14-byte header arrives in more than one piece,struct.unpack()raisesstruct.error: unpack requires a buffer of 14 bytes, and the call (or the event loop) fails on a valid message._ipc_recv()starts in the middle of a message and blocks or misparses.recv()then returnsb''forever and the loop spins at full CPU. The call never returns, soauto_reconnectnever 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 setspython3-i3ipc: nullfor 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
_recv_exact(sock, size)keeps callingrecv(size - len(data))until it hassizebytes, or until the peer closes the connection._ipc_recv()uses it for the header and for the payload.('', 0), as before. The event loop relies on that to emitipc_shutdown.ConnectionError._message()andmain()already handle that error: they reconnect ifauto_reconnectis set, and otherwise raise it to the caller.test/test_ipc_recv.pyruns a small fake server on a Unix socket, so it doesn't need i3. It covers:main()returning withConnectionErrorwhen the event socket closes in the middle of an event.Testing
masterand pass with the fix. Onmaster, the split header raisesstruct.error, the split payload makes the second read hang, and both EOF cases either never return or raisestruct.error.pytest --ignore test/aio test) on 26.04 and 24.04 gives the same results with and without the fix.test_shutdown_event_reconnectandtest_window.py::test_resizefail onmastertoo, in these containers. The asyncio tests don't run there with the distribution's pytest on either branch.flake8is clean on the changed files.dpkg-buildpackage -bpasses on Ubuntu 24.04, and the built package ships the newconnection.py.