Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(gallery): cancel stale image fetches before Blob allocation (#4588)
Jarvis3fc6f5743b48
diff --git a/frontend/src/views/knowledge/gallery/ImageGallery.test.ts b/frontend/src/views/knowledge/gallery/ImageGallery.test.tsindex b879ae455..cb092c5f0 100644--- a/frontend/src/views/knowledge/gallery/ImageGallery.test.ts+++ b/frontend/src/views/knowledge/gallery/ImageGallery.test.ts@@ -13,16 +13,25 @@ const source = readFileSync(new URL('./ImageGallery.vue', import.meta.url), 'utfconst compiled = ts.transpileModule(`${source}\nexport {items, page, total, loading, error, keyword, hasMore, viewerIndex, viewerOpen,reload, loadMore, openViewer, onViewerLoadMore, initGallery,+ resolveImageSrc, thumbUrls,}`, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText-function setup() {+function setup(proxyImages = false) {const calls: Array<{kb: string; params: ImageListParamsresolve: (value: ImageListResult) => void; reject: (reason: Error) => void}> = []+ const imageCalls: Array<{+ url: string; signal?: AbortSignal+ resolve: (response: { ok: boolean; blob: () => Promise<Blob> }) => void+ reject: (reason: Error) => void+ }> = []+ const created: string[] = []+ const revoked: string[] = []+ const unmountHooks: Array<() => void> = []const props = vue.reactive({ knowledgeBaseId: 'kb-1' })const modules: Record<string, unknown> = {- vue: { ...vue, onMounted() {}, onBeforeUnmount() {}, watch() {} },+ vue: { ...vue, onMounted() {}, onBeforeUnmount: (hook: () => void) => unmountHooks.push(hook), watch() {} },'vue-i18n': { useI18n: () => ({ t: (key: string) => key, te: () => false }) },'@/api/image-gallery': {listGalleryImages: (kb: string, params: ImageListParams) => new Promise((resolve, reject) => {@@ -31,12 +40,27 @@ function setup() {fetchGalleryConfig: async () => ({ attributes: [], attribute_sources: [], mode: 'all', status: {} }),},'@/api/auth': { updateMyPreferences: async () => {} },- './galleryImageSrc': { galleryImageRequest: () => null },+ './galleryImageSrc': {+ galleryImageRequest: (url: string, kb: string) => proxyImages && url.startsWith('local://')+ ? { url: `/kb/${kb}/${url}`, headers: { Authorization: 'test-token' } }+ : null,+ },}const state: any = {}vm.runInNewContext(compiled, {exports: state, defineProps: () => props, defineEmits: () => () => {},- setTimeout, clearTimeout, URL, console, Error,+ setTimeout, clearTimeout, console, Error, AbortController,+ fetch: (url: string, options: RequestInit) => new Promise((resolve, reject) => {+ imageCalls.push({ url, signal: options.signal ?? undefined, resolve, reject })+ }),+ URL: {+ createObjectURL: () => {+ const url = `blob:gallery-${created.length + 1}`+ created.push(url)+ return url+ },+ revokeObjectURL: (url: string) => revoked.push(url),+ },require: (id: string) => {assert.ok(id in modules, `unexpected import ${id}`)return modules[id]@@ -47,9 +71,13 @@ function setup() {total, page: calls[index].params.page!, pageSize: 24,})const ids = () => Array.from(state.items.value, (img: ImageAsset) => img.id)- return { state, calls, props, respond, ids }+ const respondImage = (index: number) => imageCalls[index].resolve({ ok: true, blob: async () => new Blob(['image']) })+ const unmount = () => unmountHooks.forEach(hook => hook())+ return { state, calls, props, respond, ids, imageCalls, respondImage, created, revoked, unmount }}+const settleImages = () => new Promise<void>(resolve => setImmediate(resolve))+test('scroll appends unique images, serializes requests, and stops at the total', async () => {const { state, calls, respond, ids } = setup()const first = state.reload()@@ -145,3 +173,117 @@ test('an empty batch stops automatic loading even if the total is stale', asyncassert.equal(await state.loadMore(), false)assert.equal(calls.length, 2)})++test('proxied images share a request and cached URL, then release it on unmount', async () => {+ const { state, imageCalls, respondImage, created, revoked, unmount } = setup(true)+ const first = state.resolveImageSrc('local://image')+ assert.equal(state.resolveImageSrc('local://image'), first)+ assert.equal(imageCalls.length, 1)+ respondImage(0)+ const url = await first+ assert.equal(await state.resolveImageSrc('local://image'), url)+ assert.equal(imageCalls.length, 1)+ assert.deepEqual(created, [url])+ unmount()+ assert.deepEqual(revoked, [url])+ const late = state.resolveImageSrc('local://image')+ assert.equal(imageCalls.length, 1)+ assert.equal(await late, 'local://image')+})++test('unmount aborts a thumbnail fetch and ignores a response that arrives anyway', async () => {+ const { state, calls, imageCalls, created, revoked, unmount } = setup(true)+ const listing = state.reload()+ calls[0].resolve({ items: [{ id: 'a', url: 'local://image' } as ImageAsset], total: 1, page: 1, pageSize: 24 })+ await listing+ const pending = state.resolveImageSrc('local://image')+ unmount()+ assert.equal(imageCalls[0].signal?.aborted, true)+ let bodyReads = 0+ imageCalls[0].resolve({ ok: true, blob: async () => { bodyReads++; return new Blob(['image']) } })+ assert.equal(await pending, 'local://image')+ assert.equal(bodyReads, 0)+ assert.deepEqual(created, [])+ assert.deepEqual(revoked, [])+ assert.deepEqual(Object.keys(state.thumbUrls.value), [])+})++test('unmount during response body reading prevents late Blob URL creation', async () => {+ const { state, imageCalls, created, unmount } = setup(true)+ const pending = state.resolveImageSrc('local://image')+ let finishBody!: (blob: Blob) => void+ const body = new Promise<Blob>(resolve => { finishBody = resolve })+ let bodyReads = 0+ imageCalls[0].resolve({ ok: true, blob: () => { bodyReads++; return body } })+ await settleImages()+ assert.equal(bodyReads, 1)+ unmount()+ finishBody(new Blob(['image']))+ assert.equal(await pending, 'local://image')+ assert.deepEqual(created, [])+})++test('reload cancels old image requests without removing a replacement for the same URL', async () => {+ const { state, imageCalls, respondImage, respond, created, unmount } = setup(true)+ const old = state.resolveImageSrc('local://image')+ const listing = state.reload()+ assert.equal(imageCalls[0].signal?.aborted, true)+ const fresh = state.resolveImageSrc('local://image')+ assert.equal(imageCalls.length, 2)+ assert.equal(imageCalls[1].signal?.aborted, false)+ respondImage(0)+ assert.equal(await old, 'local://image')+ assert.equal(state.resolveImageSrc('local://image'), fresh)+ assert.equal(imageCalls.length, 2)+ respond(0, [], 0)+ await listing+ respondImage(1)+ assert.equal(await fresh, 'blob:gallery-1')+ assert.deepEqual(created, ['blob:gallery-1'])+ unmount()+})++test('switching knowledge bases releases cached URLs and starts new scoped image requests', async () => {+ const { state, props, imageCalls, respondImage, respond, created, revoked, unmount } = setup(true)+ const cached = state.resolveImageSrc('local://cached')+ respondImage(0)+ await cached+ const old = state.resolveImageSrc('local://pending')+ props.knowledgeBaseId = 'kb-2'+ const listing = state.initGallery()+ assert.equal(imageCalls[1].signal?.aborted, true)+ assert.deepEqual(revoked, ['blob:gallery-1'])+ await settleImages()+ respond(0, [], 0)+ await listing+ const fresh = state.resolveImageSrc('local://pending')+ assert.equal(imageCalls[2].url, '/kb/kb-2/local://pending')+ respondImage(1)+ assert.equal(await old, 'local://pending')+ assert.equal(state.resolveImageSrc('local://pending'), fresh)+ respondImage(2)+ await fresh+ const refetch = state.resolveImageSrc('local://cached')+ assert.equal(imageCalls[3].url, '/kb/kb-2/local://cached')+ respondImage(3)+ await refetch+ assert.deepEqual(created, ['blob:gallery-1', 'blob:gallery-2', 'blob:gallery-3'])+ unmount()+})++test('HTTP and network failures allow retry, and ordinary URLs skip the proxy', async () => {+ const { state, imageCalls, respondImage, unmount } = setup(true)+ const raw = 'local://image'+ const failedHTTP = state.resolveImageSrc(raw)+ imageCalls[0].resolve({ ok: false, blob: async () => { throw new Error('must not read') } })+ assert.equal(await failedHTTP, raw)+ const failedNetwork = state.resolveImageSrc(raw)+ imageCalls[1].reject(new Error('offline'))+ assert.equal(await failedNetwork, raw)+ const success = state.resolveImageSrc(raw)+ respondImage(2)+ assert.equal(await success, 'blob:gallery-1')+ assert.equal(await state.resolveImageSrc('https://example.com/image.png'), 'https://example.com/image.png')+ assert.equal(imageCalls.length, 3)+ unmount()+})diff --git a/frontend/src/views/knowledge/gallery/ImageGallery.vue b/frontend/src/views/knowledge/gallery/ImageGallery.vueindex 14a696dc4..5cd793310 100644--- a/frontend/src/views/knowledge/gallery/ImageGallery.vue+++ b/frontend/src/views/knowledge/gallery/ImageGallery.vue@@ -114,6 +114,12 @@ const blobByRawUrl = new Map<string, string>()// Fetches in flight, so the grid and the viewer asking for the same image at// once share one request.const inflight = new Map<string, Promise<string>>()+let imageRequests = new AbortController()++function abortImageRequests(): void {+ imageRequests.abort()+ inflight.clear()+}/** Revoke every cached object URL whose raw URL is not in keep. */function releaseBlobs(keep: Set<string> = new Set()): void {@@ -124,13 +130,17 @@ function releaseBlobs(keep: Set<string> = new Set()): void {}}-async function fetchBlobUrl(rawUrl: string): Promise<string> {+async function fetchBlobUrl(rawUrl: string, signal: AbortSignal): Promise<string> {const req = galleryImageRequest(rawUrl, props.knowledgeBaseId)if (!req) return rawUrltry {- const resp = await fetch(req.url, { headers: req.headers })- if (!resp.ok) return rawUrl- const objectUrl = URL.createObjectURL(await resp.blob())+ const resp = await fetch(req.url, { headers: req.headers, signal })+ if (!resp.ok || signal.aborted) return rawUrl+ const blob = await resp.blob()+ // Cancellation can race with a completed body read. Never create a URL+ // after this query's cleanup, even if the response ignored the abort.+ if (signal.aborted) return rawUrl+ const objectUrl = URL.createObjectURL(blob)blobByRawUrl.set(rawUrl, objectUrl)return objectUrl} catch {@@ -139,11 +149,15 @@ async function fetchBlobUrl(rawUrl: string): Promise<string> {}function resolveImageSrc(rawUrl: string): Promise<string> {+ if (imageRequests.signal.aborted) return Promise.resolve(rawUrl)const cached = blobByRawUrl.get(rawUrl)if (cached) return Promise.resolve(cached)let pending = inflight.get(rawUrl)if (!pending) {- pending = fetchBlobUrl(rawUrl).finally(() => inflight.delete(rawUrl))+ pending = fetchBlobUrl(rawUrl, imageRequests.signal).finally(() => {+ // A newer query may already have started the same URL's replacement.+ if (inflight.get(rawUrl) === pending) inflight.delete(rawUrl)+ })inflight.set(rawUrl, pending)}return pending@@ -493,6 +507,8 @@ function loadMore(): Promise<boolean> {function resetResults() {listToken+++ abortImageRequests()+ imageRequests = new AbortController()viewerOpen.value = falseitems.value = []total.value = 0@@ -583,6 +599,7 @@ onBeforeUnmount(() => {// Orphan any in-flight listing so it cannot map new blobs after unmount.listToken++initToken+++ abortImageRequests()cardObserver?.disconnect()scrollObserver?.disconnect()releaseBlobs()