Git Product home page Git Product logo

Comments (4)

rntrp avatar rntrp commented on July 4, 2024

Please tell me if you can reproduce the issue. Test PDF file: test.pdf.

package main

import (
	_ "embed"
	"sync"

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

//go:embed test.pdf
var pdf []byte

var routines = 10_000

func main() {
	wg := new(sync.WaitGroup)
	wg.Add(routines)
	for i := 0; i < routines; i++ {
		go func(r int, g *sync.WaitGroup) {
			defer g.Done()
			numPage(r)
		}(i, wg)
	}
	wg.Wait()
	println("Done")
}

func numPage(routine int) {
	// Simulate concurrent requests by deep copying the bytes.
	// The original slice is a package level variable, hence
	// it won't be GC'ed. But a function level copy of this
	// slice might be collected very early.
	c := make([]byte, len(pdf))
	copy(c, pdf)

	doc, _ := fitz.NewFromMemory(c)
	// From now on doc saves a raw memory range of bytes.
	// There is no other Go reference of c. Apparently, this
	// causes the GC to trigger its eviction.

	defer doc.Close()
	num := doc.NumPage()

	if num != 3 {
		println("Routine:", routine, "| Num pages:", num)
		// If we assume that the error is caused by the GC
		// evicting our byte slice copy too eagerly, then we
		// need to somehow force the byte slice to remain in
		// memory.

		// Uncomment the following line for the PoC fix:
		// println(c)

		// If it's indeed rectifying the issue, then the
		// above println must never print anything, since
		// doc.NumPage() will now always provide correct
		// results, i.e. 3.
	}
}

from go-fitz.

gen2brain avatar gen2brain commented on July 4, 2024

Hi, yes, I can reproduce the issue. If I uncomment println line it finishes. Also works if I increase GOGC, or if I disable GC. Not sure I understand what you mean by "original byte slice", can you send PR?

from go-fitz.

rntrp avatar rntrp commented on July 4, 2024

Created #56 but not really tested it. The idea is to retain the []byte within the Document struct until it's closed.

from go-fitz.

gen2brain avatar gen2brain commented on July 4, 2024

Merged, thanks.

from go-fitz.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.