8000 logs: a tracker reset is never undone by a poll that started before it · Issue #345 · stratum-mining/sv2-ui · GitHub
[go: up one dir, main page]

Skip to content

logs: a tracker reset is never undone by a poll that started before it #345

Closed

Description

@lucasbalieiro

Affected layers

  • Backend (server/): does not reproduce on main. ActivePoolTracker in server/src/active-pool.ts bumps a generation counter in reset(), drops the in-flight reads, and a read that started under an older generation returns no pool and writes no state, on both the success and the failure path. This landed with adjustments on container logs #288 on 2026-10-09, after the reviewed revision. The test reset drops the in-flight read so the next poll starts its own covers a full-history read in flight at reset; the variant Loupe describes, an incremental read started after a committed state, passes on main but is not in the test file. The export also says the status route is unauthenticated; on main GET /api/status requires a session, so an outside client cannot drive the ordering, though ordinary UI polling still could.
  • Frontend (src/): not affected. It shows the pool the status response names.
  • shared/: not affected. It holds no pool detection code.

Problem

As reported, reset() only cleared the state. A poll that had captured the previous state before the reset could finish after it and write a state derived from the previous run, so the next poll with the same configuration inherited the old active index. The tracker is reset when the status route observes the stack down, and status polls overlap with stop and restart operations, so the ordering occurs during an ordinary restart.

Impact

As reported, status and the dashboard would keep naming the previous pool and its authority key as current before the replacement container had completed any handshake. The mining connection itself was not affected. Low per Loupe.

Expected outcome

These hold on main and are kept here so a refactor of the tracker is reviewed against them:

  • A read started before a reset never publishes a pool or a state after it, whether it succeeds or fails.
  • After a reset, the next poll reads the full history and reports a pool only from evidence in that read.
  • The incremental-read variant gets a regression test: a committed state, an in-flight incremental read, a reset, then the stale read completes and the next poll still reports no pool.

Possible approach (non-binding)

The missing test can follow the proof of concept, next to reset drops the in-flight read so the next poll starts its own in server/src/active-pool.test.ts. No code change is expected.

Loupe findings

Related issues and PRs

Loupe export for #345 (finding #41)

Finding

  • repo: stratum-mining/sv2-ui (https://github.com/stratum-mining/sv2-ui.git)
  • reviewed revision: b610455ac2d1e017c1df5db8787c0b2e505eabdc
  • title: Prevent stale polls from resurrecting reset pool state
  • severity: low
  • location: server/src/active-pool.ts:133-168
  • cwe: CWE-362
  • scanner: llm-code-review
  • fingerprint: 2988cac1801f7bf5c3565b99838faa6e655059299bf04be7bd73e47727e75ab2

Description

reset() only assigns state = null; it does not invalidate getActivePool() calls that already captured previous. If such a call finishes after the reset, lines 160-168 unconditionally write a new state derived from that pre-reset snapshot. A subsequent poll with the same configuration then inherits the old activeIndex, allowing a connection authenticated by a destroyed/restarted container to be reported as current before the replacement has completed any handshake. In the application, the tracker is reset when /api/status observes the stack stopped, while status requests and stop/restart operations may overlap. Because these routes are unauthenticated and status polling is concurrent, a remote client can deliberately create this ordering around a restart; even ordinary UI polling can trigger it. The result does not change the mining connection, but it can make the API/UI continue displaying the former trusted pool and authority identity while the new connection is absent or failing validation. Use a generation/epoch incremented by reset() and only commit an async read if its captured generation is still current (or cancel/await in-flight reads). Prior #8050 was reviewed but covers duplicate full-history reads/resource exhaustion, not post-reset state resurrection.

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
@@ -211,3 +211,35 @@ test('invalidates the active pool when the authority key rotates without a new h
   // Then the authority key rotates (same endpoint) without a new handshake.
   assert.equal(await tracker.getActivePool('translator', poolsWithKey('rotated-key')), null);
 });
