Skip to content

Add max_connections to the tcp/tls/udp server - #3107

Merged
janiversen merged 2 commits into
pymodbus-dev:devfrom
imeerkhurram:fix/limit-tcp-connections
Dec 1, 2026
Merged

janiversen merged 2 commits into
pymodbus-dev:devfrom
imeerkhurram:fix/limit-tcp-connections

Conversation

@imeerkhurram

@imeerkhurram imeerkhurram commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the connection limit part of #3103.

ModbusTcpServer, ModbusTlsServer and ModbusUdpServer get a documented parameter max_connections (default 10, 0 = no limit). A value below 0 or above 1000 raises ParameterException. Serial servers have no limit. ServerRequestHandler is unchanged.

The limit is handled in transport.py. handle_new_connection checks it and, when the listener is full, returns a RefusedConnection, which closes the connection. It can't return None: 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_connections in connection_made, because asyncio reports nothing when a TLS handshake fails. On dev such a connection stays in active_connections forever, so a few failed handshakes would lock out every client. asyncio calls handle_new_connection for a whole batch of accepted connections before the first one is established, so connection_made checks 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) in test_server_asyncio.py used 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.

@janiversen janiversen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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:

Comment thread API_changes.rst Resolved
Comment thread pymodbus/server/requesthandler.py Resolved
@imeerkhurram imeerkhurram changed the title Limit the tcp/tls server to 10 simultaneous connections Add max_connections to the tcp/tls server Nov 29, 2026
@imeerkhurram
imeerkhurram force-pushed the fix/limit-tcp-connections branch from 5dac544 to d31af9b Compare November 29, 2026 14:41
@imeerkhurram

Copy link
Copy Markdown
Contributor Author

Thanks for the review, you're right. I reworked it: max_connections is now a documented parameter of the tcp/tls server, the server object decides, and ServerRequestHandler is unchanged.

One thing differs from your suggestion. I put the check where the connection is registered, after it is established, and not in callback_new_connection. asyncio calls callback_new_connection before the TLS handshake and reports nothing when the handshake fails, so those connections stay in active_connections. That already happens on dev, and my first version made it worse: 12 failed handshakes locked out a valid client. The description has the details. If you prefer another place for it, I'm happy to move it.

I left max_idle for a separate PR, as you suggested.

@janiversen

Copy link
Copy Markdown
Collaborator

??? callback_new_connection() is called from transport.py not asyncio !

Comment thread API_changes.rst Resolved
Comment thread pymodbus/server/server.py Resolved
Comment thread pymodbus/server/base.py Resolved
Comment thread pymodbus/server/base.py Resolved
Comment thread pymodbus/server/base.py Resolved
Comment thread pymodbus/transport/transport.py Resolved
Comment thread pymodbus/transport/transport.py Resolved
Comment thread pymodbus/server/base.py Resolved

@janiversen janiversen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

made the wrong click, the pr still need changes.

@imeerkhurram imeerkhurram changed the title Add max_connections to the tcp/tls server Add max_connections to the tcp/tls/udp server Nov 30, 2026
@imeerkhurram

Copy link
Copy Markdown
Contributor Author

You're right, sorry: asyncio calls handle_new_connection, which then calls callback_new_connection. I mixed them up. I pushed d7c3340 for your comments:

  • The server classes only pass max_connections on in CommParams. register_connection is gone and the limit is handled in transport.py.
  • handle_new_connection checks the limit. I couldn't return None there: asyncio then raises inside the transport and leaves the tcp socket open, so the client is connected and waits forever. I tried it on Python 3.10 to 3.14, always the same on tcp (with TLS the connection is closed). So it returns a small RefusedConnection, which aborts the connection.
  • connection_made checks it once more and uses self.listener directly. Without that, connections accepted at the same time all get in, because asyncio calls handle_new_connection for all of them before the first is established.
  • The docstring is a one-liner.
  • The value is checked, outside 0 to 1000 raises ParameterException. 1000 is my guess, tell me if you want another limit.
  • ModbusUdpServer has the parameter. It has no effect yet: for udp asyncio calls handle_new_connection once, for the socket, so active_connections never has more than one entry, however many clients there are. Counting clients by address would be a change in the request handler. Do you want that, or should I take the parameter out of the udp server again?

@janiversen janiversen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks.

@janiversen

Copy link
Copy Markdown
Collaborator
  • handle_new_connection checks the limit. I couldn't return None
    Sorry I relied on my memory, without checking the code, your solution is nice.
  • ModbusUdpServer has the parameter. It has no effect yet: for udp asyncio calls handle_new_connection once, for the socket, so active_connections never has more than one entry, however many clients there are. Counting clients by address would be a change in the request handler. Do you want that, or should I take the parameter out of the udp server again?
    We have seen that sometimes, suppose depending on operating system and hardware, there can be more than one socket. I do believe it also depend on how the client define the socket, we had a case a couple of years back where a windows machine had a socket pr client for whatever reason.

Please let it stay.

Connections are registered when they are established, so failed TLS
handshakes are neither counted nor left in active_connections.
@janiversen
janiversen force-pushed the fix/limit-tcp-connections branch from d7c3340 to 9df736b Compare December 1, 2026 09:25
@janiversen
janiversen merged commit 6c3f68c into pymodbus-dev:dev Dec 1, 2026
16 checks passed
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