Skip to content

fix(udp): RAII thread ownership, FD_SETSIZE guards, and safe stop()#1213

Open
ruck314 wants to merge 6 commits into
pre-releasefrom
bug-audit/udp
Open

fix(udp): RAII thread ownership, FD_SETSIZE guards, and safe stop()#1213
ruck314 wants to merge 6 commits into
pre-releasefrom
bug-audit/udp

Conversation

@ruck314
Copy link
Copy Markdown
Contributor

@ruck314 ruck314 commented May 5, 2026

Summary

  • Replace raw new/delete std::thread with std::unique_ptr<std::thread> in udp::Client and udp::Server (RAII ownership eliminates leak risk on future throw paths)
  • Add FD_SETSIZE bounds check in constructors — reject sockets with fd >= FD_SETSIZE at construction rather than crashing asynchronously in the worker thread
  • Add defensive FD_SETSIZE guards before every FD_SET(fd_, ...) call in acceptFrame() and runThread()runThread() now logs and exits cleanly instead of calling std::terminate
  • Guard stop() on thread_->joinable() to prevent std::terminate on double-stop or stop-before-start

Files changed

  • include/rogue/protocols/udp/Core.hthread_ type: raw ptr → unique_ptr
  • src/rogue/protocols/udp/Client.cpp — ctor guard, RAII thread, safe stop, runThread exit
  • src/rogue/protocols/udp/Server.cpp — ctor guard, RAII thread, safe stop, runThread exit

Tests

No new tests in this PR. The original C++ audit-style tests
(test_udp_fd_setsize_audit_repro.cpp,
test_udp_raw_thread_audit_repro.cpp) were source-text greps against
Client.cpp/Server.cpp and have been removed per review feedback —
they are too brittle to be useful as CI checks. Functional UDP
coverage at this level (oversized fd injection, double-stop)
requires kernel-level scaffolding that is out of scope here; the
audit-style intent should be formalised as an AI agent directive at
PR-review time instead.

Verification

  • Lint: ./scripts/run_linters.sh
  • C++ tests: cmake -DROGUE_BUILD_TESTS=ON -B build && cmake --build build && ctest --test-dir build
  • Python tests: pytest tests/

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Hardens the UDP transport layer by adding descriptor-range checks, moving worker ownership to RAII, and adding regression tests around those fixes. This fits the networking/protocols area by trying to make UDP client/server startup and shutdown safer under resource pressure.

Changes:

  • Added FD_SETSIZE validation in UDP client/server constructors and around FD_SET(...) usage.
  • Replaced raw worker-thread ownership with std::unique_ptr<std::thread> in UDP core/client/server.
  • Added doctest-based UDP regression tests and wired them into the CMake test tree.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/cpp/protocols/udp/test_udp_raw_thread_audit_repro.cpp Adds source-audit regression tests for thread ownership changes.
tests/cpp/protocols/udp/test_udp_fd_setsize_audit_repro.cpp Adds source-audit regression tests for FD_SETSIZE guards.
tests/cpp/protocols/udp/CMakeLists.txt Registers the new UDP C++ tests.
tests/cpp/protocols/CMakeLists.txt Includes the new UDP test subdirectory.
src/rogue/protocols/udp/Server.cpp Adds fd validation, RAII thread creation, and bad-fd worker handling for UDP server.
src/rogue/protocols/udp/Client.cpp Adds fd validation, RAII thread creation, and bad-fd worker handling for UDP client.
include/rogue/protocols/udp/Core.h Changes shared UDP thread ownership to std::unique_ptr<std::thread>.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/rogue/protocols/udp/Client.cpp Outdated
Comment thread src/rogue/protocols/udp/Server.cpp Outdated
@ruck314 ruck314 requested a review from Copilot May 5, 2026 16:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/cpp/protocols/udp/test_udp_fd_setsize_audit_repro.cpp Outdated
Comment thread tests/cpp/protocols/udp/test_udp_raw_thread_audit_repro.cpp Outdated
@ruck314 ruck314 changed the title fix(udp): UDP Client, Server, Core audit fixes + repros fix(udp): RAII thread ownership, FD_SETSIZE guards, and safe stop() May 5, 2026
- Replace raw new/delete std::thread with std::unique_ptr<std::thread>
  in udp::Client and udp::Server
- Add FD_SETSIZE bounds check in constructors to reject fd >= FD_SETSIZE
  at construction instead of crashing asynchronously in the worker thread
- Add defensive FD_SETSIZE guards before every FD_SET(fd_, ...) call in
  acceptFrame() and runThread(); runThread() logs and exits cleanly
  instead of calling std::terminate
- Guard stop() on thread_->joinable() to prevent std::terminate on
  double-stop or stop-before-start
- Add doctest regression tests for RAII thread and FD_SETSIZE invariants
@ruck314 ruck314 requested review from bengineerd and slacrherbst May 5, 2026 18:40
@ruck314 ruck314 marked this pull request as ready for review May 5, 2026 18:40
ruck314 added 3 commits May 6, 2026 09:31
Condense multi-line explanatory comments in Client.cpp and Server.cpp
to single-line summaries. Remove unnecessary doxygen comments on
self-documenting test helper functions.
Resolve CMakeLists.txt conflict: keep both hardware and udp subdirs.
Per review feedback, source-text audit-style C++ tests are too brittle
to be useful as CI tests. The two doctest files
(test_udp_fd_setsize_audit_repro.cpp,
test_udp_raw_thread_audit_repro.cpp) were grep checks against
Client.cpp/Server.cpp, not functional behaviour. Drop them and the
udp/ test subdirectory; the production hardening (FD_SETSIZE guards,
RAII thread ownership, safe stop) is unchanged.
@ruck314 ruck314 removed the request for review from slacrherbst May 7, 2026 19:06
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