Repository navigation
[c++] fix socket timeout on POSIX systems - #7137
Conversation
SO_RCVTIMEO requires struct timeval on POSIX, not int. The current code silently fails with EINVAL on macOS/Linux. Also fixes inconsistent units in listener timeout (was passing minutes instead of milliseconds).
jameslamb
left a comment
There was a problem hiding this comment.
Thank you for this!
Very good fix. It matches what I see at https://linux.die.net/man/3/setsockopt
SO_RCVTIMEO
Sets the timeout value that specifies the maximum amount of time an input function waits until it completes. It accepts a timeval structure with the number of seconds and microseconds specifying the limit on how long to wait for an input operation to complete. If a receive operation has blocked for this much time without receiving additional data, it shall return with a partial count or errno set to [EAGAIN] or [EWOULDBLOCK] if no data is received. The default for this option is zero, which indicates that a receive operation shall not time out. This option takes a timeval structure. Note that not all implementations allow this option to be set.
I'd be happy to merge this, and if it really does help with #4074 that would be great!
|
|
||
| // start listener | ||
| listener_->SetTimeout(socket_timeout_); | ||
| listener_->SetTimeout(socket_timeout_ * 1000 * 60); |
There was a problem hiding this comment.
WOW thank you for catching this!
Fix inconsistent units in listener timeout (was passing minutes instead of milliseconds)
Indeed, I looked around like this:
get grep SetTimeoutAnd found that the other use does appropriately convert to milliseconds.
| } | ||
| inline void SetTimeout(int timeout) { | ||
| setsockopt(sockfd_, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout)); | ||
| inline void SetTimeout(int timeout_ms) { |
There was a problem hiding this comment.
Just want to say I really appreciate the name change here. Arguments like this should have units on them.
I wish I could go back in time and change LightGBM's public timeout parameter to timeout_seconds (both accepting seconds and making the unit part of the name).
Summary
SO_RCVTIMEOto usestruct timevalon POSIX (macOS/Linux) instead ofintProblem
On POSIX systems,
setsockopt(SO_RCVTIMEO)requires astruct timeval, not anint. The current code silently fails withEINVALon macOS/Linux, meaning socket timeouts are never actually set.Verification (run on Linux):
Changes
src/network/socket_wrapper.hpp:#include <sys/time.h>for POSIXstruct timevalforSO_RCVTIMEOon POSIXDWORDfor Windows (unchanged behavior)src/network/linkers_socket.cpp:listener_->SetTimeout(socket_timeout_ * 1000 * 60)to match line 139Test Plan
Related Issue
Contributes to: #4074