Skip to content

Set TCP_NODELAY and SO_KEEPALIVE on all S7 sockets#677

Merged
gijzelaerr merged 4 commits into
masterfrom
claude/implement-socket-flags-Z5N6I
Apr 15, 2026
Merged

Set TCP_NODELAY and SO_KEEPALIVE on all S7 sockets#677
gijzelaerr merged 4 commits into
masterfrom
claude/implement-socket-flags-Z5N6I

Conversation

@gijzelaerr
Copy link
Copy Markdown
Owner

Summary

  • Set TCP_NODELAY on all sockets (client, server, partner, S7CommPlus server) to eliminate 100-150ms Nagle's algorithm delay
  • Set SO_KEEPALIVE on all sockets to detect dead connections during idle periods
  • Configure keepalive timing (60s idle, 10s interval, 3 probes = ~90s detection) on platforms that support it (Linux, macOS 10.15+)
  • Applied to both outgoing connections (_tcp_connect) and accepted connections (server/partner accept())

Closes #673. Based on feedback from real PLC testing in #668.

Test plan

  • New unit tests for socket options in tests/test_connection.py
  • Verify no regressions on all platforms (Linux, macOS, Windows)
  • Confirm latency improvement with real PLC

🤖 Generated with Claude Code

claude and others added 4 commits April 15, 2026 04:18
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>
@gijzelaerr gijzelaerr merged commit 4f358b7 into master Apr 15, 2026
38 checks passed
@gijzelaerr gijzelaerr added this to the 4.0 milestone Apr 15, 2026
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.

Optimize TCP socket options for low-latency S7 communication

2 participants