Opencode crashes attempting to read too big file #7275

Open
opened 2026-02-16 18:06:40 -05:00 by yindo · 3 comments
Owner

Originally created by @alkar1 on GitHub (Jan 23, 2026).

Originally assigned to: @thdxr on GitHub.

Description

Looking at the code in packages/opencode/src/tool/read.ts (lines 94-108):

The key issue here is:

  1. Line 94: const lines = await file.text().then((text) => text.split("\n"))
    This will load the ENTIRE file into memory first, then split it by \n. If you have a huge file with no line breaks:
  • The entire file will be read into memory as a single string
  • split("\n") will create an array with just 1 element (the entire file content)
  • This means memory usage equals the size of the huge file
  1. Line 99-100: There's a MAX_LINE_LENGTH of 2000 characters, but this only truncates individual lines after they're already in memory.
  2. Line 102-104: There's a MAX_BYTES limit of 50KB (line 14: const MAX_BYTES = 50 * 1024), but this check happens AFTER the file is already fully loaded into memory.

I see significant memory and performance risks with this approach. The current implementation will struggle with large, non-delimited files, potentially causing system instability or excessive memory consumption. The code lacks efficient streaming or chunked reading mechanisms that could handle such edge cases gracefully.
The file's entire contents get loaded before any processing occurs, which defeats the purpose of the byte limit check. This means for a massive 1GB file, the entire gigabyte will occupy memory, only to have most of it immediately discarded. This approach is fundamentally flawed and inefficient for handling large files.
The file index.ts reveals similar issues in its status() function, which reads entire file contents without considering potential memory overhead.

Plugins

No response

OpenCode version

No response

Steps to reproduce

  1. Create a file bigger then available RAM
  2. Ask opencode to read first line

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Originally created by @alkar1 on GitHub (Jan 23, 2026). Originally assigned to: @thdxr on GitHub. ### Description Looking at the code in packages/opencode/src/tool/read.ts (lines 94-108): The key issue here is: 1. Line 94: const lines = await file.text().then((text) => text.split("\n")) This will load the ENTIRE file into memory first, then split it by \n. If you have a huge file with no line breaks: - The entire file will be read into memory as a single string - split("\n") will create an array with just 1 element (the entire file content) - This means memory usage equals the size of the huge file 2. Line 99-100: There's a MAX_LINE_LENGTH of 2000 characters, but this only truncates individual lines after they're already in memory. 3. Line 102-104: There's a MAX_BYTES limit of 50KB (line 14: const MAX_BYTES = 50 * 1024), but this check happens AFTER the file is already fully loaded into memory. I see significant memory and performance risks with this approach. The current implementation will struggle with large, non-delimited files, potentially causing system instability or excessive memory consumption. The code lacks efficient streaming or chunked reading mechanisms that could handle such edge cases gracefully. The file's entire contents get loaded before any processing occurs, which defeats the purpose of the byte limit check. This means for a massive 1GB file, the entire gigabyte will occupy memory, only to have most of it immediately discarded. This approach is fundamentally flawed and inefficient for handling large files. The file index.ts reveals similar issues in its status() function, which reads entire file contents without considering potential memory overhead. ### Plugins _No response_ ### OpenCode version _No response_ ### Steps to reproduce 1. Create a file bigger then available RAM 2. Ask opencode to read first line ### Screenshot and/or share link _No response_ ### Operating System _No response_ ### Terminal _No response_
yindo added the bugperf labels 2026-02-16 18:06:40 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 23, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #8956: Request Entity Too Large not caught before trying to read large files
  • #2585: Massive memory use on opening opencode when there is a very large file
  • #9699: Memory usage grows very quickly when running commands with large output
  • #2003: OpenCode doesn't perform as well as Claude Code when processing large files

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 23, 2026): This issue might be a duplicate of existing issues. Please check: - #8956: Request Entity Too Large not caught before trying to read large files - #2585: Massive memory use on opening opencode when there is a very large file - #9699: Memory usage grows very quickly when running commands with large output - #2003: OpenCode doesn't perform as well as Claude Code when processing large files Feel free to ignore if none of these address your specific case.
Author
Owner

