Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation Fault when inserting three (but not two) copies of the same source page into one destination page #2742

Closed
paternal opened this issue Oct 14, 2023 · 9 comments

Comments

@paternal
Copy link

paternal commented Oct 14, 2023

Describe the bug (mandatory)

When copying the same page three times into one destination page, a segmentation fault happens.

To Reproduce (mandatory)

Download dummy.pdf, and run this script:

import fitz

dest = fitz.open()
destpage = dest.new_page(width=842, height=595)

a5 = fitz.Rect(0, 0, destpage.rect.width / 3, destpage.rect.height)
shiftright = fitz.Rect(destpage.rect.width/3, 0, destpage.rect.width/3, 0)

src = fitz.open("dummy.pdf")

destpage.show_pdf_page(a5, src, 0)
destpage.show_pdf_page(a5 + shiftright, src, 0)
destpage.show_pdf_page(a5 + shiftright + shiftright, src, 0)

dest.save("dummy-nup.pdf")
print("The end!")

Expected behavior (optional)

The scripts runs without errors, and produces a file dummy-nup.pdf with three copies of the only page of dummy.pdf on its first page.

Screenshots (optional)

(312) louis@oryx:~/projets/pdfautonup/src$ python nup.py
The end!
Segmentation fault

Your configuration (mandatory)

  • Operating system, potentially version and bitness: Debian trixie.
  • Python version, bitness: Python 3.12.0 (installed from the Debian repositories)
  • PyMuPDF version, installation method (wheel or generated from source) : 1.23.5, installed from wheel (I guess?)

For example, the output of print(sys.version, "\n", sys.platform, "\n", fitz.__doc__) would be sufficient (for the first two bullets).

$ pip install pymupdf
Collecting pymupdf
  Obtaining dependency information for pymupdf from https://files.pythonhosted.org/packages/1c/9b/ad383fe1949b5925b903ff57bd915dd6e3479c8229f16942f22324e393d4/PyMuPDF-1.23.5-cp312-none-manylinux2014_x86_64.whl.metadata
  Using cached PyMuPDF-1.23.5-cp312-none-manylinux2014_x86_64.whl.metadata (3.4 kB)
Requirement already satisfied: PyMuPDFb==1.23.5 in /home/louis/.virtualenvs/312/lib/python3.12/site-packages (from pymupdf) (1.23.5)
Using cached PyMuPDF-1.23.5-cp312-none-manylinux2014_x86_64.whl (4.3 MB)
Installing collected packages: pymupdf
Successfully installed pymupdf-1.23.5
$ python -c 'import sys, fitz; print(sys.version, "\n", sys.platform, "\n", fitz.__doc__)'
3.12.0 (main, Oct  4 2023, 06:27:34) [GCC 13.2.0] 
 linux 
 
PyMuPDF 1.23.5: Python bindings for the MuPDF 1.23.4 library.
Version date: 2023-10-11 00:00:01.
Built for Python 3.12 on linux (64-bit).

Additional context (optional)

A few (weird?) things:

  • The script ends with a segmentation fault at the end of it: the print() called at the last line is executed: the segmentation fault happens after it.
  • The segmentation fault disappears if the page is copied twice instead of thrice (if line 13 is removed).
  • The script works: output file is generated as expected, in spite of the segmentation fault.
  • The script works (without segmentation fault) with the very same pymupdf version and python3.11.
  • The segmentation fault happens even if the destination file is not written (i.e. if line dest.save(…) is removed).
  • fitz.__doc__ reads: PyMuPDF 1.23.5: Python bindings for the MuPDF 1.23.4 library.: Could the version mismatch here explain the problem?

Thanks for your work!
Louis

@JorjMcKie JorjMcKie added the bug label Oct 14, 2023
@JorjMcKie
Copy link
Collaborator

Confirmed - thanks for your report.
Interestingly, if you use the new rebased version of PyMuPDF, the problem will not occur: import fitz_new as fitz.

@paternal
Copy link
Author

Interestingly, if you use the new rebased version of PyMuPDF, the problem will not occur: import fitz_new as fitz.

With a warning (or at least, some clutter in the output):

$ python nup.py
platform/c++/implementation/internal.cpp:205:reinit_singlethreaded(): Reinitialising as single-threaded.

@julian-smith-artifex-com
Copy link
Collaborator

The "... Reinitialising as single-threaded." text is a diagnostic rather than a warning. Unfortunately there's no way to disable it.

I'll disable the diagnostic (it's actually in MuPDF, not PyMuPDF) in a future release.

@JorjMcKie
Copy link
Collaborator

We are going to lower the priority of resolving this, because the new alternative implementation of PyMuPDF does not have this problem.
Therefore, as an immediate solution, change the import statement in affected scripts to ìmport fitz_new as fitz`.

@julian-smith-artifex-com
Copy link
Collaborator

Note that #2775 is fixed in the current release, 1.23.6 (see #2786), so import fitz_new as fitz is a good workaround for this issue, until we move to using the rebased implementation by default.

@julian-smith-artifex-com
Copy link
Collaborator

Fixed in 1.23.9 where import fitz gets the rebased implementation.

@paris-ci
Copy link

paris-ci commented Feb 2, 2024

Fixed in 1.23.9 where import fitz gets the rebased implementation.

When updating from 1.23.8 to 1.23.9 or any version above, I get a segfault when python exits, if I import the module.

Using import fitz_old as fitz instead fixes the problem.

@julian-smith-artifex-com
Copy link
Collaborator

Fixed in 1.23.9 where import fitz gets the rebased implementation.

When updating from 1.23.8 to 1.23.9 or any version above, I get a segfault when python exits, if I import the module.

Using import fitz_old as fitz instead fixes the problem.

PyMuPDF-1.23.21 works fine for me with the above test, on Linux (Python-3.11) and Windows (Python-3.12).

Please send complete information about the failure you're seeing - the OS, OS version, Python version, PyMuPDF version.

Also include the input file if it is different from above, and your code if different from above.

julian-smith-artifex-com added a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Feb 2, 2024
@paris-ci
Copy link

paris-ci commented Feb 2, 2024

Fixed in 1.23.9 where import fitz gets the rebased implementation.

When updating from 1.23.8 to 1.23.9 or any version above, I get a segfault when python exits, if I import the module.
Using import fitz_old as fitz instead fixes the problem.

PyMuPDF-1.23.21 works fine for me with the above test, on Linux (Python-3.11) and Windows (Python-3.12).

Please send complete information about the failure you're seeing - the OS, OS version, Python version, PyMuPDF version.

macOS 14.2.1 (ARM64/M1), Python 3.11.7, pymupdf==1.23.21.

Also include the input file if it is different from above, and your code if different from above.

There is no input file. It's a fairly complex Django project, and even when a simple command is ran (say python manage.py check that doesn't process any PDFs whatsoever, but imports every app and module), python will segfault when exiting.

Attached is the segfault report if that can help.

translated.txt

Python-2024-02-02-184646.ips.txt

I'm trying to build a smaller reproduction case, but having trouble. Running a Django command in this project will consistently segfault, however.

julian-smith-artifex-com added a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Feb 6, 2024
…age().

This test appears to pass fine on rebased.
julian-smith-artifex-com added a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Feb 10, 2024
…age().

This test appears to pass fine on rebased.
julian-smith-artifex-com added a commit that referenced this issue Feb 13, 2024
This test appears to pass fine on rebased.
julian-smith-artifex-com added a commit that referenced this issue Feb 14, 2024
This test appears to pass fine on rebased.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants