fix(web): cancel superseded compression work and bound the ingest fan-out - #40
Merged
Conversation
Each compress run gets its own AbortController; invalidateJob aborts the prior run's controller before bumping the generation, and removeJob/clearAll abort any run still in flight for jobs no longer in the queue.
Dropping many large files fanned out every createImageBitmap decode at once; ingestFiles now runs through the same pool the compress queue uses, so at most MAX_CONCURRENT_JOBS decode in parallel.
…le mock Bun's mock.module patches the shared module registry for the whole test run. use-optimizer.test.tsx replaces @/lib/image/ingest wholesale and only restores it in its own afterAll, which runs after collection has already resolved every file's imports; a separate file importing the real ingest.ts gets the stub instead. runWithConcurrency's bounding and ordering are already covered by lib/compress/__tests__/pool.test.ts.
jobsRef.current.length was read before the ingest await, so a second drop during a slow ingest saw the pre-ingest count and could bypass MAX_FILES. pendingIngestRef tracks files already claimed but not yet committed to jobsRef, so the next addFiles call sees an accurate starting count.
quality and outputFormat were sanitized but resize dimensions passed through raw; a large typed value reached the browser canvas path unclamped.
|
Contributor
|
React Doctor found no new issues. 🎉 Reviewed by React Doctor for commit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reopens the work from #39, which GitHub closed automatically when #38 merged and its base branch was deleted. Rebased onto main; the diff is unchanged.
Why
compressWithApi'sfetchtook noAbortSignal. The generation map discarded stale results, but superseded requests still ran to completion, so dragging the quality slider stacked overlapping batches of uploads. A hung request never settled at all: the job stuck in "processing", Recompress stayed disabled, and four hung jobs deadlocked the queue until reload.ingestFilesranPromise.allover every accepted file. Dropping 50 large images started 50 simultaneous full-resolutioncreateImageBitmapdecodes before any compression began, and the pixel-limit check ran after the decode.addFilesread the job count before anawait, so a second drop during a slow ingest saw the old count and bypassed the 50-file ceiling.sanitizeCompressionOptionsclamped quality but passedresizeWidth/resizeHeightthrough raw, so a large typed value allocated a tab-killing canvas.What changed
compressWithApiaccepts an optional signal and always applies a 120sAbortSignal.timeoutceiling. The server's write timeout is 90s, so the client only gives up after the server certainly has.AbortControllerper job run.invalidateJobaborts the prior run before claiming the next generation, andremoveJob/clearAllabort what they discard. A timeout surfaces as "The API did not respond in time."; a manual abort is always a stale run and stays silent. The browser encode path is untouched: canvas encoding has no abort API, and the stale check after the await is its cancellation.runWithConcurrencyatMAX_CONCURRENT_JOBS, preserving outcome order by index.addFilesclaims its file count in a ref before awaiting, so concurrent drops see each other and the 50-file gate holds.resizeWidth/resizeHeightare rounded and clamped to[0, 16384]in the sanitize funnel every run path already flows through.Tests
The supersession and removal tests now also assert the first run's signal was aborted. A new test holds an ingest open and asserts the second
addFilessees the first drop's file count; it fails without the fix, verified by stashing. Three new cases cover the dimension clamp. 59 pass, 0 fail, typecheck clean.Notes for review
ingest.test.tswas written and then removed: Bun'smock.modulepatches the module registry for the whole test process, and the use-optimizer suite replaces@/lib/image/ingestbefore other files import it, so the ingest test could only ever see the stub. The bounding change is covered by thePromise.allremoval, the pool's own invariant tests, and the full suite.addFiles's try/finally wraps only theawait, not the whole body as first drafted, because the wider shape tripped oxlint's react-compiler memo-dependency rule. Behavior is identical.