Skip to content

Update UnixSocketMixin to disable socket cleanup on Py >= 3.13#592

Open
cuu508 wants to merge 5 commits into
aio-libs:masterfrom
cuu508:fix_test_unixsocket_on_py313
Open

Update UnixSocketMixin to disable socket cleanup on Py >= 3.13#592
cuu508 wants to merge 5 commits into
aio-libs:masterfrom
cuu508:fix_test_unixsocket_on_py313

Conversation

@cuu508
Copy link
Copy Markdown
Contributor

@cuu508 cuu508 commented May 9, 2026

What do these changes do?

This change fixes the only failing test on Python 3.13:

aiosmtpd/tests/test_server.py::TestUnthreaded::test_unixsocket

This test starts an Unix listener, exercises it, closes it and checks if the socket still exists on the filesystem. The socket does exist on Python 3.12 and below (the test passes), but does not exist on Python 3.13 (the test fails).

This is because Python added automatic socket cleanup: python/cpython#111246

There's a new cleanup_socket=False argument for create_unix_server which restores the old behavior (docs). I think it makes sense for aiosmtpd to use it, to keep the shutdown behavior consistent between Python versions.

Are there changes in behavior for the user?

Yes:

  • before the change, on Python 3.13, if the library user starts and stops a unix socket listener, the socket is automatically cleaned up.
  • after the change, on Python 3.13, if the library user starts and stops a unix socket listener, the socket is left on the filesystem (matching the Python 3.12 and below behavior).

There's no change when using Python 3.12 and below.

Related issue number

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • tox testenvs have been executed in the following environments:
    • Ubuntu 24.04, python 3.12 and python 3.13
  • Documentation reflects the changes
  • Add a news fragment into the NEWS.rst file

@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.81%. Comparing base (dc78ef6) to head (3587c30).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #592      +/-   ##
==========================================
+ Coverage   97.75%   97.81%   +0.05%     
==========================================
  Files          23       23              
  Lines        5709     5712       +3     
  Branches      351      352       +1     
==========================================
+ Hits         5581     5587       +6     
+ Misses         79       75       -4     
- Partials       49       50       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cuu508 cuu508 force-pushed the fix_test_unixsocket_on_py313 branch from 2707444 to 7eda3d4 Compare May 9, 2026 11:24
cuu508 added 4 commits May 13, 2026 17:24
Starting from Python 3.13, the unix listener automatically cleans up
its socket from the filesystem on shutdown. This can be disabled by
passing in cleanup_socket=False. I think it makes sense for aiosmtpd
to do that, to keep the shutdown behavior consistent between Python versions.

This change fixes the only failing test on Python 3.13:

aiosmtpd/tests/test_server.py::TestUnthreaded::test_unixsocket
Passing *kw to `create_unix_server` works at runtime but I'm not
sure how to annotate it correctly.

Added "type: ignore" to silence mypy warning about unrecognized
cleanup_socket argument:
python/typeshed#15742
@cuu508 cuu508 force-pushed the fix_test_unixsocket_on_py313 branch from 74918fa to 95b926f Compare May 13, 2026 14:24
For some unclear reason, if:

* tests are run on python 3.13
* and the system has a proper, non-loopback IPv6 address
* and inside tests we mock the socket.recv() function

then `asyncio.base_events.Server._wakeup` gets called twice
and throws an exception on the second call.

As a workaround, in test_controller.py, mock something adjacent
to socket.recv – the socket.create_connection function.
Comment thread aiosmtpd/tests/test_server.py
Copy link
Copy Markdown
Member

@webknjaz webknjaz left a comment

Choose a reason for hiding this comment

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

I think the overall approach is fine as a compatibility fix. But long-term, it may make sense to flip the behavior down the line. Or at least provide a way to opt into this behavior and/or have a CM interface for this.

In general, I'd expect servers to clean garbage they've produced, upon completion.

Comment thread aiosmtpd/controller.py
return self.loop.create_unix_server(
self._factory_invoker,
path=self.unix_socket,
ssl=self.ssl_context,
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.

It'd be cleaner to have

Suggested change
ssl=self.ssl_context,
**_create_unix_server_trailing_kwargs,

and compute the var conditionally with {} for older versions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

IIRC I initially tried that but couldn't make mypy happy.

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.

2 participants