Skip to content

fix(apple): keep a cached canvas image's inline data alive - #737

Merged
houko merged 1 commit into
mainfrom
fix/apple-canvas-cache-race
Nov 19, 2026
Merged

houko merged 1 commit into
mainfrom
fix/apple-canvas-cache-race

Conversation

@houko

@houko houko commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #715. Apple half of #738.

ImageSource.Key names inline data by its backing address and length, and the Apple canvas image cache's loadedImage did not keep the source. Once an app dropped an inline Data buffer, a new buffer of the same length could land at the same address, get the old cache entry, and the canvas drew the old picture with no error. The entry now keeps its source, as the Linux and Windows image slots (imageSlot.src) and Android's canvasSource already do, so an address cannot be reused while its key is cached.

This was also the cause of the TestCanvasImageCacheKeepsAWidePass flake: after the first pass's sources were collected, later sources reused their addresses and matched the evicted entries. The test is unchanged and no longer needs anything to keep its sources alive.

The web backend has the same gap; it is left to #738.

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS
  • Android
  • Windows
  • Linux
  • Web
  • Mini programs
  • Docs or website only

Screenshots

None. No visual change for an app whose buffers stay alive.

Verification

  • go test -count=50 -run TestCanvasImageCache ./internal/platform/apple on macOS arm64: passes 50 of 50; with the fix reverted, 13 of 50 runs fail with "a wide pass's evicted image kept its native image".
  • go test ./internal/platform/apple passes.
  • GOOS=ios GOARCH=arm64 go vet ./internal/platform/apple passes.

Checklist

  • The title is a Conventional Commit.
  • Docs whose facts change are updated.
  • No new third-party code, assets or dependencies.

@houko

houko commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

The diagnosis is right, but the reuse it works around is a real bug, not only a test artifact: loadedImage (internal/platform/apple/canvas.go:784) does not keep src, so once an app drops an inline Data buffer, a new buffer of the same length at the same address gets the old entry and the canvas draws the old picture. Linux and Windows avoid this by keeping src in imageSlot, and Android in canvasSource. Filed as #738, which also covers the web backend.

Suggest fixing it in the cache instead: keep the source (or its Data) in loadedImage, so a key cannot be reused while its entry exists. The test then needs no runtime.KeepAlive, and #715 is fixed at its cause.

ImageSource.Key names inline data by its address, and loadedImage did not keep the source, so once an app dropped a buffer, a new one of the same length at the same address got the old entry and a canvas drew the old picture. The entry now keeps its source, as Linux and Windows image slots do. This was also the cause of the TestCanvasImageCacheKeepsAWidePass flake.

Fixes #715
@houko
houko force-pushed the fix/apple-canvas-cache-race branch from 798e9d3 to c39b0fd Compare November 19, 2026 00:04
@houko houko changed the title test(apple): keep canvas cache sources alive fix(apple): keep a cached canvas image's inline data alive Nov 19, 2026
@github-actions github-actions Bot added the bug Something isn't working label Nov 19, 2026
@houko

houko commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

Reviewed independently. canvasImage is the only path in the Apple backend that keys by ImageSource.Key() (shaders go through it; their textures are matched with Same against sources they hold, and Pixels go through canvasFrame), so keeping src in loadedImage closes the gap there. The extra memory is at most the encoded bytes of the 64 cached entries plus the current pass and loads under way, freed on eviction. TestCanvasImageCacheKeepsAWidePass still checks that eviction releases native images: its kept entries now hold the first pass's buffers, so the second pass cannot reuse their addresses and must evict them. On macOS arm64, -count=50 -run TestCanvasImageCache passes 50/50, and fails 13/50 with the fix reverted. CI is green on c39b0fd. The changelog line for this fix comes with #751, which covers the web half.

@houko
houko merged commit a42b4cc into main Nov 19, 2026
23 checks passed
@houko
houko deleted the fix/apple-canvas-cache-race branch November 19, 2026 00:25
houko added a commit to houko/nsgo that referenced this pull request Nov 19, 2026
ImageSource.Key names inline data by its address, and the web canvas image cache did not keep the source, so a collected buffer's address could come back in a new buffer of the same length and be handed the old image, which the cache never evicts. The entry now keeps its source, as the Apple canvas cache does since nsgo-dev#737 and the Linux and Windows canvas image slots already did.

Part of nsgo-dev#738
houko added a commit that referenced this pull request Nov 19, 2026
ImageSource.Key names inline data by its address, and the web canvas image cache did not keep the source, so a collected buffer's address could come back in a new buffer of the same length and be handed the old image, which the cache never evicts. The entry now keeps its source, as the Apple canvas cache does since #737 and the Linux and Windows canvas image slots already did.

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

Labels

bug Something isn't working iOS macOS size/XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apple: TestCanvasImageCacheKeepsAWidePass is flaky (evicted image still holds its native image)

1 participant