I have written an Beego HTTP server, that when a user hits an endpoint:
- the server requests an image from another server (for instance imgur)
- it reads the bytes of the image and passes them to gographics/imagick
- this (should) resize the image and return the byte array of the result
what actually happens is my HTTP server completely hangs, I don't even get to the error handling, and I get a 502 bad gateway on all endpoints of the server.
My code looks like this:
func processContactImage(idx int, image []byte) ([]byte, error) {
imagick.Initialize()
defer imagick.Terminate()
log.Println("idx: ", idx)
mw := imagick.NewMagickWand()
log.Println("reading image blob: ", image)
err := mw.ReadImageBlob(image)
if err != nil {
log.Println("reading blob failed: ", err)
return []byte{}, err
}
//...
}
I can see in the terminal the log message "reading image blob: [bytes, bytes bytes]" and I have copied the bytes printed to another small program to test the bytes do indeed hold an image, they do.
It completely hangs on err := mw.ReadImageBlob(image)
and I don't think it even gets into the if err != nil
as I never see that log message.
Tips on how I should debug this would be welcome. I have written a small program to test the image magick funtions work on the byte array in a standalone enviroment, and it all works fine.
My thoughts:
- I don't completely understand how Go handles stack/heap, I thought it was able to move things to the heap if necessary and I didn't need to manage this. However I am storing an image in memory, I thought perhaps
seg fault
but Im not sure why it doesn't crash but hangs... -
ReadImageBlob
is expecting a type of image data, and its not getting it, although I would have then thought it got to the error
EDIT:
OK thanks for comments, after more research, it seems like it is related to the fact I am running this in Docker, it hadn't occured that this could be an issue however:
- I have moved the initialization of imagemagick to main and the error still occurs
- When I run the application without docker, and I pass a byte array to the handler, imagemagick code runs fine.
- When I attach to the docker container, add a small test program that adds a circle to an image using imagemagick (however not a web service, just a binary) it works, albeit very slowly
My dockerfile looks like this:
FROM golang:1.7-alpine
RUN apk update && apk add git && apk add g++ && apk add bzr && \
rm -rf /var/cache/apk/*
# ENV GOPATH /go
# Install beego & bee
RUN go get github.com/astaxie/beego
RUN go get github.com/beego/bee
RUN go get github.com/tools/godep
RUN apk add --update alpine-sdk
RUN apk add imagemagick-dev
RUN go get gopkg.in/gographics/imagick.v2/imagick
I wonder if I am missing a library or something and its hanging inside the C api and Go is waiting for a response. Is there any way I can debug this?
OK... As it turns out the issue is something else... perhaps multiple requests at once or something... I'm not sure, but I have created this gist demonstrating using imagemagick, in Go, in a handler, and it works locally no problem inside my docker container. The mystery continues....