fix(audit-g2): async race / state-leak across eight stores
Extracts gallery.js's hand-rolled inflightId pattern into a new useInflightToken composable; adopts in every store that previously had no guard against late-response overwrites or wrong-image URL interpolation. Two operator-impacting bugs the audit (workflow wf_bbe3fdb1-e62) flagged: - modal.removeTag rolled back the chip rail unconditionally even when only the secondary dismiss POST had failed — UI lied until refresh. And all tag-mutation URLs interpolated currentImageId AFTER an await, so a fast prev/next could route DELETE/POST to the wrong image. Both fixed: split try/catch (dismiss failure surfaces a warning, doesn't roll back the delete); imageId captured at call-time and used in URLs throughout. - suggestions.accept dereferenced currentImageId after the awaited POST /api/tags, so the subsequent /suggestions/accept could apply A's chosen tag to image B AND push it to B's allowlist. Fixed by capturing imageId at click-time + inflight guard on load(). Same shape across artist / downloads / artistDirectory / tagDirectory / posts stores: rapid filter/nav changes used to interleave responses (last-writer-wins). Now the late response is discarded and the most-recent request wins. Filter-change-during- search no longer drops the second fetch because the loading flag was still true from the first. gallery.js's inflightId removed in favor of the shared composable so the pattern stays consistent.
This commit is contained in:
@@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { useApi } from '../composables/useApi.js'
|
||||
import { useAsyncAction } from '../composables/useAsyncAction.js'
|
||||
import { useInflightToken } from '../composables/useInflightToken.js'
|
||||
|
||||
export const usePostsStore = defineStore('posts', () => {
|
||||
const api = useApi()
|
||||
@@ -19,6 +20,12 @@ export const usePostsStore = defineStore('posts', () => {
|
||||
const doneOlder = ref(false)
|
||||
const doneNewer = ref(false)
|
||||
const anchorId = ref(null)
|
||||
// loadInitial, loadMore, loadAround, loadOlder, loadNewer all share
|
||||
// one `loading` flag and previously had no inflight guard. A filter
|
||||
// change (loadInitial) racing a still-in-flight loadMore would
|
||||
// append the prior filter's items into the new filter's feed.
|
||||
// Audit 2026-06-02.
|
||||
const inflight = useInflightToken()
|
||||
|
||||
function _qs() {
|
||||
const q = {}
|
||||
@@ -36,6 +43,7 @@ export const usePostsStore = defineStore('posts', () => {
|
||||
}
|
||||
|
||||
async function loadInitial(newFilters) {
|
||||
inflight.cancel()
|
||||
filters.value = {
|
||||
artist_id: newFilters?.artist_id ?? null,
|
||||
platform: newFilters?.platform ?? null,
|
||||
@@ -46,8 +54,10 @@ export const usePostsStore = defineStore('posts', () => {
|
||||
|
||||
async function loadMore() {
|
||||
if (loading.value || done.value) return
|
||||
const t = inflight.claim()
|
||||
await run(async () => {
|
||||
const body = await api.get('/api/posts', { params: _qs() })
|
||||
if (!t.isCurrent()) return
|
||||
items.value.push(...body.items)
|
||||
cursor.value = body.next_cursor
|
||||
if (body.next_cursor == null) done.value = true
|
||||
@@ -79,6 +89,7 @@ export const usePostsStore = defineStore('posts', () => {
|
||||
// posts feed; without this the older/newer scroll loaded unfiltered
|
||||
// global posts instead of staying in the artist's stream).
|
||||
async function loadAround(postId, newFilters) {
|
||||
inflight.cancel()
|
||||
filters.value = {
|
||||
artist_id: newFilters?.artist_id ?? null,
|
||||
platform: newFilters?.platform ?? null,
|
||||
@@ -86,10 +97,12 @@ export const usePostsStore = defineStore('posts', () => {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
anchorId.value = null
|
||||
const t = inflight.claim()
|
||||
try {
|
||||
const body = await api.get('/api/posts', {
|
||||
params: _aroundParams({ around: postId }),
|
||||
})
|
||||
if (!t.isCurrent()) return
|
||||
items.value = body.items
|
||||
cursorOlder.value = body.cursor_older
|
||||
cursorNewer.value = body.cursor_newer
|
||||
@@ -97,47 +110,54 @@ export const usePostsStore = defineStore('posts', () => {
|
||||
doneNewer.value = body.cursor_newer == null
|
||||
anchorId.value = body.anchor_id
|
||||
} catch (e) {
|
||||
if (!t.isCurrent()) return
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
if (t.isCurrent()) loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOlder() {
|
||||
if (loading.value || doneOlder.value || cursorOlder.value == null) return
|
||||
loading.value = true
|
||||
const t = inflight.claim()
|
||||
try {
|
||||
const body = await api.get('/api/posts', {
|
||||
params: _aroundParams({
|
||||
cursor: cursorOlder.value, direction: 'older',
|
||||
}),
|
||||
})
|
||||
if (!t.isCurrent()) return
|
||||
items.value.push(...body.items)
|
||||
cursorOlder.value = body.next_cursor
|
||||
if (body.next_cursor == null) doneOlder.value = true
|
||||
} catch (e) {
|
||||
if (!t.isCurrent()) return
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
if (t.isCurrent()) loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadNewer() {
|
||||
if (loading.value || doneNewer.value || cursorNewer.value == null) return
|
||||
loading.value = true
|
||||
const t = inflight.claim()
|
||||
try {
|
||||
const body = await api.get('/api/posts', {
|
||||
params: _aroundParams({
|
||||
cursor: cursorNewer.value, direction: 'newer',
|
||||
}),
|
||||
})
|
||||
if (!t.isCurrent()) return
|
||||
items.value.unshift(...body.items)
|
||||
cursorNewer.value = body.next_cursor
|
||||
if (body.next_cursor == null) doneNewer.value = true
|
||||
} catch (e) {
|
||||
if (!t.isCurrent()) return
|
||||
error.value = e
|
||||
} finally {
|
||||
loading.value = false
|
||||
if (t.isCurrent()) loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user