Skip to content

[Bug]: Test temp directories (/tmp/openclaw-*) accumulate without cleanup #34286

@habakan

Description

@habakan

Bug type

Behavior bug (incorrect output/state without crash)

Summary

Test runs create /tmp/openclaw-* temporary directories that are never cleaned up, accumulating 192+ stale directories over time.

Steps to reproduce

  1. Clone the repository and run tests: npx vitest run
  2. Check /tmp after the run:
    ls -d /tmp/openclaw-*/ | wc -l
  3. Run tests again — the count only increases

Expected behavior

Temporary directories created during tests should be cleaned up after each test suite completes.

Actual behavior

44 test files call fs.mkdtempSync / fs.mkdtemp without a corresponding fs.rm() in afterAll/afterEach. Examples:

  • src/infra/exec-approvals.test.tsmkdtempSync in helper, no cleanup
  • src/infra/dotenv.test.tsmkdtemp in withDotEnvFixture, no finally cleanup
  • src/agents/pi-auth-json.test.tsmkdtemp per test, no cleanup
  • src/gateway/hooks-mapping.test.ts — 8 mkdtempSync calls, no cleanup

The existing test helpers (withTempHome, withStateDirEnv, createTempHomeEnv) properly handle cleanup, but these 44 files bypass them and call mkdtemp directly.

OpenClaw version

main (commit 150c048)

Operating system

Ubuntu 20.04 (Linux 5.4.0-216-generic)

Install method

pnpm dev (source checkout)

Logs, screenshots, and evidence

$ ls -d /tmp/openclaw-*/ | wc -l
192

$ ls -d /tmp/openclaw-*/
/tmp/openclaw-0xxu2H/
/tmp/openclaw-41vG5I/
/tmp/openclaw-6pTB1G/
/tmp/openclaw-agent/
/tmp/openclaw-before-write-Jgr5AG/
/tmp/openclaw-depth-policy-TlWPpD/
/tmp/openclaw-dotenv-test-*/
/tmp/openclaw-exec-approvals-*/
/tmp/openclaw-gw-*/
... (192 directories total)

Impact and severity

  • Affected: All developers running tests locally; CI environments
  • Severity: Low (does not block workflows, but wastes disk space)
  • Frequency: Every test run adds more stale directories
  • Consequence: Gradual disk space accumulation; stale test artifacts in /tmp

Additional information

Proposed fix

Add trackedMkdtempSync / trackedMkdtemp utility functions to test/test-env.ts that wrap mkdtemp, track created directories, and clean them up via the existing installTestEnv().cleanup().

const trackedTempDirs: string[] = [];

export function trackedMkdtempSync(prefix: string): string {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
  trackedTempDirs.push(dir);
  return dir;
}

export function cleanupTrackedTempDirs(): void {
  for (const dir of trackedTempDirs) {
    try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
  }
  trackedTempDirs.length = 0;
}

Why not monkey-patch fs.mkdtempSync? ~13 files destructure the import (import { mkdtempSync } from "node:fs"), capturing a direct reference. An explicit utility function is more reliable.

Migration plan:

  1. First PR: Add utilities + migrate src/infra/ tests (6 files)
  2. Follow-up PRs: Migrate remaining files by area

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions