gen2brain / go-fitz

Golang wrapper for the MuPDF Fitz library
GNU Affero General Public License v3.0
369 stars 87 forks source link

system crash when converting page to image #82

Closed bao-huynh-ostv closed 1 year ago

bao-huynh-ostv commented 1 year ago

Hi, im converting around 30 pages to jpg, size of each image around 500kb to 2mb. Some image was converted successfully but some throw errors

fatal error: non-Go code set up signal handler without SA_ONSTACK flag

runtime stack:
runtime.throw({0x1052c8854?, 0x14000072000?})
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/panic.go:1047 +0x40 fp=0x140001e8e10 sp=0x140001e8de0 pc=0x10488a6f0
runtime.sigNotOnStack(0x10)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/signal_unix.go:1029 +0x5c fp=0x140001e8e30 sp=0x140001e8e10 pc=0x1048a09fc
runtime.adjustSignalStack(0x10, 0x105e4c040, 0x140001e8ed8)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/signal_unix.go:581 +0x250 fp=0x140001e8ea0 sp=0x140001e8e30 pc=0x10489fd30
runtime.sigtrampgo(0x10, 0x140001e9040, 0x140001e90a8)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/signal_unix.go:469 +0x170 fp=0x140001e8f20 sp=0x140001e8ea0 pc=0x10489f980
runtime.sigtrampgo(0x10, 0x140001e9040, 0x140001e90a8)
        <autogenerated>:1 +0x1c fp=0x140001e8f50 sp=0x140001e8f20 pc=0x1048bde5c
runtime.sigtramp()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/sys_darwin_arm64.s:227 +0x4c fp=0x140001e9010 sp=0x140001e8f50 pc=0x1048bcccc

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/asm_arm64.s:1172 +0x4 fp=0x14000084fe0 sp=0x14000084fe0 pc=0x1048bbc94

