Skip to content

Commit

Permalink
jsoncom: gracefully report EMSGSIZE errors
Browse files Browse the repository at this point in the history
When `jsoncomm` fails because the message is too big it currently
does not indicate just how big the message was. This commit adds
this information so that it's easier for us to determine what to
do about it.

We could also include a pointer to `/proc/sys/net/core/wmem_defaults`
but it seems we want to not require fiddling with that so let's
not do it for now.

See also osbuild#1838
  • Loading branch information
mvo5 authored and achilleas-k committed Aug 13, 2024
1 parent f4dc0f3 commit 29f926f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion osbuild/util/jsoncomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,13 @@ def send(self, payload: object, *, fds: Optional[list] = None):
if fds:
cmsg.append((socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds)))

n = self._socket.sendmsg([serialized], cmsg, 0)
try:
n = self._socket.sendmsg([serialized], cmsg, 0)
except OSError as exc:
if exc.errno == errno.EMSGSIZE:
raise BufferError(
f"jsoncomm message size {len(serialized)} is too big") from exc
raise exc
assert n == len(serialized)

def send_and_recv(self, payload: object, *, fds: Optional[list] = None):
Expand Down
12 changes: 12 additions & 0 deletions test/mod/test_util_jsoncomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#

import asyncio
import errno
import os
import pathlib
import tempfile
import unittest
from concurrent import futures

import pytest

from osbuild.util import jsoncomm


Expand Down Expand Up @@ -216,3 +219,12 @@ def test_send_and_recv(self):
self.assertEqual(ping, pong)
pong, _, _ = a.recv()
self.assertEqual(ping, pong)

def test_send_and_recv_tons_of_data_still_errors(self):
a, _ = jsoncomm.Socket.new_pair()

ping = {"data": "1" * 1_000_000}
with pytest.raises(BufferError) as exc:
a.send(ping)
assert str(exc.value) == "jsoncomm message size 1000012 is too big"
assert exc.value.__cause__.errno == errno.EMSGSIZE

0 comments on commit 29f926f

Please sign in to comment.