Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic When Using canvas.Image #327

Closed
Seanld opened this issue Dec 3, 2024 · 1 comment
Closed

Panic When Using canvas.Image #327

Seanld opened this issue Dec 3, 2024 · 1 comment

Comments

@Seanld
Copy link

Seanld commented Dec 3, 2024

I had been using canvas.Image instead of image.Image in my code because the documentation mentioned possible decrease in encoding/decoding overhead. It worked for a long time, but I pulled a new version of this project while managing dependencies, and it started panicking telling me hash of unhashable type canvas.Image.

It seems to be a problem with embedImage using image.Image as a key in a map, but not expecting canvas.Image to be used in its place. I'm not sure what changed to break this, but it must be a library-side regression.

I changed my code to use image.Image and now it works exactly as intended.

Full stack trace:

panic: panic: runtime error: hash of unhashable type canvas.Image
       stacktrace:
       goroutine 5 [running]:
       runtime/debug.Stack()
               /usr/local/go/src/runtime/debug/stack.go:26 +0x67
       github.com/sourcegraph/conc/panics.NewRecovered(0x1, {0x12346c0, 0xc000198350})
               /go/pkg/mod/github.com/sourcegraph/[email protected]/panics/panics.go:59 +0xc7
       github.com/sourcegraph/conc/panics.(*Catcher).tryRecover(0xc00019cbb0)
               /go/pkg/mod/github.com/sourcegraph/[email protected]/panics/panics.go:28 +0x7b
       panic({0x12346c0?, 0xc000198350?})
               /usr/local/go/src/runtime/panic.go:785 +0x132
       github.com/tdewolff/canvas/renderers/pdf.(*pdfPageWriter).embedImage(0xc0001b2280, {0x14ad080, 0xc0001a07c0}, 0x0)
               /go/pkg/mod/github.com/tdewolff/[email protected]/renderers/pdf/writer.go:1172 +0xb0
       github.com/tdewolff/canvas/renderers/pdf.(*pdfPageWriter).DrawImage(0xc0001b2280, {0x14ad080, 0xc0001a07c0}, 0x0, {{0x3c57e8960d3791cb, 0xbfb5acb6f46508e0, 0x406396f9db22d0e6}, {0x3fb5acb6f46508e0, 0x3c57e8960d3791cb, 0x403560da740da73e}})
               /go/pkg/mod/github.com/tdewolff/[email protected]/renderers/pdf/writer.go:1159 +0x890
       github.com/tdewolff/canvas/renderers/pdf.(*PDF).RenderImage(0xc000172120, {0x14ad080, 0xc0001a07c0}, {{0x3c57e8960d3791cb, 0xbfb5acb6f46508e0, 0x406396f9db22d0e6}, {0x3fb5acb6f46508e0, 0x3c57e8960d3791cb, 0x403560da740da73e}})
               /go/pkg/mod/github.com/tdewolff/[email protected]/renderers/pdf/pdf.go:256 +0xcb
       github.com/tdewolff/canvas.(*Canvas).RenderViewTo(0xc0001a5120, {0x14aea50, 0xc000172120}, {{0x3ff0000000000000, 0x0, 0x0}, {0x0, 0x3ff0000000000000, 0x0}})
               /go/pkg/mod/github.com/tdewolff/[email protected]/canvas.go:831 +0x492
       github.com/tdewolff/canvas.(*Canvas).RenderTo(...)
               /go/pkg/mod/github.com/tdewolff/[email protected]/canvas.go:812
       github.com/tdewolff/canvas/renderers.PDF.func1({0x14a5c40, 0xc00013e038}, 0xc0001a5120)
               /go/pkg/mod/github.com/tdewolff/[email protected]/renderers/renderers.go:239 +0x12c
       github.com/tdewolff/canvas.(*Canvas).WriteFile(0xc0001a5120, {0xc00023b5c0, 0x1f}, 0xc000138060)
               /go/pkg/mod/github.com/tdewolff/[email protected]/canvas.go:852 +0x77
       github.com/tdewolff/canvas/renderers.Write({0xc00023b5c0, 0x1f}, 0xc0001a5120, {0x0, 0x0, 0x0})
               /go/pkg/mod/github.com/tdewolff/[email protected]/renderers/renderers.go:49 +0x34c
       everyname-renderer/lib/pagemaker.(*Page).RenderToFile(0xc00014c9c0, {0xc00023b5c0, 0x1f}, {0x14b0a68, 0x1b87560}, 0xc0001a5100)
               /renderer/lib/pagemaker/page.go:63 +0x5a5
       everyname-renderer/lib/renderer.Renderer.RenderBatch.func1()
               /renderer/lib/renderer/batch.go:57 +0xf2
       everyname-renderer/lib/renderer.Renderer.RenderBatch.(*ErrorPool).Go.func2()
               /go/pkg/mod/github.com/sourcegraph/[email protected]/pool/error_pool.go:30 +0x34
       github.com/sourcegraph/conc/pool.(*Pool).worker(0xc00019cba0)
               /go/pkg/mod/github.com/sourcegraph/[email protected]/pool/pool.go:154 +0xde
       github.com/sourcegraph/conc/panics.(*Catcher).Try(0xc00019cbb0, 0xc000036b50)
               /go/pkg/mod/github.com/sourcegraph/[email protected]/panics/panics.go:23 +0x78
       github.com/sourcegraph/conc.(*WaitGroup).Go.func1()
               /go/pkg/mod/github.com/sourcegraph/[email protected]/waitgroup.go:32 +0x90
       created by github.com/sourcegraph/conc.(*WaitGroup).Go in goroutine 34
               /go/pkg/mod/github.com/sourcegraph/[email protected]/waitgroup.go:30 +0xe5
@tdewolff
Copy link
Owner

tdewolff commented Dec 6, 2024

You should probably use a pointer to canvas.Image, i.e. allocate it. I believe this only speeds up the SVG renderer.

@Seanld Seanld closed this as completed Jan 15, 2025
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

No branches or pull requests

2 participants