goroutine 1 [IO wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/proc.go:381 +0xe0 fp=0x1400011fa70 sp=0x1400011fa50 pc=0x10488d0c0
runtime.netpollblock(0x140002efb08?, 0x492b4f4?, 0x1?)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/netpoll.go:527 +0x158 fp=0x1400011fab0 sp=0x1400011fa70 pc=0x104886838
internal/poll.runtime_pollWait(0x12d732038, 0x72)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/netpoll.go:306 +0xa0 fp=0x1400011fae0 sp=0x1400011fab0 pc=0x1048b5b10
internal/poll.(*pollDesc).wait(0x14000193900?, 0x10486bfe0?, 0x0)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/internal/poll/fd_poll_runtime.go:84 +0x28 fp=0x1400011fb10 sp=0x1400011fae0 pc=0x104926b58
internal/poll.(*pollDesc).waitRead(...)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/internal/poll/fd_poll_runtime.go:89
internal/poll.(*FD).Accept(0x14000193900)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/internal/poll/fd_unix.go:614 +0x250 fp=0x1400011fbc0 sp=0x1400011fb10 pc=0x10492b5e0
net.(*netFD).accept(0x14000193900)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/net/fd_unix.go:172 +0x28 fp=0x1400011fc80 sp=0x1400011fbc0 pc=0x1049ece78
net.(*TCPListener).accept(0x1400000f9f8)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/net/tcpsock_posix.go:148 +0x28 fp=0x1400011fcb0 sp=0x1400011fc80 pc=0x104a03a68
net.(*TCPListener).AcceptTCP(0x1400000f9f8)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/net/tcpsock.go:284 +0x2c fp=0x1400011fcf0 sp=0x1400011fcb0 pc=0x104a02afc
github.com/labstack/echo/v4.tcpKeepAliveListener.Accept({0x140002efd48?})
        /Users/baohuynh/go/pkg/mod/github.com/labstack/echo/v4@v4.10.2/echo.go:982 +0x1c fp=0x1400011fd30 sp=0x1400011fcf0 pc=0x104f7264c
github.com/labstack/echo/v4.(*tcpKeepAliveListener).Accept(0x140000021a0?)
        <autogenerated>:1 +0x2c fp=0x1400011fd50 sp=0x1400011fd30 pc=0x104f78b6c
net/http.(*onceCloseListener).Accept(0x140001d4b40?)
        <autogenerated>:1 +0x30 fp=0x1400011fd70 sp=0x1400011fd50 pc=0x104b0f700
net/http.(*Server).Serve(0x1400030a0f0, {0x1058b10d0, 0x14000010988})
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/net/http/server.go:3059 +0x2cc fp=0x1400011fea0 sp=0x1400011fd70 pc=0x104aecdec
github.com/labstack/echo/v4.(*Echo).Start(0x14000342240, {0x1052948ec, 0x5})
        /Users/baohuynh/go/pkg/mod/github.com/labstack/echo/v4@v4.10.2/echo.go:679 +0xc0 fp=0x1400011fee0 sp=0x1400011fea0 pc=0x104f708c0
main.main()
        /Users/baohuynh/PhuocBao/Coding/otsv/one-gallery/api-one-magazine/cmd/main.go:23 +0xfc fp=0x1400011ff70 sp=0x1400011fee0 pc=0x104f7966c
runtime.main()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/proc.go:250 +0x200 fp=0x1400011ffd0 sp=0x1400011ff70 pc=0x10488ccb0
runtime.goexit()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/asm_arm64.s:1172 +0x4 fp=0x1400011ffd0 sp=0x1400011ffd0 pc=0x1048bbc94

Below is the code, i just following the snippet Screen Shot 2023-05-17 at 10 12 06

I'm using arm64 arch to execute those code

gen2brain commented 1 year ago

I don't see any reference to go-fitz in your crash. What is needed is a small reproducible code with problematic documents, not some description of what you are doing. Sorry, like it is nobody can help.

bao-huynh-ostv commented 1 year ago

I don't see any reference to go-fitz in your crash. What is needed is a small reproducible code with problematic documents, not some description of what you are doing. Sorry, like it is nobody can help.

Hi @gen2brain , sorry for my missing code, i just updated the code

gen2brain commented 1 year ago

@bao-huynh-ostv How can I test and reproduce a snippet of code that is in the image? Why do you even think go-fitz is to blame when is not even mentioned in panic, can you try to get a gdb trace? As I said, what is needed here is a small reproducible code that will crash, not the whole of your project with a screenshot of some part of the fitz code that is proven to work and is the same as included tests.

gen2brain commented 1 year ago

Basically, does this code work or not for you, if not, the document you are using can possibly be useful:

package main

import (
    "fmt"
    "image/jpeg"
    "os"
    "path/filepath"

    "github.com/gen2brain/go-fitz"
)

func main() {
    doc, err := fitz.New("test.pdf")
    if err != nil {
        panic(err)
    }

    defer doc.Close()

    tmpDir, err := os.MkdirTemp(os.TempDir(), "fitz")
    if err != nil {
        panic(err)
    }

    // Extract pages as images
    for n := 0; n < doc.NumPage(); n++ {
        img, err := doc.Image(n)
        if err != nil {
            panic(err)
        }

        f, err := os.Create(filepath.Join(tmpDir, fmt.Sprintf("test%03d.jpg", n)))
        if err != nil {
            panic(err)
        }

        err = jpeg.Encode(f, img, &jpeg.Options{jpeg.DefaultQuality})
        if err != nil {
            panic(err)
        }

        f.Close()
    }
}
bao-huynh-ostv commented 1 year ago

Hi @gen2brain, i try to use those code but it still got errors related to goroutine. It can only convert successfully 2 pages

signal 16 received but handler not on signal stack
fatal error: non-Go code set up signal handler without SA_ONSTACK flag

runtime stack:
runtime.throw({0x10474b7a3?, 0x14000052000?})
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/panic.go:1047 +0x40 fp=0x140023d3440 sp=0x140023d3410 pc=0x1043ba360
runtime.sigNotOnStack(0x10)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/signal_unix.go:1029 +0x5c fp=0x140023d3460 sp=0x140023d3440 pc=0x1043cf00c
runtime.adjustSignalStack(0x10, 0x14002400000, 0x140023d3508)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/signal_unix.go:581 +0x250 fp=0x140023d34d0 sp=0x140023d3460 pc=0x1043ce340
runtime.sigtrampgo(0x10, 0x140023d3670, 0x140023d36d8)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/signal_unix.go:469 +0x170 fp=0x140023d3550 sp=0x140023d34d0 pc=0x1043cdf90
runtime.sigtrampgo(0x10, 0x140023d3670, 0x140023d36d8)
        <autogenerated>:1 +0x1c fp=0x140023d3580 sp=0x140023d3550 pc=0x1043e8acc
runtime.sigtramp()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/sys_darwin_arm64.s:227 +0x4c fp=0x140023d3640 sp=0x140023d3580 pc=0x1043e7cbc

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/asm_arm64.s:1172 +0x4 fp=0x14000060fe0 sp=0x14000060fe0 pc=0x1043e6c94

goroutine 1 [running]:
        goroutine running on other thread; stack unavailable

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/proc.go:381 +0xe0 fp=0x1400004efa0 sp=0x1400004ef80 pc=0x1043bcd30
runtime.goparkunlock(...)
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/proc.go:387
runtime.forcegchelper()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/proc.go:305 +0xb0 fp=0x1400004efd0 sp=0x1400004efa0 pc=0x1043bcb80
runtime.goexit()
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/asm_arm64.s:1172 +0x4 fp=0x1400004efd0 sp=0x1400004efd0 pc=0x1043e6c94
created by runtime.init.6
        /opt/homebrew/Cellar/go/1.20.2/libexec/src/runtime/proc.go:293 +0x24

Here is the example file i used to test: S_and_D-Spring_2008.pdf

gen2brain commented 1 year ago

I cannot reproduce crash with your example file and with the code I pasted above, that same code crashes for you or what? You are mentioning goroutines, you are actually trying to use goroutines for Image() or not? That is like the one and only open issue and that is not supported. Please, I don't want to pull the info from you anymore, I don't have time for that, send an example that will crash. If you are trying to use goroutines then the issue is invalid, because that is not supported.

bao-huynh-ostv commented 1 year ago

I used the code that you sent me above, not modify anything except the path. Can you try to reproduce on apple m1 device?

gen2brain commented 1 year ago

No, I don't have or use apple devices. If you are having problems with bundled library try with the external library.

bao-huynh-ostv commented 1 year ago

thank you for your support, i will close the issue.