From f7a99f944d77ae38a7bd59406f3daeef84b9dda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Fri, 16 Aug 2024 16:14:03 +0200 Subject: [PATCH 1/2] Fix compatibility with Python 3.13 _UnixSelectorEventLoop in Python 3.13 expect _unix_server_sockets dict, add one. It's later used (among others) in _stop_serving(), but not in close() directly, so just adding attribute is enough. The related CPython change: 74b868f636a "gh-111246: Remove listening Unix socket on close (#111483)" Add a test for unix sockets too. --- changes/176.bugfix.rst | 1 + src/gbulb/glib_events.py | 1 + tests/test_glib_events.py | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 changes/176.bugfix.rst diff --git a/changes/176.bugfix.rst b/changes/176.bugfix.rst new file mode 100644 index 0000000..84b1d77 --- /dev/null +++ b/changes/176.bugfix.rst @@ -0,0 +1 @@ +Fix unix socket handling on Python 3.13 diff --git a/src/gbulb/glib_events.py b/src/gbulb/glib_events.py index 0215a37..379a23f 100644 --- a/src/gbulb/glib_events.py +++ b/src/gbulb/glib_events.py @@ -201,6 +201,7 @@ class GLibBaseEventLoopPlatformExt(unix_events.SelectorEventLoop): def __init__(self): self._sighandlers = {} + self._unix_server_sockets = {} def close(self): for sig in list(self._sighandlers): diff --git a/tests/test_glib_events.py b/tests/test_glib_events.py index 04fd2ca..f8b3058 100644 --- a/tests/test_glib_events.py +++ b/tests/test_glib_events.py @@ -1,5 +1,7 @@ import asyncio +import os import sys +import tempfile from unittest import mock, skipIf import pytest @@ -575,3 +577,43 @@ async def run(): assert server_success glib_loop.run_until_complete(run()) + + +def test_unix_sockets(glib_loop): + server_done = asyncio.Event() + server_done._loop = glib_loop + server_success = False + + async def cb(reader, writer): + nonlocal server_success + + writer.write(b"cool data\n") + await writer.drain() + + print("reading") + d = await reader.readline() + print("hrm", d) + server_success = d == b"thank you\n" + + writer.close() + server_done.set() + + async def run(): + with tempfile.TemporaryDirectory() as tmpdir: + path = os.path.join(tmpdir, "socket") + await asyncio.start_unix_server(cb, path) + reader, writer = await asyncio.open_unix_connection(path) + + d = await reader.readline() + assert d == b"cool data\n" + + writer.write(b"thank you\n") + await writer.drain() + + writer.close() + + await server_done.wait() + + assert server_success + + glib_loop.run_until_complete(run()) From de49d849e69c7b60c74c397c143d67d5450ddf3b Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 17 Aug 2024 09:08:52 +0800 Subject: [PATCH 2/2] Apply suggestions from code review --- changes/176.bugfix.rst | 2 +- tests/test_glib_events.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/changes/176.bugfix.rst b/changes/176.bugfix.rst index 84b1d77..c9b8a49 100644 --- a/changes/176.bugfix.rst +++ b/changes/176.bugfix.rst @@ -1 +1 @@ -Fix unix socket handling on Python 3.13 +Unix socket handling was corrected to support changes in Python 3.13. diff --git a/tests/test_glib_events.py b/tests/test_glib_events.py index f8b3058..146c9d5 100644 --- a/tests/test_glib_events.py +++ b/tests/test_glib_events.py @@ -590,9 +590,7 @@ async def cb(reader, writer): writer.write(b"cool data\n") await writer.drain() - print("reading") d = await reader.readline() - print("hrm", d) server_success = d == b"thank you\n" writer.close()