Repository navigation
Conversation
When self.ignore_rcv_tout == True: self.connected is not set to False opon receicve timeout
New helper: set_socket_timeout()
pre-commit fix
pre-commit fix
|
Alternative solution: We could pass an argument from
It might also be a cleaner approach than using a variable. |
gijzelaerr
left a comment
There was a problem hiding this comment.
Thanks, @hs2bws. A get_status() that notices a lost connection is worth having. I ran this branch against a socket pair (two Partner objects wired as in tests/test_partner.py), and a few things need to change before it can go in.
Your question: flag or argument
The argument, please. A keyword on receive_data() and _recv_exact() (for example ignore_timeout: bool = False) carries no state. The flag can be left set: conn.ignore_rcv_tout = True runs before the inner try, so if gettimeout() or settimeout() raises, nothing resets it. A new keyword with a default does not break existing callers.
Problems
-
set_socket_timeout()has no effect while the listener runs. It does not take_io_lock, so thefinallyin_recv_listener()puts the old timeout back over it. Afteras_b_recv()and thenset_socket_timeout(3.0), the socket's timeout was stillNone. It also leavesconn.timeoutat 5.0, so the two disagree. -
set_socket_timeout(0)is accepted. That makes the socket non-blocking. The nextb_recv()returned -1 and the status dropped to 1. Please rejecttimeout <= 0. -
An ordinary receive timeout now reads as a lost connection. A synchronous
b_recv()on an idle link times out,receive_data()setsconn.connected = False, and with the new checkget_status()returns 1 although the TCP connection is fine. On master it returns 2, but the connection object is already unusable there (the nextb_recv()returns -1 with "Not connected"), so this is an existing bug that the new check makes visible. The same applies to a timeout whileb_send()waits for its acknowledgement. Ifget_status()is going to readconn.connected, a timeout outside the listener must not clear it either, or it needs handling inb_recv()andb_send(). -
A peer that closes is still not detected. When the other partner called
stop(), the listener ended with "Invalid COTP DT header length" andget_status()still returned 2: that path does not clearconn.connected. I ran this before #987 was merged, which changes whatdisconnect()sends, so it is worth re-checking on current master.
Smaller points
- The race in the description is not on master:
get_status()there never readsconn.connected. The race only exists once this PR adds that check, so the description should say that the PR makesget_status()follow the connection's state and avoids a false "running" while the listener times out. get_status()setsself.connected = Falseas a side effect and without a lock. I would rather it stayed a pure read, for example by reporting connected only when bothself.connectedandself._connection.connectedare true, or have the code that detects the loss clear the flag.- There are no tests. Please add at least: the status after the listener has timed out a few times (still 2), the status after the connection is lost, and
set_socket_timeout()with and without a running listener and with an invalid value. set_socket_timeout()is a second concern. What is it for?recv_timeoutand the connection'stimeoutalready exist. If it is needed, a separate PR would be easier to review. It also counts a call on an unconnected partner as a send error, which it is not.- Typos: "opon receicve" in
connection.py, "timetout" in the docstring. CHANGES.md: the entry leaves out theget_status()change, which is the part users will notice, and has no PR number. It now conflicts with master after #987.
|
Thanks @gijzelaerr for the reply and your suggestions. Regarding the socket timeout: I recognize the shortcomings and will revise it. The default value of 5 seconds is somewhat too long for certain time-critical PLC applications. If no response is received within 100 ms of a get_status(): How can we reliably determine whether a connection to the peer is still active? What about the other Snap7 packages (client, server, etc.): does it actually make sense to set |
|
Thanks, @hs2bws, the cable-pull case is exactly the one this needs to handle. Should a timeout set For the client, a timeout waiting for a reply is less clear-cut: the reply can still arrive later and would then be read as the answer to the next request. The client checks PDU references for some requests, but not all, so I would leave its behaviour alone in this PR. How to tell whether the peer is still there. TCP alone does not notice a pulled cable until it next tries to send and its retransmissions give up, which can take minutes. That is why your
After the link comes back, the old TCP connection does not recover; a new one has to be set up, which is why only One more thing I found while looking at this: Suggested shape, in this PR or split up as you prefer:
|
In the event of a connection interruption,
_connection.connectedis set tofalse. This is checked within theget_status()function.The issue is that, inside
_recv_listener(), a socket timeout causes_connection.connectedto be set tofalseand then immediately back totrue. Ifget_status()happens to be called at that exact moment, it will return a status of1(running).By setting
connection.ignore_rcv_tout = True, it is now possible to prevent_connection.connectedfrom being set tofalsewhen a receive timeout occurs.