Skip to content

Make the render timeout stop work and bound concurrent renders - #117

Open
Aerhhh wants to merge 2 commits into
security/input-clamps-pixel-budgetfrom
security/render-timeout-cancellation
Open

Aerhhh wants to merge 2 commits into
security/input-clamps-pixel-budgetfrom
security/render-timeout-cancellation

Conversation

@Aerhhh

@Aerhhh Aerhhh commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Why

Follow-up to the security review (see #116). GeneratorImageBuilder.build() cancelled its task on timeout, but no render loop checked the interrupt flag, so a timed-out render ran to completion, held its memory, and was still written to the cache. Builds also ran on an unbounded virtual-thread executor, so an attacker could stack as many of these as they could send.

What changed

  • RenderCancellation.check() (new) is called once per frame, line or item in the tooltip, inventory, container, HUD, scene, glint, composite and GIF-encoding loops. A cancelled render now ends at its next check with GeneratorTimeoutException.
  • No cache write after cancellation: Generator.generate checks between render and the cache put.
  • Concurrency limit: a fair semaphore caps simultaneous builds. image.generator.maxConcurrentRenders sets it (default max(2, CPUs), read once at class load). A build that cannot get a slot within the timeout fails with GeneratorTimeoutException. The slot is released by the render task, not the waiting caller, so a render still winding down keeps counting. A build started from inside a render reuses its parent's slot.
  • Timeout type: build() now throws GeneratorTimeoutException (a GeneratorException, same message) on timeout, and cancels the task if the waiting caller is interrupted.

Behaviour changes to be aware of

  • Under load, builds now queue for a slot instead of all running at once; the wait counts against the same timeout.
  • Renders stuck inside code with no check (Marmalade's ImageUtil.toGifBytes, JDK image I/O) still run until that call returns.

Not in this PR

NBT and scene parsing still run on the caller's thread before build(), outside the timeout. Moving them is an API change; their cost is bounded separately (#116 and the parser follow-up).

Testing

  • New GeneratorImageBuilderTimeoutTest (3 tests): a spinning render is stopped by the timeout, a render that finishes after cancellation is not cached, and a nested build completes.
  • Targeted run of 77 tests across the touched generators: 76 pass. The one error is DisplayToolTest.enchantedRenderIsAnimated hitting the 20 s render timeout on a loaded local machine; it fails the same way on unmodified master there and passed in CI on Bound untrusted render input with clamps and a shared pixel budget #116. The full suite was not run locally.

A timed-out build only interrupted its task, and no render loop looked at
the interrupt flag, so the work ran to completion, kept its memory and was
still written to the cache. Builds also ran on an unbounded executor.

- Add RenderCancellation.check() and call it once per frame, line or item
  in the tooltip, inventory, container, HUD, scene, glint, composite and
  GIF encoding loops.
- Skip the cache write for a render that finished after cancellation.
- Limit concurrent builds with a fair semaphore
  (image.generator.maxConcurrentRenders, default max(2, CPUs)); a build
  that cannot get a slot within the timeout fails. Nested builds reuse
  their parent's slot.
- Throw GeneratorTimeoutException on timeout and cancel the task when the
  waiting caller is interrupted.
Both branches add an import and a check at the same render loops; the
only conflicts were the adjacent import lines.
@Aerhhh
Aerhhh changed the base branch from master to security/input-clamps-pixel-budget November 21, 2026 02:02
@Aerhhh

Aerhhh commented Nov 21, 2026

Copy link
Copy Markdown
Owner Author

This now sits on top of #116: both changes add a line at the same render loops, so I merged #116's branch into this one (the only conflicts were adjacent import lines) and pointed this PR at that branch. Merge #116 first; GitHub will retarget this PR to master when that branch goes away. The other follow-ups (#118 to #122) are independent, and all seven branches merge together cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant