openclaw - 💡(How to fix) Fix Bug: Non-atomic write of exec-approvals file + unbounded transcript readFileSync [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#54296Fetched 2026-04-08 01:29:25
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0

Two resource management issues that could impact reliability:

Root Cause

Two resource management issues that could impact reliability:

Code Example

fs.writeFileSync(filePath, `${JSON.stringify(file, null, 2)}\n`, { mode: 384 });

---

const lines = fs.readFileSync(transcriptPath, "utf-8").split(/\r?\n/);
RAW_BUFFERClick to expand / collapse

Summary

Two resource management issues that could impact reliability:

1. Exec-Approvals Non-Atomic Write (dist/exec-approvals-BF_Qfdq8.js:651)

The exec-approvals file (controls command execution permissions, contains socket auth tokens) is written with bare writeFileSync without atomic temp+rename:

fs.writeFileSync(filePath, `${JSON.stringify(file, null, 2)}\n`, { mode: 384 });

A crash mid-write produces a corrupt file, potentially disabling security controls.

2. Unbounded Transcript Read (dist/gateway-cli-Dsd9gHBa.js:12283, 31883)

Entire transcript files are read into memory synchronously:

const lines = fs.readFileSync(transcriptPath, "utf-8").split(/\r?\n/);

Transcripts grow unboundedly over long sessions. Used for idempotency checks (line 12283) and compaction (line 31883).

Impact

  1. Corrupted exec-approvals → security controls disabled or permissive defaults
  2. Large transcripts → significant memory pressure or OOM, denial of service

Suggested Fix

  1. Exec-approvals: Atomic write (temp file + rename())
  2. Transcripts: Use streaming reads (readline) instead of readFileSync. Add size limit check.

extent analysis

Fix Plan

To address the two resource management issues, we will implement the following fixes:

1. Atomic Write for Exec-Approvals

  • Use a temporary file to write the data atomically
  • Rename the temporary file to the original file path

Example code:

const fs = require('fs');
const os = require('os');
const path = require('path');

// ...

const tempFilePath = `${filePath}.tmp`;
fs.writeFileSync(tempFilePath, `${JSON.stringify(file, null, 2)}\n`, { mode: 384 });
fs.renameSync(tempFilePath, filePath);

2. Streaming Reads for Transcripts

  • Use the readline module to stream the transcript file
  • Add a size limit check to prevent excessive memory usage

Example code:

const fs = require('fs');
const readline = require('readline');

// ...

const transcriptPath = 'path/to/transcript';
const sizeLimit = 10 * 1024 * 1024; // 10MB
const fileSize = fs.statSync(transcriptPath).size;

if (fileSize > sizeLimit) {
  throw new Error(`Transcript file exceeds size limit: ${fileSize} > ${sizeLimit}`);
}

const fileStream = fs.createReadStream(transcriptPath, { encoding: 'utf-8' });
const rl = readline.createInterface({
  input: fileStream,
  crlfDelay: Infinity
});

const lines = [];
rl.on('line', (line) => {
  lines.push(line);
});

rl.on('close', () => {
  // Process the lines
});

Verification

To verify the fixes, you can:

  • Test the atomic write by simulating a crash during the write process and checking that the file is not corrupted
  • Test the streaming reads by processing a large transcript file and checking that the memory usage does not exceed the expected limit

Extra Tips

  • Consider using a more robust temporary file naming strategy to avoid conflicts
  • Monitor the size of the transcript files and adjust the size limit as needed to prevent excessive memory usage.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING