Open
Conversation
- Add non-blocking TCP connection with 5 second timeout - Add _connect_with_timeout() helper using native sockets and select() - Server socket creation still uses blocking SDLNet_TCP_Open - Client connections now use timeout to prevent indefinite hangs Root cause: SDLNet_TCP_Open blocks indefinitely on macOS when host is unreachable. Solution: Use non-blocking connect() with select() to verify connectivity before using SDLNet_TCP_Open. Fixes remote_raspi_init() hanging on macOS.
222448082Ashen
approved these changes
Jan 5, 2026
222448082Ashen
left a comment
There was a problem hiding this comment.
Detailed Feedback
-
Architecture & Simplicity:
- The "double connect" strategy (connect with raw socket -> close -> connect with SDL_net) is a pragmatic solution. While it does involve two TCP handshakes, it ensures compatibility with the existing
SDL_netarchitecture without rewriting the entire networking stack. - Note: Be aware that some strict server-side firewalls or rate limiters might flag two rapid connections from the same IP as suspicious, but for the intended use case (Raspberry Pi connection), this is unlikely to be an issue.
- The "double connect" strategy (connect with raw socket -> close -> connect with SDL_net) is a pragmatic solution. While it does involve two TCP handshakes, it ensures compatibility with the existing
-
Code Quality & Readability:
- The extraction of
_connect_with_timeoutinto a static helper function is excellent. It keepssk_open_tcp_connectionclean and readable. - Platform-specific includes and socket flags are correctly guarded with
#ifdef _WIN32/#else, maintaining cross-platform compatibility.
- The extraction of
-
Maintainability:
- Restoring the socket to blocking mode at the end of
_connect_with_timeout(lines 190-196) is good practice for function reusability, even though the socket is closed immediately in the current usage. This prevents future bugs if the helper is used elsewhere.
- Restoring the socket to blocking mode at the end of
-
Performance:
- The
inet_ptoncheck (lines 117-120) is a good optimization to avoid unnecessary DNS lookups for IP addresses. - There is a minor redundancy where
SDLNet_ResolveHostmight be called twice (once in the helper, once in the main function) for hostnames, but the performance impact is negligible compared to the network latency.
- The
-
Testing:
- The PR description indicates thorough testing on macOS. Since this code involves platform-specific socket calls (
fcntlvsioctlsocket), reliance on the manual verification described is high. The logic appears correct for both POSIX and Windows sockets.
- The PR description indicates thorough testing on macOS. Since this code involves platform-specific socket calls (
Checklist Verification
- Code Quality: Clean and modular.
- Functionality: Addresses the root cause (blocking connect).
- Edge Cases: Handles timeouts and immediate failures correctly.
- Backward Compatibility: Server creation (
host == nullptr) path is untouched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes the issue where
remote_raspi_inithangs indefinitely on macOS when trying to connect to a remote Raspberry Pi.Root Cause:
SDLNet_TCP_Openis a blocking function with no timeout. On macOS, when the host is unreachable, it hangs indefinitely (potentially for minutes due to OS-level TCP timeout settings).Solution: Implement a non-blocking TCP connection with a 5-second timeout using native socket calls:
fcntl()/ioctlsocket()EINPROGRESS)select()with timeout to wait for connectiongetsockopt(SO_ERROR)SDLNet_TCP_Open(which now succeeds immediately)Changes to
network_driver.cpp:fcntl.h,winsock2.h, etc.)SK_CONNECTION_TIMEOUT_SECONDSconstant (5 seconds)_connect_with_timeout()helper functionsk_open_tcp_connection()to use timeout for client connectionsType of change
How Has This Been Tested?
Built and tested on macOS using CMake:
Testing Checklist
Checklist