h2non / bimg

Go package for fast high-level image processing powered by libvips C library
https://pkg.go.dev/github.com/h2non/bimg?tab=doc
MIT License
2.71k stars 338 forks source link

Trim fails with SIGSEGV: segmentation when using in multiple goroutines #290

Open piotrkochan opened 5 years ago

piotrkochan commented 5 years ago

Example code:

package main

import (
    "fmt"
    "github.com/h2non/bimg"
    "sync"
)

func process (img string, wg *sync.WaitGroup) {
    buffer, err := bimg.Read(img)
    if err != nil {
        panic(err)
    }

    source := bimg.NewImage(buffer)

    options := bimg.Options{
        Height: 500,
        Width: 300,
        Type: bimg.JPEG,
        Trim: true,

        //Gravity:   bimg.GravitySmart,
        //Background: bimg.Color{B: 255, G: 255, R: 255},
    }

    newImage, err := source.Process(options)

    if err != nil {
        panic(err)
    }

    fmt.Print(newImage)
}

func main() {
    wg := &sync.WaitGroup{}
    wg.Add(2)

    go process("/home/user/photo/2B/39/90/00/0/@T1210160125-000_0001.png", wg)
    go process("/home/user/photo/2B/39/90/00/0/@T1210160125-000_0002.png", wg)

    wg.Wait()
}

Code fails with error:

(bimg:24025): GLib-GObject-CRITICAL **: 12:30:25.872: g_value_type_compatible: assertion 'G_TYPE_IS_VALUE (src_type)' failed
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x50 pc=0x7f34d473f3af]

runtime stack:
runtime.throw(0x50de46, 0x2a)
        /usr/local/go/src/runtime/panic.go:617 +0x72
runtime.sigpanic()
        /usr/local/go/src/runtime/signal_unix.go:374 +0x4a9
.......

But it works when:

  1. There is no Trim operation
  2. When there is only one running goroutine.
oSethoum commented 1 year ago

Hi @piotrkochan, am facing the same problem, any work around this issue?