1
0
mirror of https://github.com/xmrig/xmrig.git synced 2026-04-18 13:22:43 -04:00

feat: stability improvements, see detail below

Key stability improvements made (deterministic + bounded)
1) Bounded memory usage in long-running stats
Fixed unbounded growth in NetworkState latency tracking:
Replaced std::vector<uint16_t> m_latency + push_back() with a fixed-size ring buffer (kLatencyWindow = 1024) and explicit counters.
Median latency computation now operates on at most 1024 samples, preventing memory growth and avoiding performance cliffs from ever-growing copies/sorts.
2) Prevent crash/UAF on shutdown + more predictable teardown
Controller shutdown ordering (Controller::stop()):
Now stops m_miner before destroying m_network.
This reduces chances of worker threads submitting results into a network listener that’s already destroyed.
Thread teardown hardening (backend/common/Thread.h):
Destructor now checks std::thread::joinable() before join().
Avoids std::terminate() if a thread object exists but never started due to early exit/error paths.
3) Fixed real leaks (including executable memory)
Executable memory leak fixed (crypto/cn/CnCtx.cpp):
CnCtx::create() allocates executable memory for generated_code via VirtualMemory::allocateExecutableMemory(0x4000, ...).
Previously CnCtx::release() only _mm_free()’d the struct, leaking the executable mapping.
Now CnCtx::release() frees generated_code before freeing the ctx.
GPU verification leak fixed (net/JobResults.cpp):
In getResults() (GPU result verification), a cryptonight_ctx was created via CnCtx::create() but never released.
Added CnCtx::release(ctx, 1).
4) JobResults: bounded queues + backpressure + safe shutdown semantics
The old JobResults could:

enqueue unlimited std::list items (m_results, m_bundles) → unbounded RAM,
call uv_queue_work per async batch → unbounded libuv threadpool backlog,
delete handler directly while worker threads might still submit → potential crash/UAF.
Changes made:

Hard queue limits:
kMaxQueuedResults = 4096
kMaxQueuedBundles = 256
Excess is dropped (bounded behavior under load).
Async coalescing:
Only one pending async notification at a time (m_pendingAsync), reducing eventfd/uv wake storms.
Bounded libuv work scheduling:
Only one uv_queue_work is scheduled at a time (m_workScheduled), preventing CPU starvation and unpredictable backlog.
Safe shutdown:
JobResults::stop() now detaches global handler first, then calls handler->stop().
Shutdown detaches m_listener, clears queues, and defers deletion until in-flight work is done.
Defensive bound on GPU result count:
Clamp count to 0xFF inside JobResults as well, not just in the caller, to guard against corrupted kernels/drivers.
5) Idempotent cleanup
VirtualMemory::destroy() now sets pool = nullptr after delete:
prevents accidental double-delete on repeated teardown paths.
Verification performed
codespell . --config ./.codespellrc: clean
CMake configure + build completed successfully (Release build)

Signed-off-by: rezky_nightky <with.rezky@gmail.com>
This commit is contained in:
rezky_nightky
2026-01-21 21:22:43 +07:00
parent cb7511507f
commit 5ca4828255
7 changed files with 199 additions and 31 deletions

View File

@@ -210,7 +210,7 @@ void xmrig::NetworkState::printResults() const
printHashes(m_accepted, m_hashes);
printDiff(m_diff);
if (m_active && !m_latency.empty()) {
if (m_active && m_latencyCount > 0) {
printAvgTime(avgTime());
}
@@ -298,13 +298,19 @@ void xmrig::NetworkState::onResultAccepted(IStrategy *strategy, IClient *client,
uint32_t xmrig::NetworkState::latency() const
{
const size_t calls = m_latency.size();
const size_t calls = m_latencyCount;
if (calls == 0) {
return 0;
}
auto v = m_latency;
std::nth_element(v.begin(), v.begin() + calls / 2, v.end());
std::array<uint16_t, kLatencyWindow> v;
const size_t start = (m_latencyPos + kLatencyWindow - calls) % kLatencyWindow;
for (size_t i = 0; i < calls; ++i) {
v[i] = m_latency[(start + i) % kLatencyWindow];
}
std::nth_element(v.begin(), v.begin() + calls / 2, v.begin() + calls);
return v[calls / 2];
}
@@ -312,11 +318,11 @@ uint32_t xmrig::NetworkState::latency() const
uint64_t xmrig::NetworkState::avgTime() const
{
if (m_latency.empty()) {
if (m_latencyCount == 0) {
return 0;
}
return connectionTime() / m_latency.size();
return connectionTime() / m_latencyCount;
}
@@ -342,7 +348,12 @@ void xmrig::NetworkState::add(const SubmitResult &result, const char *error)
std::sort(m_topDiff.rbegin(), m_topDiff.rend());
}
m_latency.push_back(result.elapsed > 0xFFFF ? 0xFFFF : static_cast<uint16_t>(result.elapsed));
m_latency[m_latencyPos] = result.elapsed > 0xFFFF ? 0xFFFF : static_cast<uint16_t>(result.elapsed);
m_latencyPos = (m_latencyPos + 1) % kLatencyWindow;
if (m_latencyCount < kLatencyWindow) {
m_latencyCount++;
}
}
@@ -355,5 +366,6 @@ void xmrig::NetworkState::stop()
m_fingerprint = nullptr;
m_failures++;
m_latency.clear();
m_latencyCount = 0;
m_latencyPos = 0;
}

View File

@@ -27,7 +27,6 @@
#include <array>
#include <string>
#include <vector>
namespace xmrig {
@@ -60,6 +59,8 @@ protected:
void onResultAccepted(IStrategy *strategy, IClient *client, const SubmitResult &result, const char *error) override;
private:
constexpr static size_t kLatencyWindow = 1024;
uint32_t latency() const;
uint64_t avgTime() const;
uint64_t connectionTime() const;
@@ -70,7 +71,9 @@ private:
bool m_active = false;
char m_pool[256]{};
std::array<uint64_t, 10> m_topDiff { { } };
std::vector<uint16_t> m_latency;
std::array<uint16_t, kLatencyWindow> m_latency { { } };
size_t m_latencyCount = 0;
size_t m_latencyPos = 0;
String m_fingerprint;
String m_ip;
String m_tls;