Skip to content

Commit 63f58d2

Browse files
authored
Merge pull request #2 from herjarsa/fix/sqlite-wal-crash-on-open-24287
fix(memory): prevent unbounded cache growth causing 92GB leak (closes anomalyco#12513)
2 parents 8af29c7 + 7f5461a commit 63f58d2

4 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎packages/opencode/src/lsp/client.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ export async function create(input: { serverID: string; server: LSPServer.Handle
682682
},
683683
async shutdown() {
684684
logger.info("shutting down")
685+
pushDiagnostics.clear()
686+
pullDiagnostics.clear()
687+
for (const key of Object.keys(files)) delete files[key]
685688
connection.end()
686689
connection.dispose()
687690
await Process.stop(input.server.process)

‎packages/opencode/src/lsp/lsp.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ export const layer = Layer.effect(
212212
yield* Effect.addFinalizer(() =>
213213
Effect.promise(async () => {
214214
await Promise.all(s.clients.map((client) => client.shutdown()))
215+
s.clients.length = 0
216+
s.broken.clear()
217+
s.spawning.clear()
215218
}),
216219
)
217220

‎packages/opencode/src/project/instance.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { makeRuntime } from "@/effect/run-service"
44
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
55
import { iife } from "@/util/iife"
66
import { Log } from "@/util"
7+
import { createLruCache } from "@/util/cache"
78
import { LocalContext } from "../util"
89
import * as Project from "./project"
910
import { WorkspaceContext } from "@/control-plane/workspace-context"
@@ -15,7 +16,9 @@ export interface InstanceContext {
1516
}
1617

1718
const context = LocalContext.create<InstanceContext>("instance")
18-
const cache = new Map<string, Promise<InstanceContext>>()
19+
const cache = createLruCache<string, Promise<InstanceContext>>({
20+
maxEntries: 20,
21+
})
1922
const project = makeRuntime(Project.Service, Project.defaultLayer)
2023

2124
const disposal = {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* LRU cache with max entries limit for preventing memory leaks
3+
*/
4+
5+
export type LruCacheOpts = {
6+
maxEntries?: number
7+
onEvict?: (key: any, value: any) => void
8+
}
9+
10+
type LruCacheEntry<V> = {
11+
value: V
12+
lastAccess: number
13+
}
14+
15+
export function createLruCache<K = any, V = any>(opts: LruCacheOpts = {}) {
16+
const { maxEntries = Infinity, onEvict } = opts
17+
const cache = new Map<K, LruCacheEntry<V>>()
18+
19+
function evictOne() {
20+
let oldestKey: K | null = null
21+
let oldestAccess = Infinity
22+
23+
for (const [key, entry] of cache) {
24+
if (entry.lastAccess < oldestAccess) {
25+
oldestAccess = entry.lastAccess
26+
oldestKey = key
27+
}
28+
}
29+
30+
if (oldestKey !== null) {
31+
delete_(oldestKey)
32+
}
33+
}
34+
35+
function delete_(key: K): boolean {
36+
const entry = cache.get(key)
37+
if (!entry) return false
38+
onEvict?.(key, entry.value)
39+
return cache.delete(key)
40+
}
41+
42+
return {
43+
get(key: K): V | undefined {
44+
const entry = cache.get(key)
45+
if (!entry) return undefined
46+
entry.lastAccess = Date.now()
47+
return entry.value
48+
},
49+
50+
set(key: K, value: V): void {
51+
if (cache.size >= maxEntries && !cache.has(key)) {
52+
evictOne()
53+
}
54+
cache.set(key, { value, lastAccess: Date.now() })
55+
},
56+
57+
has(key: K): boolean {
58+
return cache.has(key)
59+
},
60+
61+
delete(key: K): boolean {
62+
return delete_(key)
63+
},
64+
65+
clear(): void {
66+
for (const [key, entry] of cache) {
67+
onEvict?.(key, entry.value)
68+
}
69+
cache.clear()
70+
},
71+
72+
get size() {
73+
return cache.size
74+
},
75+
76+
*[Symbol.iterator](): IterableIterator<[K, V]> {
77+
for (const [key, entry] of cache) {
78+
yield [key, entry.value]
79+
}
80+
},
81+
82+
entries(): IterableIterator<[K, V]> {
83+
return this[Symbol.iterator]()
84+
},
85+
}
86+
}

0 commit comments

Comments
 (0)