Repository navigation
Conversation
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.
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
A Unix stream socket can hand over a message in any number of pieces, and a
recv(n)may return fewer thannbytes. The synchronousConnection._ipc_recv()assumes otherwise, and 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 byteson a valid message.recv()then returnsb''forever and the loop spins at full CPU. The call never returns, soauto_reconnectnever 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 singlesock_recv(). A reply cut off by EOF is logged and returned truncated, so the caller fails with aJSONDecodeErrorinstead of a connection error.Change
Connection: a new_recv_exact(sock, size)keeps callingrecv(size - len(data))until it hassizebytes or 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.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 raisesConnectionError. Whenauto_reconnectis 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._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.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 withConnectionErrorwhen the event socket closes in the middle of an event.test/aio/test_ipc_recv.py(asyncio): the same four reply cases.Testing
master, 8 of the 9 fail. In the sync tests, the split header raisesstruct.error, the split payload makes the second read hang, and both EOF cases either never return or raisestruct.error. In the asyncio tests, the split header raisesstruct.error, a reply cut off mid-payload raisesJSONDecodeError, and one cut off mid-header raisesstruct.error. The asyncio split-payload test already passes onmasterthanks to aio: Read all message_length bytes from message response #189, and stays as a regression test.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 onmastertoo, in these environments:NameError: name 'display' is not defined(i3ipc/_private/sync.py);event_loopfixture, which 1.x removed), and with 0.20 they fail, mostly withAttributeError.flake8is 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 onmastertoo.