Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(gfs): release shared blob URLs safely

Miguelon Peluchon401515ca512a
diff --git a/control-ui/components/GfsFileThumbnail/index.tsx b/control-ui/components/GfsFileThumbnail/index.tsx
index a36d0c3a9..08fc091ec 100644
--- a/control-ui/components/GfsFileThumbnail/index.tsx
+++ b/control-ui/components/GfsFileThumbnail/index.tsx
@@ -4,7 +4,12 @@ import { useEffect, useState } from 'react'
import { IconImage } from '@components/Sidebar/icons'
import { GFS_FILE_THUMBNAIL_MAX_BYTES } from '@constants/gfsFileThumbnail'
import { gfsFetchFileBlob } from '@lib/api'
-import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
+import {
+ getCachedGfsBlob,
+ releaseCachedGfsBlob,
+ retainCachedGfsBlob,
+ setCachedGfsBlob,
+} from '@lib/gfsBlobCache'
import { gfsImagePreviewMimeType } from '@lib/gfsImagePreview'
import type { GfsFileThumbnailProps } from './types'
@@ -33,9 +38,9 @@ export function GfsFileThumbnail({
return
}
const cached = getCachedGfsBlob(rid)
- if (cached) {
+ if (cached && retainCachedGfsBlob(rid, cached.blobUrl)) {
setSrc(cached.blobUrl)
- return
+ return () => releaseCachedGfsBlob(rid, cached.blobUrl)
}
let cancelled = false
let objectUrl: string | null = null
@@ -56,9 +61,12 @@ export function GfsFileThumbnail({
const mimeType = gfsImagePreviewMimeType(fileName) ?? fetched.type
const wrappedBlob =
mimeType && mimeType !== fetched.type ? new Blob([fetched], { type: mimeType }) : fetched
- objectUrl = URL.createObjectURL(wrappedBlob)
- setCachedGfsBlob(rid, { blobUrl: objectUrl, mimeType: wrappedBlob.type })
- setSrc(objectUrl)
+ const cachedEntry = setCachedGfsBlob(rid, {
+ blobUrl: URL.createObjectURL(wrappedBlob),
+ mimeType: wrappedBlob.type,
+ })
+ objectUrl = cachedEntry.blobUrl
+ setSrc(cachedEntry.blobUrl)
} catch {
if (!cancelled) setFailed(true)
}
@@ -67,7 +75,7 @@ export function GfsFileThumbnail({
void load()
return () => {
cancelled = true
- if (objectUrl) URL.revokeObjectURL(objectUrl)
+ if (objectUrl) releaseCachedGfsBlob(rid, objectUrl)
}
}, [fileName, rid, shouldSkip])
diff --git a/control-ui/components/GfsImagePreview/index.tsx b/control-ui/components/GfsImagePreview/index.tsx
index 22d6bb8e0..ac95effa6 100644
--- a/control-ui/components/GfsImagePreview/index.tsx
+++ b/control-ui/components/GfsImagePreview/index.tsx
@@ -7,7 +7,12 @@ import { useToast } from '@components/Toast'
import { IconX } from '@components/icons'
import { Button } from '@components/ui'
import { gfsFetchFileBlob } from '@lib/api'
-import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
+import {
+ getCachedGfsBlob,
+ releaseCachedGfsBlob,
+ retainCachedGfsBlob,
+ setCachedGfsBlob,
+} from '@lib/gfsBlobCache'
import { assertGfsImagePreviewSize } from '@lib/gfsImagePreview'
import type { GfsImagePreviewProps } from './types'
@@ -45,14 +50,13 @@ export function GfsImagePreview({
useEffect(() => {
let active = true
let objectUrl: string | null = null
- const isFromCacheRef = { current: false }
async function loadPreview(): Promise<void> {
// If the row thumbnail already paid the proxy round-trip for this
// resource, reuse its object URL and skip the network entirely.
const cached = getCachedGfsBlob(rid)
- if (cached) {
- isFromCacheRef.current = true
+ if (cached && retainCachedGfsBlob(rid, cached.blobUrl)) {
+ objectUrl = cached.blobUrl
if (!active) return
// Reconstruct the Blob from the same bytes the thumbnail cached
// would require keeping the Blob alive; instead we copy the
@@ -79,12 +83,14 @@ export function GfsImagePreview({
if (!active) return
const blob = new Blob([fetched], { type: mimeType })
setSourceBlob(blob)
- objectUrl = URL.createObjectURL(blob)
- // Publish to the cache so the next preview open skips the
- // network. We don't revoke the URL on unmount — the next open
- // may want it.
- setCachedGfsBlob(rid, { blobUrl: objectUrl, mimeType: blob.type })
- setPreviewUrl(objectUrl)
+ const cachedEntry = setCachedGfsBlob(rid, {
+ blobUrl: URL.createObjectURL(blob),
+ mimeType: blob.type,
+ })
+ objectUrl = cachedEntry.blobUrl
+ // Publish to the cache so the next preview open skips the network.
+ // This preview holds one reference until its cleanup runs.
+ setPreviewUrl(cachedEntry.blobUrl)
} catch (error) {
if (!active) return
setPreviewError(error instanceof Error ? error.message : 'Could not load the image preview')
@@ -94,9 +100,9 @@ export function GfsImagePreview({
void loadPreview()
return () => {
active = false
- // Only revoke object URLs we created ourselves. Cache-served URLs
- // are owned by the row thumbnail component.
- if (objectUrl && !isFromCacheRef.current) URL.revokeObjectURL(objectUrl)
+ // Release this preview's reference. The cache revokes the URL only
+ // after the last thumbnail or preview consumer leaves.
+ if (objectUrl) releaseCachedGfsBlob(rid, objectUrl)
}
}, [byteLength, mimeType, rid])
diff --git a/control-ui/components/__tests__/GfsFileThumbnail.test.tsx b/control-ui/components/__tests__/GfsFileThumbnail.test.tsx
index d4832e2c9..061c49417 100644
--- a/control-ui/components/__tests__/GfsFileThumbnail.test.tsx
+++ b/control-ui/components/__tests__/GfsFileThumbnail.test.tsx
@@ -63,6 +63,27 @@ describe('GfsFileThumbnail', () => {
expect(mockCreateObjectUrl).toHaveBeenCalledTimes(1)
})
+ it('releases the cached URL when the row unmounts so a remount fetches again', async () => {
+ mockGfsFetchFileBlob.mockResolvedValue(new Blob(['fake-image-bytes']))
+ const firstRender = renderThumb({
+ byteLength: 64 * 1024,
+ fileName: 'logo.png',
+ rid: 'r-remount',
+ })
+
+ await screen.findByAltText('Thumbnail of logo.png')
+ firstRender.unmount()
+
+ renderThumb({
+ byteLength: 64 * 1024,
+ fileName: 'logo.png',
+ rid: 'r-remount',
+ })
+
+ await waitFor(() => expect(mockGfsFetchFileBlob).toHaveBeenCalledTimes(2))
+ expect(mockRevokeObjectUrl).toHaveBeenCalledWith('blob:gfs-thumbnail')
+ })
+
it('rewraps the blob with image/svg+xml when the proxy returns SVG bytes', async () => {
const svgBytes = new Blob(['<svg></svg>'], { type: 'application/octet-stream' })
mockGfsFetchFileBlob.mockResolvedValueOnce(svgBytes)
diff --git a/control-ui/lib/gfsBlobCache.ts b/control-ui/lib/gfsBlobCache.ts
index 20c9b5765..4db593be7 100644
--- a/control-ui/lib/gfsBlobCache.ts
+++ b/control-ui/lib/gfsBlobCache.ts
@@ -4,25 +4,59 @@
* this the preview modal would re-fetch the same image every time it
* opens, which dominates the "preview feels slow" complaint.
*
- * The cache is module-scoped and survives navigation between folders.
- * Entries stay valid for the lifetime of the renderer; when the
- * window unloads the underlying blob URLs are released by the browser.
+ * The cache is module-scoped and survives navigation between folders while
+ * a thumbnail or preview still references an entry. The last consumer
+ * releases the object URL; window unload also releases any remaining URLs.
*/
export type CachedGfsBlob = {
blobUrl: string
mimeType: string
}
-const blobUrlCache = new Map<string, CachedGfsBlob>()
+type CacheEntry = CachedGfsBlob & {
+ references: number
+}
+
+const blobUrlCache = new Map<string, CacheEntry>()
export function getCachedGfsBlob(rid: string): CachedGfsBlob | null {
return blobUrlCache.get(rid) ?? null
}
-export function setCachedGfsBlob(rid: string, entry: CachedGfsBlob): void {
- blobUrlCache.set(rid, entry)
+/**
+ * Store a URL and claim its first owner. If another loader won the race for
+ * this resource, retain that URL and release the newly-created one instead.
+ */
+export function setCachedGfsBlob(rid: string, entry: CachedGfsBlob): CachedGfsBlob {
+ const existing = blobUrlCache.get(rid)
+ if (existing) {
+ if (existing.blobUrl !== entry.blobUrl) URL.revokeObjectURL(entry.blobUrl)
+ existing.references += 1
+ return existing
+ }
+ const cacheEntry = { ...entry, references: 1 }
+ blobUrlCache.set(rid, cacheEntry)
+ return cacheEntry
+}
+
+export function retainCachedGfsBlob(rid: string, blobUrl: string): boolean {
+ const entry = blobUrlCache.get(rid)
+ if (!entry || entry.blobUrl !== blobUrl) return false
+ entry.references += 1
+ return true
+}
+
+export function releaseCachedGfsBlob(rid: string, blobUrl: string): void {
+ const entry = blobUrlCache.get(rid)
+ if (!entry || entry.blobUrl !== blobUrl) return
+ entry.references -= 1
+ if (entry.references > 0) return
+ blobUrlCache.delete(rid)
+ URL.revokeObjectURL(blobUrl)
}
-export function clearCachedGfsBlob(rid: string): void {
+export function clearCachedGfsBlob(rid: string, blobUrl?: string): void {
+ const entry = blobUrlCache.get(rid)
+ if (!entry || (blobUrl !== undefined && entry.blobUrl !== blobUrl)) return
blobUrlCache.delete(rid)
}
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index c6b258943..cb12f538b 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.446",
+ "version": "0.1.447",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.446",
+ "version": "0.1.447",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-ui/package.json b/control-ui/package.json
index 73e7bf984..a389c6693 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.446",
+ "version": "0.1.447",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",
diff --git a/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx b/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx
index 0109f0bc1..d2f475fce 100644
--- a/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx
+++ b/desktop-app/ui/src/components/GfsFileThumbnail/index.tsx
@@ -1,7 +1,12 @@
import { useEffect, useState } from 'react'
import { IconImage } from '@components/SidebarNav/icons'
import { GFS_FILE_THUMBNAIL_MAX_BYTES } from '@constants/gfsFileThumbnail'
-import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
+import {
+ getCachedGfsBlob,
+ releaseCachedGfsBlob,
+ retainCachedGfsBlob,
+ setCachedGfsBlob,
+} from '@lib/gfsBlobCache'
import { gfsImagePreviewMimeType } from '@lib/gfsImagePreview'
import type { GfsFileThumbnailProps } from './types'
@@ -26,9 +31,9 @@ export function GfsFileThumbnail({ byteLength, fileName, rid }: GfsFileThumbnail
return
}
const cached = getCachedGfsBlob(rid)
- if (cached) {
+ if (cached && retainCachedGfsBlob(rid, cached.blobUrl)) {
setSrc(cached.blobUrl)
- return
+ return () => releaseCachedGfsBlob(rid, cached.blobUrl)
}
let cancelled = false
let objectUrl: string | null = null
@@ -47,9 +52,12 @@ export function GfsFileThumbnail({ byteLength, fileName, rid }: GfsFileThumbnail
// <img> can actually paint the SVG.
const mimeType = gfsImagePreviewMimeType(fileName)
const blob = mimeType !== null ? new Blob([bytes], { type: mimeType }) : new Blob([bytes])
- objectUrl = URL.createObjectURL(blob)
- setCachedGfsBlob(rid, { blobUrl: objectUrl, mimeType: blob.type })
- setSrc(objectUrl)
+ const cachedEntry = setCachedGfsBlob(rid, {
+ blobUrl: URL.createObjectURL(blob),
+ mimeType: blob.type,
+ })
+ objectUrl = cachedEntry.blobUrl
+ setSrc(cachedEntry.blobUrl)
} catch {
if (!cancelled) setFailed(true)
}
@@ -58,7 +66,7 @@ export function GfsFileThumbnail({ byteLength, fileName, rid }: GfsFileThumbnail
void load()
return () => {
cancelled = true
- if (objectUrl) URL.revokeObjectURL(objectUrl)
+ if (objectUrl) releaseCachedGfsBlob(rid, objectUrl)
}
}, [fileName, rid, shouldSkip])
diff --git a/desktop-app/ui/src/components/GfsImagePreview/index.tsx b/desktop-app/ui/src/components/GfsImagePreview/index.tsx
index bfb545617..26b5ce27f 100644
--- a/desktop-app/ui/src/components/GfsImagePreview/index.tsx
+++ b/desktop-app/ui/src/components/GfsImagePreview/index.tsx
@@ -2,7 +2,12 @@ import { useEffect, useId, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { IconButton, StatusBanner } from '@components/Common'
import { IconClose, IconCopy } from '@components/SidebarNav/icons'
-import { getCachedGfsBlob, setCachedGfsBlob } from '@lib/gfsBlobCache'
+import {
+ getCachedGfsBlob,
+ releaseCachedGfsBlob,
+ retainCachedGfsBlob,
+ setCachedGfsBlob,
+} from '@lib/gfsBlobCache'
import { assertGfsImagePreviewSize } from '@lib/gfsImagePreview'
import type { GfsImagePreviewProps } from './types'
@@ -38,14 +43,13 @@ export function GfsImagePreview({
useEffect(() => {
let active = true
let objectUrl: string | null = null
- const isFromCacheRef = { current: false }
const loadPreview = async () => {
// If the row thumbnail already paid the proxy round-trip for this
// resource, reuse its object URL and skip the network entirely.
const cached = getCachedGfsBlob(cacheKey)
- if (cached) {
- isFromCacheRef.current = true
+ if (cached && retainCachedGfsBlob(cacheKey, cached.blobUrl)) {
+ objectUrl = cached.blobUrl
if (!active) return
setPreviewUrl(cached.blobUrl)
// The cached blob is reachable only through the object URL; for
@@ -67,12 +71,14 @@ export function GfsImagePreview({
if (!active) return
const blob = new Blob([bytes], { type: mimeType })
setSourceBlob(blob)
- objectUrl = URL.createObjectURL(blob)
- // Publish to the cache so the next preview open skips the
- // network. We don't revoke the URL on unmount — the next open
- // may want it.
- setCachedGfsBlob(cacheKey, { blobUrl: objectUrl, mimeType: blob.type })
- setPreviewUrl(objectUrl)
+ const cachedEntry = setCachedGfsBlob(cacheKey, {
+ blobUrl: URL.createObjectURL(blob),
+ mimeType: blob.type,
+ })
+ objectUrl = cachedEntry.blobUrl
+ // Publish to the cache so the next preview open skips the network.
+ // This preview holds one reference until its cleanup runs.
+ setPreviewUrl(cachedEntry.blobUrl)
} catch (error) {
if (!active) return
onDownloadError?.(error)
@@ -83,9 +89,9 @@ export function GfsImagePreview({
void loadPreview()
return () => {
active = false
- // Only revoke object URLs we created ourselves. Cache-served URLs
- // are owned by the row thumbnail component.
- if (objectUrl && !isFromCacheRef.current) URL.revokeObjectURL(objectUrl)
+ // Release this preview's reference. The cache revokes the URL only
+ // after the last thumbnail or preview consumer leaves.
+ if (objectUrl) releaseCachedGfsBlob(cacheKey, objectUrl)
}
}, [byteLength, cacheKey, gfsUri, mimeType, onDownloadError])
diff --git a/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx b/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx
index ddebace02..df9afda0f 100644
--- a/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx
+++ b/desktop-app/ui/src/components/__tests__/GfsFileThumbnail.test.tsx
@@ -65,6 +65,27 @@ describe('GfsFileThumbnail', () => {
expect(mockCreateObjectUrl).toHaveBeenCalledTimes(1)
})
+ it('releases the cached URL when the row unmounts so a remount fetches again', async () => {
+ mockDownload.mockResolvedValue({ bytes: new Uint8Array([1, 2, 3, 4]).buffer })
+ const firstRender = renderThumb({
+ byteLength: 64 * 1024,
+ fileName: 'logo.png',
+ rid: 'r-remount',
+ })
+
+ await screen.findByAltText('Thumbnail of logo.png')
+ firstRender.unmount()
+
+ renderThumb({
+ byteLength: 64 * 1024,
+ fileName: 'logo.png',
+ rid: 'r-remount',
+ })
+
+ await waitFor(() => expect(mockDownload).toHaveBeenCalledTimes(2))
+ expect(mockRevokeObjectUrl).toHaveBeenCalledWith('blob:gfs-thumbnail')
+ })
+
it('rewraps the blob with image/svg+xml when the download returns SVG bytes', async () => {
const svgBytes = new TextEncoder().encode('<svg></svg>').buffer
mockDownload.mockResolvedValueOnce({ bytes: svgBytes })
diff --git a/desktop-app/ui/src/lib/gfsBlobCache.ts b/desktop-app/ui/src/lib/gfsBlobCache.ts
index 20c9b5765..4db593be7 100644
--- a/desktop-app/ui/src/lib/gfsBlobCache.ts
+++ b/desktop-app/ui/src/lib/gfsBlobCache.ts
@@ -4,25 +4,59 @@
* this the preview modal would re-fetch the same image every time it
* opens, which dominates the "preview feels slow" complaint.
*
- * The cache is module-scoped and survives navigation between folders.
- * Entries stay valid for the lifetime of the renderer; when the
- * window unloads the underlying blob URLs are released by the browser.
+ * The cache is module-scoped and survives navigation between folders while
+ * a thumbnail or preview still references an entry. The last consumer
+ * releases the object URL; window unload also releases any remaining URLs.
*/
export type CachedGfsBlob = {
blobUrl: string
mimeType: string
}
-const blobUrlCache = new Map<string, CachedGfsBlob>()
+type CacheEntry = CachedGfsBlob & {
+ references: number
+}
+
+const blobUrlCache = new Map<string, CacheEntry>()
export function getCachedGfsBlob(rid: string): CachedGfsBlob | null {
return blobUrlCache.get(rid) ?? null
}
-export function setCachedGfsBlob(rid: string, entry: CachedGfsBlob): void {
- blobUrlCache.set(rid, entry)
+/**
+ * Store a URL and claim its first owner. If another loader won the race for
+ * this resource, retain that URL and release the newly-created one instead.
+ */
+export function setCachedGfsBlob(rid: string, entry: CachedGfsBlob): CachedGfsBlob {
+ const existing = blobUrlCache.get(rid)
+ if (existing) {
+ if (existing.blobUrl !== entry.blobUrl) URL.revokeObjectURL(entry.blobUrl)
+ existing.references += 1
+ return existing
+ }
+ const cacheEntry = { ...entry, references: 1 }
+ blobUrlCache.set(rid, cacheEntry)
+ return cacheEntry
+}
+
+export function retainCachedGfsBlob(rid: string, blobUrl: string): boolean {
+ const entry = blobUrlCache.get(rid)
+ if (!entry || entry.blobUrl !== blobUrl) return false
+ entry.references += 1
+ return true
+}
+
+export function releaseCachedGfsBlob(rid: string, blobUrl: string): void {
+ const entry = blobUrlCache.get(rid)
+ if (!entry || entry.blobUrl !== blobUrl) return
+ entry.references -= 1
+ if (entry.references > 0) return
+ blobUrlCache.delete(rid)
+ URL.revokeObjectURL(blobUrl)
}
-export function clearCachedGfsBlob(rid: string): void {
+export function clearCachedGfsBlob(rid: string, blobUrl?: string): void {
+ const entry = blobUrlCache.get(rid)
+ if (!entry || (blobUrl !== undefined && entry.blobUrl !== blobUrl)) return
blobUrlCache.delete(rid)
}