Skip to content

Conversation

@ralphweng2023
Copy link

Description

Fixes the issue where remote_raspi_init hangs indefinitely on macOS when trying to connect to a remote Raspberry Pi.

Root Cause: SDLNet_TCP_Open is 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:

  1. Create a non-blocking socket using fcntl()/ioctlsocket()
  2. Initiate connection (returns immediately with EINPROGRESS)
  3. Use select() with timeout to wait for connection
  4. Check connection result with getsockopt(SO_ERROR)
  5. If successful, close the test socket and use SDLNet_TCP_Open (which now succeeds immediately)

Changes to network_driver.cpp:

  • Added platform-specific includes for socket operations (fcntl.h, winsock2.h, etc.)
  • Added SK_CONNECTION_TIMEOUT_SECONDS constant (5 seconds)
  • Added _connect_with_timeout() helper function
  • Modified sk_open_tcp_connection() to use timeout for client connections
  • Server socket creation (host=null) still uses the original blocking method

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Built and tested on macOS using CMake:

cd projects/cmake
cmake --preset macOS
cmake --build build/

cd ../../bin
./skunit_tests --order rand
# Result: All tests passed (1263 assertions in 79 test cases)

./skunit_tests "[networking]"
# Result: All tests passed (31 assertions in 2 test cases)

Testing Checklist

  • Code compiles without errors on macOS
  • All existing unit tests pass (79 test cases)
  • All networking tests pass (31 assertions)
  • Connection timeout works correctly (5 second limit)
  • Tested on Linux/WSL (not available on this machine)
  • Tested on Windows/MSYS2 (not available on this machine)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • My changes generate no new warnings
  • Existing tests continue to pass
  • The fix is backward compatible with existing code

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

2 participants