Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Exception ignored in tp_clear of: <class 'memoryview'> #110408

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

Open
wg-postalgia opened this issue Oct 5, 2023 · 16 comments
Open

Exception ignored in tp_clear of: <class 'memoryview'> #110408

wg-postalgia opened this issue Oct 5, 2023 · 16 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@wg-postalgia
Copy link

wg-postalgia commented Oct 5, 2023

Bug report

Bug description:

Searching this error the only reference I find was a previous conversation where this issue was considered theoretical and not reproducible.

I made a small script which can reproduce the error.
It's really frustrating to do, because the error isn't caught by the debugger, so it's really hard to figure out what was triggering the error, since the Traceback is seemingly random. I slowly pruned my code away to leave this minimal case.

The main code iterates through a PDF file, implemented as an iterator that returns the next page of the PDF (via poppler) as an image at a given DPI. For each page, a Future is created with ProcessPoolExecutor which delays and returns.

Both the iterator through poppler AND the futures are required.

I have solved the problem in my code by removing the iterator, but spent the time to create this reproduction in case it helps someone track down this bug.

Prerequisite: https://pdf2image.readthedocs.io/en/latest/installation.html

import functools
import threading
import time
from pdf2image import convert_from_path, pdfinfo_from_path
from concurrent.futures import ProcessPoolExecutor


class Pdf2ImageIterator:

    def __init__(self, pdf_path: str, poppler_path: str, dpi_high: int):
        self._path = pdf_path
        self._dpi_high = dpi_high
        self._poppler_path = poppler_path
        self.count = pdfinfo_from_path(self._path, None, None, poppler_path=self._poppler_path)["Pages"]

    def __iter__(self):
        self._page = 1
        return self

    def __next__(self):
        try:
            page_h = convert_from_path(self._path, poppler_path=self._poppler_path, dpi=self._dpi_high,
                                        first_page=self._page, last_page=self._page)
            self._page += 1
            return page_h
        except Exception as e:
            print(f"Pdf2Images {e}")


def my_done_callback(i, image, future):
    print(f"my_done_callback on {threading.get_ident()}")


def my_future(i, image):
    print(f"my_future on {threading.get_ident()}")
    time.sleep(1)
    return 0


if __name__ == '__main__':
    ppe = ProcessPoolExecutor(None)

    poppler_path = r"C:\U<YOUR-PATH>\poppler-23.01.0\Library\bin"
    pdf = r"<PDF file with multiple pages>"
    pages = Pdf2ImageIterator(pdf, poppler_path, 200)

    for i, im in enumerate(pages):
        future = ppe.submit(my_future, i, im)
        future.add_done_callback(functools.partial(my_done_callback, i, im))

    print("Done")

Example console logs:

Exception ignored in tp_clear of: <class 'memoryview'>
Traceback (most recent call last):
  File "...\plugins\python-ce\helpers\pydev\pydevd_tracing.py", line 56, in _internal_set_trace
    filename = frame.f_back.f_code.co_filename.lower()
BufferError: memoryview has 1 exported buffer

Exception ignored in tp_clear of: <class 'memoryview'>
Traceback (most recent call last):
  File "...Programs\Python\Python310\lib\threading.py", line 568, in set
    with self._cond:
BufferError: memoryview has 1 exported buffer

Exception ignored in tp_clear of: <class 'memoryview'>
Traceback (most recent call last):
  File "...\plugins\python-ce\helpers\pydev\pydevd_tracing.py", line 56, in _internal_set_trace
    filename = frame.f_back.f_code.co_filename.lower()
BufferError: memoryview has 1 exported buffer

Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32

CPython versions tested on:

3.10

Operating systems tested on:

Windows

@wg-postalgia wg-postalgia added the type-bug An unexpected behavior, bug, or error label Oct 5, 2023
@gaogaotiantian
Copy link
Member

Are you able to reproduce this without the debugger (using pure python script in cmdline like python yourscript.py)? I'm assuming you are using PyCharm or some other IDEs because the exception was raised from pydev.

@wg-postalgia
Copy link
Author

I am using PyCharm and cannot reproduce without the debugger.
Does that mean it's a debugger bug? I'm not sure where to re-make this report to, any suggestions?
Thank you

@gaogaotiantian
Copy link
Member

I believe this is a dup of #77894. I think this is still a CPython issue, the debugger should not be able to do anything to crash Python. However, without a simple repro that involves "only CPython", it's not easy to track down the issue. It requires plenty of work here to understand the problem.

@wg-postalgia
Copy link
Author

Agreed, yes that's the original conversation I referenced - sorry I didn't have the correct migrated link.
Happy to close this as duplicate and leave for anyone who feels like investigating this in the future?

It's kind of fun to reproduce a bug that was only theoretical - at least now that I have a fix anyways :)

@cyc1111111111
Copy link

同意,是的,这就是我引用的原始对话 - 抱歉,我没有正确的迁移链接。 很高兴将其作为重复项关闭并留给任何想要将来调查此问题的人吗?

重现一个理论上的错误是很有趣的 - 至少现在我已经修复了:)

Have you solved this problem? I had the same problem

@wg-postalgia
Copy link
Author

wg-postalgia commented Oct 24, 2023 via email

@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 28, 2023
@ForeverFerret
Copy link

ProcessPoolExecutor || multiprocessing.Pool

谢谢您! 您的回答对我同样有帮助!
经我测试, 我使用的Pycharm专业版2023.3.2版本会有这个问题, 我又接着尝试了2022.2.5和2022.1.4也都有同样的问题!
但使用VSCode调试则完全没有问题, 所以我怀疑是pycharm对dev做了特别的处理导致了问题

@dnparadice
Copy link

+1 this is annoying, debugger should handle multiprocessing more elegantly

@tobiaswuerth
Copy link

+1

@Andre1122
Copy link

I recently replaced my computer and switched the CPU from Core to AMD The same code encountered this issue. Setting CPU to 1 can solve the problem. I want to know if this error is caused by AMD CPUs I am considering whether to replace the computer again.

@wg-postalgia
Copy link
Author

No it happens on my "11th Gen Intel(R) Core(TM) i5-1135G7 " so not AMD only.

@devdanzin
Copy link
Contributor

devdanzin commented Mar 10, 2025

Can you reproduce the issue in main? Seems related to #127085, so it might have been fixed recently.

Also maybe #77894.

@tkohegyi
Copy link

I face with Exception ignored in tp_clear of: <class 'memoryview'> error many times when I run threads in python code and try to debug it:

Exception ignored in tp_clear of: <class 'memoryview'>
Traceback (most recent call last):
File "", line 1, in
BufferError: memoryview has 1 exported buffer

Process finished with exit code -1073741819 (0xC0000005)

This actually means debug is not possible.

@dnparadice
Copy link

This issue is resolved in Python 3.12

@tkohegyi
Copy link

Sorry @dnparadice, but I experienced this with Python 3.12.

@dnparadice
Copy link

Sorry @dnparadice, but I experienced this with Python 3.12.

That is too bad, I used to see this exception regularly and have not seen it since updating to Python 3.12.
Maybe post the code that causes the exception here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

10 participants