+
+test('reset is not undone by an older in-flight poll', async () => {
+  let readCount = 0;
+  let releaseRead!: () => void;
+  const readGate = new Promise<void>((resolve) => {
+    releaseRead = resolve;
+  });
+  const tracker = new ActivePoolTracker(async () => {
+    readCount += 1;
+    if (readCount === 1) {
+      return [
+        log('Trying upstream 1 of 2: primary.example.com:3333'),
+        log('Connected to upstream at 192.0.2.10:3333'),
+        log('Received: SetupConnectionSuccess(used_version: 2, flags: 0x00000000)'),
+      ];
+    }
+    if (readCount === 2) {
+      await readGate;
+    }
+    return [];
+  });
+
+  await tracker.getActivePool('translator', POOLS);
+  const stalePoll = tracker.getActivePool('translator', POOLS);
+  await new Promise<void>((resolve) => setImmediate(resolve));
+
+  tracker.reset();
+  releaseRead();
+  await stalePoll;
+
+  assert.equal(await tracker.getActivePool('translator', POOLS), null);
+});

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
@@ -127,10 +127,12 @@ function getConfigKey(container: LogContainerRole, pools: PoolConfig[]): string
  */
 export class ActivePoolTracker {
   private state: TrackerState | null = null;
+  private generation = 0;
 
   constructor(private readonly readLogs: ActivePoolLogProvider) {}
 
   reset(): void {
+    this.generation += 1;
     this.state = null;
   }
 
@@ -146,12 +148,15 @@ export class ActivePoolTracker {
     const configKey = getConfigKey(container, pools);
     const previous = this.state?.configKey === configKey ? this.state : null;
     const readStartedAt = Math.floor(Date.now() / 1000);
+    const generation = this.generation;
 
     try {
       const lines = await this.readLogs(
         container,
         previous ? { since: previous.since } : undefined
       );
+      if (generation !== this.generation) return null;
+
       const detected = detectActivePool(pools, lines, previous ?? undefined);
       const activeIndex = detected.activeIndex !== null && pools[detected.activeIndex]
         ? detected.activeIndex
@@ -171,6 +176,8 @@ export class ActivePoolTracker {
         ? null
         : { name: pools[activeIndex].name, index: activeIndex };
     } catch {
+      if (generation !== this.generation) return null;
+
       const activeIndex = previous?.activeIndex !== null &&
         previous?.activeIndex !== undefined &&
         pools[previous.activeIndex]

This finding was discovered by Project Loupe.

Activity

  1. added
    area:logs
    and removed on Oct 14, 2026
  2. changed the title [-]Prevent stale polls from resurrecting reset pool state[/-] [+]logs: a tracker reset is never undone by a poll that started before it[/+] on Oct 14, 2026
  3. GitGab19 commented on Oct 14, 2026

    @GitGab19
    Member

    This looks fixed by #288. The tracker now bumps a generation counter on reset, and a read that started under an older generation returns null on both the success and the failure path, so a pre-reset poll can no longer republish the old pool. reset drops the in-flight read so the next poll starts its own in active-pool.test.ts covers it. @lucasbalieiro can you confirm, so we close this as completed? The incremental-read variant of that test would be a nice extra but not a blocker.

  4. lucasbalieiro commented on Oct 18, 2026

    @lucasbalieiro
    CollaboratorAuthor

    @lucasbalieiro can you confirm, so we close this as completed?

    Not exactly bcause the issue's Expected Outcome section explicitly states that adding a regression test for the incremental-read variant is a MUST, so we cannot close it based on what we currently have.

    That said, I personally feel that these Loupe issues are mostly nitpicks about details that probably don't deserve this level of scrutiny. My preference would be to close this as a low-priority issue.

    However, being fair with the AI,adding this test coverage doesn't seem like a significant effort, so we could implement it before closing the issue and satisfy the AI desire.

  5. lucasbalieiro commented on Oct 18, 2026

    @lucasbalieiro
    CollaboratorAuthor

    added the missing coverage in #387

  6. added a commit that references this issue on Oct 20, 2026
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