1
0
mirror of https://github.com/gohugoio/hugo.git synced 2024-05-11 05:54:58 +00:00

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
This commit is contained in:
Bjørn Erik Pedersen
2018-02-13 21:45:51 +01:00
parent 53dac9a506
commit 58382e9572
4 changed files with 120 additions and 5 deletions

View File

@@ -28,7 +28,8 @@ type imageCache struct {
absCacheDir string
pathSpec *helpers.PathSpec
mu sync.RWMutex
store map[string]*Image
store map[string]*Image
}
func (c *imageCache) isInCache(key string) bool {
@@ -69,6 +70,11 @@ func (c *imageCache) getOrCreate(
}
// Now look in the file cache.
// Multiple Go routines can invoke same operation on the same image, so
// we need to make sure this is serialized per source image.
parent.createMu.Lock()
defer parent.createMu.Unlock()
cacheFilename := filepath.Join(c.absCacheDir, key)
// The definition of this counter is not that we have processed that amount