gen2brain / go-fitz

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

Create Root Context in mupdf #97

Closed charlesoconor closed 6 months ago

charlesoconor commented 7 months ago

They recommend creating a root context for applications running concurrently. This should allow for caching fonts and layers across runs. I was also dealing with segfaults out of mupdf when creating images in a threaded server since there were some shared objects the library creates that weren't safe to access.

Problems with this solution:

I was looking to see if it makes sense to clean up and upstream or if the problems seem to big. Could update the build to link in the concurrency only on Unix system.

Repo to get this to happen a lot of the time with documents that share things across pages. Unfortunately I can't share the pdf we found that did this consistently.

package main

import (
    "context"
    "flag"
    "log"
    "os"
    "sync"

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

func main() {
    ctx := context.Background()
    flag.Parse()
    args := flag.Args()
    if len(args) != 1 {
        log.Fatal(flag.ErrHelp.Error())
    }

    bs, err := os.ReadFile(args[0])
    if err != nil {
        panic(err)
    }

    doc, err := fitz.NewFromMemory(bs)
    if err != nil {
        panic(err)
    }
    defer doc.Close()

    n := doc.NumPage()
    wg := sync.WaitGroup{}
    wg.Add(n)

    for i := 0; i < n; i++ {
        go func(idx int, wg *sync.WaitGroup) {
            defer wg.Done()
            doc, err := fitz.NewFromMemory(bs)
            if err != nil {
                panic(err)
            }
            defer doc.Close()

            _, err = doc.ImageDPI(idx, 300)
            if err != nil {
                panic(err)
            }
            log.Printf("page %d\n", idx+1)
        }(i, &wg)
    }
    wg.Wait()
}
gen2brain commented 7 months ago

Sorry, without a reproducible example, I don't even know what this is supposed to fix if it works, and what it does. It adds a lot of complexity, and for now, without known reason. My impression was that anything MuPDF thread-related is only for displaying PDFs.

charlesoconor commented 6 months ago

Understood. I'll try to find something I can share and report back but closing for now.