Comments (4)
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.
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.
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.
Merged, thanks.
from go-fitz.
Related Issues (20)
- getting crash for some pdf format HOT 1
- start program HOT 2
- undefined: fitz.New HOT 3
- fitz is not importing correctly HOT 2
- centos alpine build faild HOT 5
- Linux platform, when PDF is invalid
- Error when convert PDF to Image HOT 3
- outline.page.page undefined (type _Ctype_int has no field or method page) compiler (MissingFieldOrMethod) HOT 6
- libmupdf_linux_amd64.a error HOT 8
- Implement Text Extraction in PyMuPdf Fitz Layout Mode HOT 4
- how to build mupdf static library in window platform HOT 10
- cannot find builtin CJK font HOT 1
- doc.HTML() can it support <table> <tr> <td>? HOT 1
- linux compilation error HOT 3
- linux ubuntu build fail HOT 5
- Makes discord/lilliput segfault HOT 2
- fitz is empty HOT 2
- How to build docker with this package?
- GCC 12.2 Support HOT 10
- Is it possible to add PDF splitting and merging functions? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-fitz.