Skip to content

Close the TCP connection instead of sending a disconnect request - #987

Merged
gijzelaerr merged 5 commits into
masterfrom
fix/disconnect-close-sequence
Nov 23, 2026
Merged

gijzelaerr merged 5 commits into
masterfrom
fix/disconnect-close-sequence

Conversation

@gijzelaerr

@gijzelaerr gijzelaerr commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Refs #972. A CP 343-1 keeps a connection resource after each disconnect(), until the PLC restarts.

Confirmed by the reporter on 20 November: on a CPU 313C behind a CP 343-1 the connection count went to 2 and back to 1 with this branch. The S7-314 has not been re-tested with this version.

What the captures show

Three captures from the reporter, each with a different version of our close sequence:

  1. Disconnect request to the wrong reference, FIN right behind it: the CP resets (fixed in Address the peer's COTP reference in the disconnect request #979).
  2. Disconnect request to the right reference, FIN 48 µs behind it: the CP resets.
  3. Disconnect request without the stray byte, and no FIN while we wait for a confirm (the first version of this PR): the CP still resets 1 ms after the request, never sends a disconnect confirm, and 430 ms later still acks on that connection.

So the CP 343-1 rejects the disconnect request itself.

Change, in both clients

disconnect() no longer sends a COTP disconnect request. It closes its sending side of the TCP connection (FIN), waits up to DISCONNECT_CLOSE_TIMEOUT (1 s, and at most the connection timeout) for the peer to close its own, and then closes the socket.

Two reasons to expect this is the right release:

  • RFC 1006 releases a transport connection by closing the TCP connection, and does not use a disconnect request for it.
  • Native Snap7 does the same for a client: TSnap7Peer::PeerDisconnect() calls isoDisconnect(true), the "only TCP disconnection" path that sends no disconnect request.

The peer reference from #979 is still parsed; nothing sends it any more.

Not covered

  • disconnect() and Partner.stop() take up to one second against a peer that does not close its side. Two partners in one process do exactly that, so tests/test_partner.py shortens the wait with a fixture; without it the partner tests take 22 s instead of 7 s.

Testing

  • Socket-pair tests: the peer sees a FIN and no data before it, can still send after our FIN, disconnect() returns as soon as the peer closes, a silent peer is bounded by the timeout, data in flight is discarded, and the socket is closed when the shutdown fails.
  • The same for the async client against a local TCP server.
  • Full suite: 1671 passed, 62 skipped.

A CP 343-1 kept the connection's resource after disconnect() even with
the DR addressed to its reference (#979): the capture shows our FIN 7 us
after the DR and the CP answering both with a reset, never with a DC.
disconnect() now waits up to one second for a DC, or for the peer to
close or reset the connection, before closing the socket, in both
clients.

The DR also carried a zero byte after its header, outside the length
indicator. Class 0 allows no user data in a DR, so it is dropped.

Refs #972.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gijzelaerr
gijzelaerr force-pushed the fix/disconnect-close-sequence branch from 488de24 to db0747e Compare November 11, 2026 13:41
Merges master and reworks the fix after the third capture in #972: with
the wait for a disconnect confirm in place, the CP 343-1 still answered
the COTP disconnect request with a reset and kept the connection.

RFC 1006 releases a connection by closing the TCP connection. disconnect()
now sends no disconnect request: it closes its sending side, waits up to
DISCONNECT_CLOSE_TIMEOUT (1 s, and at most the connection timeout) for the
peer to close its own, and then closes the socket. Both clients.
@gijzelaerr gijzelaerr changed the title Wait for the peer to confirm the disconnect request Close the TCP connection instead of sending a disconnect request Nov 13, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gijzelaerr and others added 2 commits November 23, 2026 16:14
…rmation

The entry sat under 3.6.0 after merging master, which does not contain this
change. The reporter confirmed in #972 that a CP 343-1 now releases the
connection.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@gijzelaerr
gijzelaerr enabled auto-merge (squash) November 23, 2026 14:17
@gijzelaerr
gijzelaerr merged commit 00d1f19 into master Nov 23, 2026
20 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.

1 participant