Skip to content

Harden TCP/UDP socket loops and use RTHS-friendly network defaults#12

Open
joecga4 wants to merge 1 commit into
aschellenberg74:masterfrom
joecga4:fix/socket-robustness-rths-defaults
Open

Harden TCP/UDP socket loops and use RTHS-friendly network defaults#12
joecga4 wants to merge 1 commit into
aschellenberg74:masterfrom
joecga4:fix/socket-robustness-rths-defaults

Conversation

@joecga4

@joecga4 joecga4 commented Jul 6, 2026

Copy link
Copy Markdown

Socket fixes in the vendored SRC/openseesExtra copies and the simApplicationClient C stubs, from profiling real-time hybrid simulation runs. The same changes are proposed for OpenSees's SRC/actor/channel originals in aschellenberg74/OpenSees#6 so host and vendored sockets stay consistent.

Robustness (bug fixes)

  • The 9 recv/send loops in TCP_Socket never check the return value: peer close (recv() == 0) spins forever, and on error (−1) the data pointer moves backwards while the byte count grows (memory corruption). All loops now treat <= 0 as an error and return −1, retry on EINTR, and the send paths restore the caller's byte-swapped data before bailing out.
  • Same treatment for the 8 recvfrom/sendto chunk loops in UDP_Socket (which now also advance by actual byte count).
  • TCP_SocketSSL treated SSL_read() == 0 (clean shutdown) as success; checks are now <= 0.
  • The C client stubs chunked UDP at 512 bytes while the C++ side used 9126, so a C++→C message over 512 bytes was silently truncated; both sides now use the same constant.

Latency/reliability defaults

  • TCP_NODELAY on by default in TCP_Socket/TCP_SocketSSL and the C stubs (Nagle + delayed-ACK added ~40 ms stalls per step).
  • MAX_UDP_DATAGRAM 9126 → 1432 so a logical datagram fits in one MTU; one lost IP fragment used to drop the whole message. Both ends of a UDP channel must be rebuilt together.
  • UDP_Socket requests 512 KB SO_RCVBUF/SO_SNDBUF in its three constructors.

Measured: OneBayFrame distributed (3 processes), max DAQ round-trip 1224 → 82 µs; local and distributed recorder outputs bit-identical to the pre-change baseline; loadPackage integration test against a host OpenSeesPy OK.

🤖 Generated with Claude Code

Robustness (bug fixes):
- The 9 recv/send loops in the vendored TCP_Socket never checked the
  return value: peer close (recv() == 0) spins forever and an error
  (-1) moves the data pointer backwards while growing the byte count
  (memory corruption). All loops now treat <= 0 as an error and return
  -1, retry on EINTR, and the send paths restore the caller's
  byte-swapped data before bailing out.
- Same treatment for the 8 recvfrom/sendto chunk loops in UDP_Socket,
  which now advance by the actual byte count.
- TCP_SocketSSL treats SSL_read() == 0 (clean shutdown) as an error
  (was only < 0).
- The C client stubs chunked UDP at 512 bytes while the C++ side used
  9126: a C++->C message over 512 bytes was silently truncated. Both
  sides now use the same value.

Latency/reliability defaults (hybrid-simulation traffic is small
request/response messages every integration step):
- TCP_NODELAY on by default in TCP_Socket/TCP_SocketSSL and in the C
  client stubs (Nagle + delayed-ACK added ~40 ms stalls per step).
- MAX_UDP_DATAGRAM 9126 -> 1432 so a logical datagram fits in one MTU
  (one lost IP fragment dropped the whole message). Socket.h,
  Socket.cpp and the C stubs stay in sync; both ends of a UDP channel
  must be rebuilt together.
- UDP_Socket requests 512 KB SO_RCVBUF/SO_SNDBUF in its three
  constructors so per-step datagram bursts are not dropped.

Measured on OneBayFrame distributed (3 processes): max DAQ round-trip
1224 -> 82 us; local and distributed outputs bit-identical to the
pre-change baseline. The same fixes are proposed for OpenSees's
SRC/actor/channel copies so host and vendored sockets stay consistent.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant