Stop the player freeing a render context libmpv still uses

The segfault: Qt destroys and recreates a widget's QOpenGLContext whenever it
moves to another top-level window, then calls initializeGL() again. libmpv
permits one render context per handle, so the second creation failed with
"There is already a mpv_render_context set" -- and the except branch assigned
self._render_ctx = None, dropping the last Python reference to the first
context, which libmpv was still holding a function pointer into. python-mpv's
MpvRenderContext has no __del__ and free() does not unregister the callback,
so the ctypes trampoline was collected while registered and the next frame
notification jumped into freed memory.

Two invariants fix it. initializeGL() now tears down any existing render
context first, so a second call is an ordinary recreation. Teardown clears
update_cb, calls free() with the GL context current, and only then drops the
reference -- and it is connected to QOpenGLContext.aboutToBeDestroyed, so it
runs before the GL context dies instead of never. The local reference during
teardown is load-bearing: it is what keeps the trampoline alive until free()
returns.

Three things were destroying that context. Fullscreen reparented the panel
into a new top-level window (twice per toggle) and put it back at the end of
the splitter, losing the pane layout; it now fullscreens the main window and
hides the chrome, reparenting nothing. The tab cross-fade and the dialog blur
both grab() the widget tree, which on an OpenGL surface forces a framebuffer
readback and returns black -- the fade is skipped for pages holding the video,
and dialogs dim rather than blur.

Verified on a real Wayland GL context: ten forced context destroy/recreate
cycles re-establish the render context every time, and the full app survives
tab switching, six fullscreen toggles and resizes with no render-context
error and a clean exit. Before this, the same startup dumped core.

Also here because they are one-line consequences of touching _create_mpv: an
explicit per-platform hwdec list ending in software decoding, and the restored
volume actually reaching mpv -- the slider set its value before connecting its
signal, so playback always started at 100.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-09 00:48:18 +02:00
parent 2420091a4e
commit 5ae2817eea
8 changed files with 517 additions and 69 deletions
+34
View File
@@ -10,8 +10,42 @@ records.
## Unreleased
### Fixed
- **The player no longer crashes the app.** Qt destroys and recreates a
widget's OpenGL context whenever it moves to another top-level window, and
calls `initializeGL()` again. libmpv allows one render context per handle, so
the second creation failed — and the failure path dropped the last reference
to the *first* context while libmpv still held a function pointer into it.
Python then collected the callback trampoline under libmpv's feet and the
next frame notification jumped into freed memory. `initializeGL()` now tears
down first so recreation is clean, teardown clears the callback and frees
with the GL context current, and it is wired to
`QOpenGLContext.aboutToBeDestroyed` so it runs before the context goes away
rather than never. Verified over ten forced context destroy/recreate cycles.
- **Fullscreen no longer reparents the video.** It took the player out of its
layout and into a new top-level window, destroying the OpenGL context twice
per toggle — the most direct route to the crash above — and on the way back
re-added the panel at the end of the splitter, so the video reappeared beside
the queue with the pane sizes lost. The main window now goes fullscreen and
the chrome around the video is hidden instead. Nothing is reparented.
- **Switching tabs no longer risks the player.** The cross-fade grabbed a
screenshot of the outgoing page; on an OpenGL surface that forces a
framebuffer readback and returns black, so the "fade" was a black slab and
the readback endangered the context. Pages containing the video now switch
without the fade.
- Dialogs dimmed instead of blurred while the player exists, for the same
reason — every dialog in the app screenshotted the whole window.
- The saved volume never reached mpv: the slider set its restored value before
its change signal was connected, so playback always started at 100.
### Changed
- mpv is given an explicit `hwdec` list per platform (`player.hwdec`, default
`auto`), each ending in software decoding, so a machine with broken GPU
interop plays rather than showing a black frame. This does **not** silence
`Cannot load libcuda.so.1` — that comes from the driver stack below mpv and
appears with `hwdec=no` too.
- **The update check now looks at SageTube's own releases.** It queried PyPI's
`ytsage` package for the version and `oop7/YTSage` for the changelog, then
linked to upstream's downloads — a different program's release stream. It now