@alkar1 commented on GitHub (Jan 23, 2026):

For quick workaround create file .opencode/plugin/safety.ts with following code:

import { type Hooks, type PluginInput } from "./index"

const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10MB

export default function SafetyPlugin(input: PluginInput): Hooks {
  return {
    "tool.execute.before": ({ tool }, { args }) => {
      if (tool !== "read") return

      const { filePath } = args as { filePath: string }

      // Skip non-existent files - delegate to read tool's error handling
      const size = Bun.file(filePath).size
      if (size === 0) return

      if (size > MAX_FILE_SIZE) {
        const sizeMB = (size / 1024 / 1024).toFixed(2)
        throw new Error(`File too large: ${filePath} (${sizeMB}MB exceeds ${MAX_FILE_SIZE / 1024 / 1024}MB limit)`)
      }
    },
  }
}
@alkar1 commented on GitHub (Jan 23, 2026): For quick workaround create file `.opencode/plugin/safety.ts` with following code: ``` import { type Hooks, type PluginInput } from "./index" const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10MB export default function SafetyPlugin(input: PluginInput): Hooks { return { "tool.execute.before": ({ tool }, { args }) => { if (tool !== "read") return const { filePath } = args as { filePath: string } // Skip non-existent files - delegate to read tool's error handling const size = Bun.file(filePath).size if (size === 0) return if (size > MAX_FILE_SIZE) { const sizeMB = (size / 1024 / 1024).toFixed(2) throw new Error(`File too large: ${filePath} (${sizeMB}MB exceeds ${MAX_FILE_SIZE / 1024 / 1024}MB limit)`) } }, } } ```
Author
Owner

@skyler14 commented on GitHub (Jan 23, 2026):

For quick workaround create file .opencode/plugin/safety.ts with following code:

import { type Hooks, type PluginInput } from "./index"

const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10MB

export default function SafetyPlugin(input: PluginInput): Hooks {
  return {
    "tool.execute.before": ({ tool }, { args }) => {
      if (tool !== "read") return

      const { filePath } = args as { filePath: string }

      // Skip non-existent files - delegate to read tool's error handling
      const size = Bun.file(filePath).size
      if (size === 0) return

      if (size > MAX_FILE_SIZE) {
        const sizeMB = (size / 1024 / 1024).toFixed(2)
        throw new Error(`File too large: ${filePath} (${sizeMB}MB exceeds ${MAX_FILE_SIZE / 1024 / 1024}MB limit)`)
      }
    },
  }
}

installing this in ~/.opencode or somewhere else? Tried on mac m1 in that dir and no luck

@skyler14 commented on GitHub (Jan 23, 2026): > For quick workaround create file `.opencode/plugin/safety.ts` with following code: > > ``` > import { type Hooks, type PluginInput } from "./index" > > const MAX_FILE_SIZE = 10 * 1024 * 1024 // 10MB > > export default function SafetyPlugin(input: PluginInput): Hooks { > return { > "tool.execute.before": ({ tool }, { args }) => { > if (tool !== "read") return > > const { filePath } = args as { filePath: string } > > // Skip non-existent files - delegate to read tool's error handling > const size = Bun.file(filePath).size > if (size === 0) return > > if (size > MAX_FILE_SIZE) { > const sizeMB = (size / 1024 / 1024).toFixed(2) > throw new Error(`File too large: ${filePath} (${sizeMB}MB exceeds ${MAX_FILE_SIZE / 1024 / 1024}MB limit)`) > } > }, > } > } > ``` installing this in ~/.opencode or somewhere else? Tried on mac m1 in that dir and no luck
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#7275