resource: Fix multi-threaded image processing issue
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 13 Feb 2018 20:45:51 +0000 (21:45 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 15 Feb 2018 08:41:29 +0000 (09:41 +0100)
commitd8fdffb55268464d54558d6f9cd3874b612dc7c7
tree433266be0c7d178fcc6b1868712b00eb9173b408
parent2851af0225cdf6c4e47058979cd22949ed6d1fc0
resource: Fix multi-threaded image processing issue

When doing something like this with the same image from a partial used in, say, both the home page and the single page:

```bash
{{ with $img }}
{{ $big := .Fill "1024x512 top" }}
{{ $small := $big.Resize "512x" }}
{{ end }}
```

There would be timing issues making Hugo in some cases try to process the same image with the same instructions in parallel.

You would experience errors of type:

```bash
png: invalid format: not enough pixel data
```

This commit works around that by adding a mutex per image. This should also improve the performance, sligthly, as it avoids duplicate work.

The current workaround before this fix is to always operate on the original:

```bash
{{ with $img }}
{{ $big := .Fill "1024x512 top" }}
{{ $small := .Fill "512x256 top" }}
{{ end }}
```

Fixes #4404
resource/image.go
resource/image_cache.go
resource/image_test.go
resource/testhelpers_test.go