[MG_Remote, MG_Test, scripts] (P7): bound the pre-auth cap and pause accepting when out of descriptors

This commit is contained in:
2026-09-23 13:45:00 -04:00
parent bc62b17b0c
commit efc899a1b0
6 changed files with 115 additions and 11 deletions
+10 -3
View File
@@ -89,6 +89,11 @@ namespace MobileGL::MG_Remote::Server {
// port probe; one that held its slot longer is a failure. A real client writes its first frame
// the moment it connects, so it is never on the wrong side of this.
inline constexpr std::uint32_t kPreAuthProbeGraceMs = 250;
// MOBILEGL_IPC_PREAUTH_MAX never exceeds this. Every pending connection is a descriptor the
// supervisor holds, and an unbounded knob would let a connection flood walk it into
// RLIMIT_NOFILE (the supervisor also survives that now - it pauses accepting - but a knob
// should not be the way in).
inline constexpr std::uint32_t kPreAuthMaxPendingCeiling = 256;
// The refusal details a peer (and the log) sees. Named once so the supervisor and the unit test
// cannot drift on the words; the TCP lane's scripts mirror them rather than parse them, so a
@@ -108,8 +113,9 @@ namespace MobileGL::MG_Remote::Server {
// MOBILEGL_IPC_PREAUTH_MS: how long a new connection has to present a complete first
// frame. A real client writes its Hello (or DataBind) the moment it connects.
std::uint32_t deadlineMs = 2000;
// MOBILEGL_IPC_PREAUTH_MAX: connections the supervisor holds unauthenticated at once; one
// more is refused Busy at accept.
// MOBILEGL_IPC_PREAUTH_MAX: connections the supervisor holds unauthenticated at once, at
// most kPreAuthMaxPendingCeiling. When they are all taken a newcomer displaces the busiest
// address's oldest one, or is refused Busy at accept (ChoosePendingToDisplace).
std::uint32_t maxPending = 8;
// MOBILEGL_IPC_AUTH_BACKOFF_AFTER: failures from one address before it is refused at
// accept. 0 turns the backoff off (the fuzz arm does, to send many malformed frames from
@@ -124,7 +130,8 @@ namespace MobileGL::MG_Remote::Server {
static PreAuthKnobs FromEnvironment() {
PreAuthKnobs knobs;
knobs.deadlineMs = Read("MOBILEGL_IPC_PREAUTH_MS", knobs.deadlineMs, false);
knobs.maxPending = Read("MOBILEGL_IPC_PREAUTH_MAX", knobs.maxPending, false);
knobs.maxPending =
std::min(Read("MOBILEGL_IPC_PREAUTH_MAX", knobs.maxPending, false), kPreAuthMaxPendingCeiling);
knobs.backoffAfter = Read("MOBILEGL_IPC_AUTH_BACKOFF_AFTER", knobs.backoffAfter, true);
knobs.backoffBaseMs = Read("MOBILEGL_IPC_AUTH_BACKOFF_MS", knobs.backoffBaseMs, false);
return knobs;
+25 -3
View File
@@ -714,16 +714,23 @@ public:
std::vector<pollfd> fds;
for (;;) {
Reap();
fds.clear();
fds.push_back({m_listener, POLLIN, 0});
for (const auto& peer : m_pending) fds.push_back({peer.fd, POLLIN, 0});
auto now = Clock::now();
// While accepting is paused (out of descriptors, AcceptNew) the listener sits out the
// poll - it stays readable, and would spin the loop - and the pause's end is a wakeup.
const bool acceptPaused = now < m_acceptResumeAt;
fds.clear();
fds.push_back({acceptPaused ? -1 : m_listener, POLLIN, 0});
for (const auto& peer : m_pending) fds.push_back({peer.fd, POLLIN, 0});
// 250 ms is the old accept timeout, i.e. how often a finished child is reaped while
// nothing arrives; a pending deadline that falls sooner shortens it.
std::int64_t timeout = 250;
for (const auto& peer : m_pending)
timeout = std::clamp<std::int64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(peer.deadline - now).count(), 0, timeout);
if (acceptPaused)
timeout = std::clamp<std::int64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(m_acceptResumeAt - now).count() + 1, 0,
timeout);
const int polled = ::poll(fds.data(), static_cast<nfds_t>(fds.size()), static_cast<int>(timeout));
if (polled < 0 && errno != EINTR) {
WireLogError("MG_Remote server: supervisor poll failed: %s", std::strerror(errno));
@@ -823,6 +830,18 @@ private:
int fd = -1;
const auto accepted = SocketTransport::AcceptOne(m_listener, 0, &fd);
if (accepted == MOBILEGL_ERR_TIMEOUT) return 0;
if (accepted == MOBILEGL_ERR_OUT_OF_MEMORY) {
// f2-auth fix round. Out of descriptors (or memory) is not a dead listener: the
// connection waits in the backlog, and pending connections give descriptors back
// as they are judged or reach their deadline. The first cut returned 73 here, so
// a connection flood against a low RLIMIT_NOFILE (or a large pending cap) took the
// supervisor down. Now accepting pauses for kAcceptPauseMs and the loop goes on.
m_acceptResumeAt = now + std::chrono::milliseconds(kAcceptPauseMs);
WireLogError("MG_Remote server: accept paused for %u ms: out of descriptors or memory with %zu "
"connections awaiting authentication",
kAcceptPauseMs, m_pending.size());
return 0;
}
if (accepted != MOBILEGL_OK) return 73;
std::string address = PeerAddress(fd);
if (const std::uint32_t retry = m_backoff.RetryAfterMs(address, now); retry != 0) {
@@ -979,6 +998,9 @@ private:
int m_handoff = -1;
std::vector<PendingPeer> m_pending;
std::vector<PendingPeer> m_ready;
// Accepting resumes at this time after accept(2) ran out of descriptors or memory.
static constexpr std::uint32_t kAcceptPauseMs = 100;
Clock::time_point m_acceptResumeAt{};
};
}
@@ -486,7 +486,13 @@ namespace MobileGL::MG_Remote::Transport {
if (fd < 0) {
// The same two non-failures AcceptPair re-polls on.
if (errno == EINTR || errno == ECONNABORTED) continue;
WireLogError("MG_Remote SocketTransport: accept failed: %s", std::strerror(errno));
const int error = errno;
WireLogError("MG_Remote SocketTransport: accept failed: %s", std::strerror(error));
// Out of descriptors or memory is the PROCESS's state, not the listener's: the
// connection waits in the backlog (f2-auth fix round; the TCP supervisor pauses
// and retries instead of exiting 73).
if (error == EMFILE || error == ENFILE || error == ENOBUFS || error == ENOMEM)
return MOBILEGL_ERR_OUT_OF_MEMORY;
return MOBILEGL_ERR_TRANSPORT_CLOSED;
}
::fcntl(fd, F_SETFD, FD_CLOEXEC);
@@ -123,6 +123,10 @@ namespace MobileGL::MG_Remote::Transport {
static MobileGLResult ConnectDataConnection(const std::string& path, std::uint32_t timeoutMs,
MobileGLByteSpan firstFrame, int* outFd);
// Accepts ONE connection (TCP options applied, CLOEXEC). MOBILEGL_ERR_TIMEOUT when none.
// MOBILEGL_ERR_OUT_OF_MEMORY when accept(2) ran out of descriptors or memory (EMFILE,
// ENFILE, ENOBUFS, ENOMEM): the connection stays in the backlog and the listener is fine,
// so a caller that serves many peers can wait and try again (the TCP supervisor does);
// TRANSPORT_CLOSED for every other failure.
static MobileGLResult AcceptOne(int listenFd, std::uint32_t timeoutMs, int* outFd);
// Reads EXACTLY one framed message from a raw socket - the header, then the payload,
// and not one byte more. A DataBind is followed on the same connection by StreamLink's
+10
View File
@@ -303,4 +303,14 @@ TEST(PreAuthGateTest, TheKnobsReadTheirEnvironmentAndKeepTheirDefaultsOtherwise)
EXPECT_EQ(knobs.backoffAfter, 5u);
EXPECT_EQ(knobs.backoffBaseMs, 1000u);
}
// f2-auth fix round: the pending cap is bounded, whatever it is set to - every pending
// connection is a descriptor. Red once by dropping the clamp: 100000 comes back.
{
ScopedEnvironment b("MOBILEGL_IPC_PREAUTH_MAX", "100000");
EXPECT_EQ(Server::PreAuthKnobs::FromEnvironment().maxPending, Server::kPreAuthMaxPendingCeiling);
}
{
ScopedEnvironment b("MOBILEGL_IPC_PREAUTH_MAX", "256");
EXPECT_EQ(Server::PreAuthKnobs::FromEnvironment().maxPending, 256u);
}
}