Skip to content

Commit 60fe35c

Browse files
dahliaclaude
andcommitted
Use collision-resistant keys in LogStore
Replace the in-memory per-trace sequence counter with a timestamp + random suffix key strategy. This eliminates two problems: - Key collisions when multiple processes share the same KvStore (each process had its own counter starting at 0). - Unbounded memory growth from the #seq Map that was never cleared. #564 (comment) #564 (comment) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6d7683d commit 60fe35c

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

packages/debugger/src/mod.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ export function resetAutoSetup(): void {
9090
class LogStore {
9191
readonly #kv: KvStore;
9292
readonly #keyPrefix: KvKey;
93-
/** Per-trace monotonically increasing counter so entries sort correctly. */
94-
readonly #seq: Map<string, number> = new Map();
9593
/** Chain of pending write promises for flush(). */
9694
#pending: Promise<void> = Promise.resolve();
9795

@@ -103,14 +101,18 @@ class LogStore {
103101
/**
104102
* Enqueue a log record for writing. The write happens asynchronously;
105103
* call {@link flush} to wait for all pending writes to complete.
104+
*
105+
* Keys use a timestamp + random suffix so that entries sort
106+
* chronologically and never collide, even across multiple processes
107+
* sharing the same {@link KvStore}.
106108
*/
107109
add(traceId: string, record: SerializedLogRecord): void {
108-
const seq = this.#seq.get(traceId) ?? 0;
109-
this.#seq.set(traceId, seq + 1);
110110
const key: KvKey = [
111111
...this.#keyPrefix,
112112
traceId,
113-
seq.toString().padStart(10, "0"),
113+
`${Date.now().toString(36).padStart(10, "0")}-${
114+
Math.random().toString(36).slice(2)
115+
}`,
114116
] as unknown as KvKey;
115117
this.#pending = this.#pending.then(() => this.#kv.set(key, record));
116118
}

0 commit comments

Comments
 (0)