Set TCP_NODELAY and SO_KEEPALIVE on all S7 sockets#677
Merged
Conversation
Eliminates 100-150ms per-exchange latency caused by Nagle's algorithm interacting with TCP delayed ACKs. S7 is a request/response protocol that sends complete PDUs per sendall() call, so Nagle only adds delay. TCP_NODELAY is set on all client-initiated sockets (snap7/connection.py, which is also used by s7/connection.py via ISOTCPConnection) and on all accepted sockets in snap7/server, snap7/partner, and s7/_s7commplus_server. SO_KEEPALIVE is also enabled to detect dead connections during idle periods. Closes #673 https://claude.ai/code/session_01CkAzrsFA7oZHZL7h2JEavb
Without explicit timing parameters, SO_KEEPALIVE uses the OS default of ~2 hours idle before the first probe fires (Linux: tcp_keepalive_time=7200), making it practically useless for PLC connections. Sets TCP_KEEPIDLE=60, TCP_KEEPINTVL=10, TCP_KEEPCNT=3 on platforms that support them (Linux, macOS 10.15+), reducing dead-connection detection to ~90s of idle. Windows keeps OS defaults, which are already more aggressive than Linux defaults. https://claude.ai/code/session_01CkAzrsFA7oZHZL7h2JEavb
The Quick Start previously showed s7.Client as the recommended API, but pip install python-snap7 delivers v3.0 which only has the snap7 package. A user following the README would get an ImportError. - Quick Start now shows only the stable snap7 API (v3.0) - Renamed "Version 3.1 (unreleased)" to "Version 4.0 (unreleased)" - Added an explicit rst note block warning that 4.0 is not yet on PyPI - Moved the s7.Client example and s7CommPlus description into the 4.0 section https://claude.ai/code/session_01CkAzrsFA7oZHZL7h2JEavb
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TCP_NODELAYon all sockets (client, server, partner, S7CommPlus server) to eliminate 100-150ms Nagle's algorithm delaySO_KEEPALIVEon all sockets to detect dead connections during idle periods_tcp_connect) and accepted connections (server/partneraccept())Closes #673. Based on feedback from real PLC testing in #668.
Test plan
tests/test_connection.py🤖 Generated with Claude Code