Repository navigation
Add max_connections to the tcp/tls/udp server - #3107
Conversation
janiversen
left a comment
There was a problem hiding this comment.
The implementation is wrong, but the idea is good.
To be effective this probably needs another parameter like e.g. max_idle, to monitor and close inactive connections (this is a change in requesthandler). Such a change is another PR:
5dac544 to
d31af9b
Compare
|
Thanks for the review, you're right. I reworked it: One thing differs from your suggestion. I put the check where the connection is registered, after it is established, and not in I left |
|
??? callback_new_connection() is called from transport.py not asyncio ! |
janiversen
left a comment
There was a problem hiding this comment.
made the wrong click, the pr still need changes.
|
You're right, sorry: asyncio calls
|
Please let it stay. |
Connections are registered when they are established, so failed TLS handshakes are neither counted nor left in active_connections.
d7c3340 to
9df736b
Compare
Fixes the connection limit part of #3103.
ModbusTcpServer,ModbusTlsServerandModbusUdpServerget a documented parametermax_connections(default 10, 0 = no limit). A value below 0 or above 1000 raisesParameterException. Serial servers have no limit.ServerRequestHandleris unchanged.The limit is handled in
transport.py.handle_new_connectionchecks it and, when the listener is full, returns aRefusedConnection, which closes the connection. It can't returnNone: asyncio then leaves the tcp socket open and the client hangs (checked on Python 3.10 to 3.14).A connection is registered in
active_connectionsinconnection_made, because asyncio reports nothing when a TLS handshake fails. Ondevsuch a connection stays inactive_connectionsforever, so a few failed handshakes would lock out every client. asyncio callshandle_new_connectionfor a whole batch of accepted connections before the first one is established, soconnection_madechecks the limit again.A refused connection is aborted, is never registered, calls no trace callback, and its data is dropped. The last part is needed on Python 3.10, where asyncio delivers data on a TLS connection after
abort(), so a refused client could still write a register.Tests: limit on TCP and TLS, simultaneous connections, no limit, illegal values, failed TLS handshakes, and refused connections.
start_server(do_tls=True)intest_server_asyncio.pyused a client TLS context, so no handshake could succeed; it now uses a server context.The tests pass on Python 3.10, 3.12 and 3.14, and merged with current
dev; ruff and zuban are clean.Closing inactive connections (
max_idle) is left for a separate PR.