Skip to content

Start the TLS test emulators on ports the OS picks at bind time - #132

Merged
gijzelaerr merged 1 commit into
masterfrom
fix/tls-test-ports
Oct 24, 2026
Merged

gijzelaerr merged 1 commit into
masterfrom
fix/tls-test-ports

Conversation

@gijzelaerr

Copy link
Copy Markdown
Owner

Summary

tests/test_s7_tls.py picked four free ports when the module was imported and bound them much later, when each fixture started its emulator. In between, the OS can hand one of those ports to another socket: get_free_tcp_ports() draws them from the ephemeral range, which outgoing client connections also use. When that happens, every test of the fixture fails with OSError: [Errno 98] Address already in use. It happened on the #102 CI run (TestAsyncClientV2WithTLS, 9 errors, ubuntu-22.04 / Python 3.10), and a re-run passed.

The emulator can now report the port it actually bound. S7CommPlusServer.port is the port while it runs (0 otherwise), and start(port=0) lets the OS pick one at bind time. The TLS fixtures start on port 0, and the tests connect to srv.port, so there is no window between choosing and binding.

Other test modules still pick ports in advance with get_free_tcp_ports(). They hold the port for much less time and haven't been seen failing. Moving them over can follow, now that the server supports it.

Sources

None; no protocol values change.

Tests

  • tests/test_s7_tls.py passes 35/35, run three times. tests/test_no_fixed_test_ports.py and tests/test_s7_server.py pass.
  • Full suite: 2350 passed. mypy s7commplus, ruff check, ruff format --check and pre-commit run --all-files are clean.

Changelog

Under New features:

  • Server.port is the TCP port the emulator listens on while it runs, and
    Server.start(port=0) lets the OS pick a free one, so tests can run
    emulators side by side without choosing ports in advance.

Checklist

  • pytest, mypy s7commplus, ruff check and ruff format --check pass
  • The Changelog section above describes every user-visible change, and CHANGES.md is unchanged
  • README.md, __all__ and the docs are updated for public API changes
  • Parsers treat PLC data as untrusted (lengths, offsets and truncation are checked)

🤖 Generated with Claude Code

test_s7_tls.py picked four free ports when the module was imported and
bound them minutes later. In between, the OS could hand one of them to
another socket (they come from the ephemeral range clients also use), and
every test of that fixture failed with "Address already in use"; this
happened on the #102 CI run. The fixtures now start the emulator on port
0 and the tests connect to the new S7CommPlusServer.port, the port it
actually bound, so there is no window left.
@gijzelaerr
gijzelaerr merged commit b058e2d into master Oct 24, 2026
12 checks passed
@gijzelaerr
gijzelaerr deleted the fix/tls-test-ports branch October 24, 2026 09:22
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.

1 participant