Skip to content

[c++] fix socket timeout on POSIX systems - #7137

Merged
jameslamb merged 1 commit into
lightgbm-org:masterfrom
wagner-austin:fix-posix-socket-timeout
Jan 23, 2026
Merged

jameslamb merged 1 commit into
lightgbm-org:masterfrom
wagner-austin:fix-posix-socket-timeout

Conversation

@wagner-austin

Copy link
Copy Markdown
Contributor

Summary

  • Fix SO_RCVTIMEO to use struct timeval on POSIX (macOS/Linux) instead of int
  • Fix inconsistent units in listener timeout (was passing minutes instead of milliseconds)

Problem

On POSIX systems, setsockopt(SO_RCVTIMEO) requires a struct timeval, not an int. The current code silently fails with EINVAL on macOS/Linux, meaning socket timeouts are never actually set.

Verification (run on Linux):

import socket, struct
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Current code (fails)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, struct.pack('i', 5000))
# OSError: [Errno 22] Invalid argument

# Fixed code (works)  
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, struct.pack('ll', 5, 0))
# Success

Changes

src/network/socket_wrapper.hpp:

  • Add #include <sys/time.h> for POSIX
  • Use struct timeval for SO_RCVTIMEO on POSIX
  • Keep DWORD for Windows (unchanged behavior)

src/network/linkers_socket.cpp:

  • Fix line 187: listener_->SetTimeout(socket_timeout_ * 1000 * 60) to match line 139

Test Plan

  • Pre-commit passes
  • Windows build (MSVC) compiles
  • Linux build (GCC) compiles
  • C++ unit tests pass (31/31)
  • Verified fix works on Linux via Python socket test

Related Issue

Contributes to: #4074

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).
@wagner-austin wagner-austin changed the title [c++] Fix socket timeout on POSIX systems [c++] fix socket timeout on POSIX systems Jan 22, 2026

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 SetTimeout

And found that the other use does appropriately convert to milliseconds.

https://github.com/microsoft/LightGBM/blob/6af94aadf37b183fe6ad671141d4eeafdcc8c1e3/src/network/linkers_socket.cpp#L139

}
inline void SetTimeout(int timeout) {
setsockopt(sockfd_, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char*>(&timeout), sizeof(timeout));
inline void SetTimeout(int timeout_ms) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@jameslamb jameslamb added the fix label Jan 22, 2026
@jameslamb
jameslamb merged commit 80ab6d3 into lightgbm-org:master Jan 23, 2026
wagner-austin added a commit to wagner-austin/LightGBM that referenced this pull request Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants