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

Crash during long running proccess in asyncio.create_subprocess_shell on Windows #62

Open
esmorun opened this issue Dec 5, 2021 · 5 comments
Labels
bug Something isn't working need repro Needs minimal, reproducible example

Comments

@esmorun
Copy link

esmorun commented Dec 5, 2021

Hi, I'm executing ffmpeg in an asyncio subprocess to convert a video. Example:

command = "ffmpeg  -i \"video.mp4\" -c:v hevc_nvenc -rc constqp -qp 27 -c:a libopus -b:a 96k \"video.mkv\""
proc = await asyncio.create_subprocess_shell(command, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)

Long running processes will eventually crash the entire PyQt GUI. Sometimes it just crashes to desktop without any output in the Python shell, and sometimes this error message appears:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\qasync\_windows.py", line 163, in run
    events = self.__proactor.select(0.01)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\qasync\_windows.py", line 69, in select
    self._poll(timeout)
  File "C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\qasync\_windows.py", line 99, in _poll
    with QtCore.QMutexLocker(self._lock):
RuntimeError: wrapped C/C++ object of type QMutex has been delet

The ffmpeg process continues as an orphaned process and finishes the job. Can anybody shed some light on what might be happening? I have tested the same code on Ubuntu and MacOS and have not encountered any issues.

I'm on PyQt 6.2.2 and Python 3.9.9.

Edit: I now understand that the error is happening because the QMutex object that is created on line 64 in windows.py is deleted before the loop at line 100 in the _poll function finishes. Just for fun I tried to remove the QMutexLocker by removing line 99:
with QtCore.QMutexLocker(self._lock):

...and this actually prevents my app from crashing, but I assume this is not a solution and the lock was there for a reason.

@awoimbee
Copy link

awoimbee commented Apr 15, 2022

I have the same issue with a long running Future. I solved it by using threading.Lock() instead of QMutex. Not sure if this could have a negative impact.

Edit: I had a freeze using threading.Lock(), i switched to multiprocessing.Lock(), it works well.

@hosaka
Copy link
Collaborator

hosaka commented Apr 11, 2023

It is a common problem with Qt bindings for python, where a c++ object underneath gets deleted, and python then tries to use a reference to it. This needs a bit more digging and ideally a reproducible example. Perhaps a long sleep call, rather than an ffmpeg encode.

@hosaka hosaka added the bug Something isn't working label Apr 11, 2023
@dnadlinger
Copy link
Contributor

I'm also seeing this using (Py)Qt6 and Python 3.11 in https://github.com/dnadlinger/artiq/tree/qt6. No leads as to a reproducible example, unfortunately. The same application works just fine when running against (Py)Qt5. Backtrace is the same as above, though I've also seen nonsensical overload resolution errors in the QMutexLocker constructor (with the same stack trace, presumably due to memory corruption).

I am not very familiar with the internals of Python's GC and how PyQt integrates with it – any ideas what kind of information might be helpful in tracking this down further?

@hosaka hosaka added the need repro Needs minimal, reproducible example label Sep 27, 2023
@dnadlinger
Copy link
Contributor

dnadlinger commented Oct 2, 2023

@esmorun / @awoimbee: You might want to check whether #87 fixes the issue.

@hosaka
Copy link
Collaborator

hosaka commented Oct 2, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working need repro Needs minimal, reproducible example
Projects
None yet
Development

No branches or pull requests

4 participants