8000 Coalesce concurrent status log reads to prevent resource exhaustion · Issue #287 · stratum-mining/sv2-ui · GitHub
[go: up one dir, main page]

Skip to content

Coalesce concurrent status log reads to prevent resource exhaustion #287

Closed

Description

@lucasbalieiro

tracking https://github.com/project-loupe/audit-sv2-ui/issues/18

Finding

  • repo: stratum-mining/sv2-ui (https://github.com/stratum-mining/sv2-ui.git)
  • reviewed revision: ba9d73547e751247c4aa79257b54d3d706f42620
  • title: Coalesce concurrent status log reads to prevent resource exhaustion
  • severity: medium
  • location: server/src/active-pool.ts:140-148
  • cwe: CWE-400
  • scanner: llm-code-review
  • fingerprint: 6bd5070d10eddc857fb7e6625e2ef68789048b6cb53adc00e52d1c3b6b59f917

Description

ActivePoolTracker.getActivePool() only installs its incremental cursor after readLogs() completes. When the tracker has no matching state (startup, reset, or configuration change), every concurrent call observes previous === null and invokes readLogs(container, undefined). In this repository that provider is readContainerLogs, where an undefined since/tail requests the container's complete log history and materializes it as a Buffer plus parsed line objects. GET /api/status is unauthenticated, Express listens on all interfaces by default, and each request awaits this method, so a remote client can issue many parallel status requests immediately after startup/reset and force many simultaneous full-history Docker log reads. With a sufficiently large mining-service log this can exhaust server/Docker memory and CPU and make the management UI unavailable. The regression test starts 32 concurrent initial polls behind a gate and expects one provider invocation; HEAD invokes it 32 times. A fix should single-flight/coalesce reads per configuration key (and ensure failures clear the in-flight entry). Prior searches for ActivePoolTracker concurrent getActivePool full log history resource exhaustion status polling and Docker logs denial service unbounded concurrent requests returned no matches.

Proof of Concept

diff --git a/server/src/active-pool.test.ts b/server/src/active-pool.test.ts
--- a/server/src/active-pool.test.ts
+++ b/server/src/active-pool.test.ts
@@ -168,3 +168,29 @@
     index: 1,
   });
 });
+
+test('coalesces concurrent initial polls into one full-history log read', async () => {
+  let readCount = 0;
+  let releaseRead!: () => void;
+  const readGate = new Promise<void>((resolve) => {
+    releaseRead = resolve;
+  });
+  const tracker = new ActivePoolTracker(async () => {
+    readCount += 1;
+    await readGate;
+    return [];
+  });
+
+  const requests = Array.from(
+    { length: 32 },
+    () => tracker.getActivePool('translator', POOLS)
+  );
+
+  try {
+    await new Promise<void>((resolve) => setImmediate(resolve));
+    assert.equal(readCount, 1);
+  } finally {
+    releaseRead();
+    await Promise.all(requests);
+  }
+});

Suggested Fix

diff --git a/server/src/active-pool.ts b/server/src/active-pool.ts
--- a/server/src/active-pool.ts
+++ b/server/src/active-pool.ts
@@ -122,5 +122,6 @@
 export class ActivePoolTracker {
   private state: TrackerState | null = null;
+  private readonly inFlight = new Map<string, Promise<ActivePool | null>>();
 
   constructor(private readonly readLogs: ActivePoolLogProvider) {}
 
@@ -137,7 +138,27 @@
       return null;
     }
 
     const configKey = getConfigKey(container, pools);
+    const inFlight = this.inFlight.get(configKey);
+    if (inFlight) return inFlight;
+
+    const read = this.readActivePool(container, pools, configKey);
+    this.inFlight.set(configKey, read);
+
+    try {
+      return await read;
+    } finally {
+      if (this.inFlight.get(configKey) === read) {
+        this.inFlight.delete(configKey);
+      }
+    }
+  }
+
+  private async readActivePool(
+    container: LogContainerRole,
+    pools: PoolConfig[],
+    configKey: string
+  ): Promise<ActivePool | null> {
     const previous = this.state?.configKey === configKey ? this.state : null;
     const readStartedAt = Math.floor(Date.now() / 1000);
 

This finding was discovered by Project Loupe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:logs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0