Repository navigation
Conversation
PatTheMav
left a comment
There was a problem hiding this comment.
I don't see a need for the frontend changes to be split into 2 separate commits, particularly as the language in the commit messages and the code comments makes references to the "same" hazard.
Indeed the third commit also uses two different ways to solve the issue:
- In
Multiview.cppa guard statement is used to check iflibobsis still initialised and bails out early. - In
ScreenshotObjthe functionality is wrapped in a conditional block doing the opposite check.
It would be more consistent if we'd use the same code pattern as in Multiview.cpp and OBSBasicPreview.
@Warchamp7 wouldn't it be more correct if the tick callback in ScreenshotObj is removed unconditionally, e.g. before a if(!obs_initialized) check?
There's IMO also no need to add (duplicate) multiline comments explaining the change in the source code itself - as long as the commit messages contain the motivation we should be fine.
|
If I'm not mistaken there is nothing that would in theory prevent So with the changes of the first commit (which is indeed correct - one needs to ensure that the object on which to check a pointer can be dereferenced itself) Once shutdown has completed So in the case of @RytoEX I'll leave it to you if that added check is "nice enough" as it currently avoids the log messages if only because both the destructors as well as A better change (that'd also pay back some of the tech debt) would be for if (obs_enter_graphics()) {
gs_vertexbuffer_destroy(actionSafeMargin);
gs_vertexbuffer_destroy(graphicsSafeMargin);
gs_vertexbuffer_destroy(fourByThreeSafeMargin);
gs_vertexbuffer_destroy(leftLine);
gs_vertexbuffer_destroy(topLine);
gs_vertexbuffer_destroy(rightLine);
obs_leave_graphics();
}The That might be an ABI break though and would thus probably require a new method like |
|
First commit in this PR fixes crash. The second commit was defensive. I can squash the commits. Or split them into seperate PRs. what's the best way to get these fixes in? I'm currently building my own obs to get around it. |
|
Large portions of this PR/discussion appear to be AI generated. Can you please confirm that the PR content, comments, and code were all human written per our policy? |
|
Applied the requested changes |
| if (!obs_initialized()) { | ||
| return; | ||
| } | ||
|
|
There was a problem hiding this comment.
This leads to obs_remove_tick_callback never being called, is that intentional?
There was a problem hiding this comment.
this is provably overly defensive
There was a problem hiding this comment.
this is provably overly defensive
I don't think it is, I had time to look into this and that function unconditionally dereferences obs->data when attempting to lock a mutex.
So if obs is a nullptr already (as indicated by obs_initialized returning false), it would lead to a crash.
If the problem is limited to the order in which destructors are called on the main thread (and obs_shutdown is only ever(!) called from the main thread) then this solutions's TOCTOU issue still exists, but is without effect as nothing can uninitialise libobs between the check and the consecutive access.
Have you checked all call sites of obs_shutdown?
There was a problem hiding this comment.
You were right, it isn't overly defensive. obs_remove_tick_callback dereferences obs->data, so calling it before the check could crash after shutdown. I moved it after the obs_initialized() guard, so it matches Multiview and OBSBasicPreview.
I checked all the call sites of obs_shutdown:
libobs/obs.c: inobs_startup(), whenobs_initfails. The frontend objects don't exist yet at that point.frontend/OBSApp.cpp: inOBSApp::~OBSApp, behindlibobs_initialized, on the main thread.test/win/test.cpp: a standalone test, not the frontend.
So the frontend only shuts down on the main thread, which means the TOCTOU window can't be hit from these destructors. I agree the proper fix is an obs_enter_graphics variant that reports success. I'd be happy to do that as a follow-up.
obs_shutdown() frees the global obs_core and sets `obs` to NULL, but obs_enter_graphics()/obs_leave_graphics() only checked obs->video.graphics, not obs itself. A caller that runs after shutdown dereferences the freed pointer. OBSBasicPreview, Multiview and ScreenshotObj release graphics resources in their destructors via obs_enter_graphics(). These destructors can run after obs_shutdown() (via a Qt DeferredDelete processed late in shutdown), at which point the graphics subsystem is already gone and entering graphics crashes. Return early when libobs is no longer initialized. ScreenshotObj removes its tick callback after that check, since obs_remove_tick_callback() also dereferences obs. After shutdown the tick callback list has already been freed, so nothing is left to remove.
|
Comments addressed |
|
Can you please reply to my previous comment as well? |
|
Closing as no response, and there are some pretty clear markers this is AI-generated. |


Reproduced an OBS crash in NixOS, this PR fixes it.
Description
OBSBasicPreview's destructor releases its GL/Vulkan resources by callingobs_enter_graphics()unconditionally. If that destructor runs afterobs_shutdown()has already freed the globalobs_coreand setobstoNULL,obs_enter_graphics()dereferences the freed pointer and crashes.This lands as a single commit (squashed per review) with three parts:
obs_enter_graphics()/obs_leave_graphics()only checkedobs->video.graphics, neverobsitself. Add the missing null check.OBSBasicPreview::~OBSBasicPreview()skips its wholegraphics-resource cleanup via the existing
obs_initialized()whenlibobs has already torn down the graphics subsystem those resources
belong to (there's nothing meaningful left to release at that point).
by auditing every
obs_enter_graphics()call site infrontend/forthe same exposure (see "Related call sites audited" below):
Multiview::~Multiview()andScreenshotObj::~ScreenshotObj().ScreenshotObjalso callsobs_remove_tick_callback(), whichdereferences
obsdirectly with no null check of its own, so it iscalled after the guard. After shutdown the tick callback list has
already been freed, so skipping it loses nothing.
Motivation and Context
Fixes a shutdown-order race between
OBSBasic's deferred deletion and theaboutToQuit-triggered call intoobs_shutdown()/applicationShutdown().This is the same root cause reported in #13248 ("OBS Studio 32.1.0 crashes
after shutdown") and in @notr1ch's comment there describing an identical
OBSBasicPreview/obs_enter_graphicscrash on 32.1.2. #13347 closed thespecific
aboutToQuitpath that was racing on that report by removing theconnection, and that is a real fix for the instance it targets. This PR
instead hardens the lower-level entry points at fault, so any caller that
manages to run graphics teardown code after
obs_shutdown()(now, or ina future regression) fails safe instead of dereferencing freed memory.
Reproduction
Built
master's parent commit of this branch (i.e. without this fix)locally via Nix (
nixpkgs'sobs-studioderivation,srcoverridden toa local checkout -- a full source build, not a substituted binary), then,
with a fresh
XDG_CONFIG_HOME(so the first-run/auto-config path isactive) ran:
This reproduced the crash in 6 of 8 attempts.
coredumpctl/gdbagainst the resulting core, with full local debug info, gives:
The fault address is
0x148in every capture (reproduced independentlytwice). That is exactly
offsetof(struct obs_core, video.graphics):i.e.
obsisNULL(set byobs_shutdown(),libobs/obs.c) andobs_enter_graphics()readsobs->video.graphicsanyway.Related call sites audited
obs_enter_graphics()has 152 call sites project-wide; almost all of themrun only during normal (obs-alive) operation. I went through every one
under
frontend/specifically, since that's where shutdown-adjacent codelives:
OBSBasicPreview::~OBSBasicPreview()Multiview::~Multiview()OBSProjector, a Qt-parented top-level window that can outliveapplicationShutdown()'s explicit cleanup list the same wayOBSBasicPreviewdoesScreenshotObj::~ScreenshotObj()muxFile()on a backgroundstd::thread, finishing via aQt::QueuedConnectionintoonFinished()->deleteLater()) can plausibly land afterobs_shutdown()if a screenshot is still in flight at quitOBSBasic::applicationShutdown()(OBSBasic.cpp:1477)obs_shutdown()is called, in the same synchronous function (see the/* XXX: any obs data must be released before calling obs_shutdown */comment a few lines above it) --obsis guaranteed non-null here by construction, not part of the raceOBSProjector::UpdateMultiviewProjectors(),InitSafeAreas(),OBSBasicSourceSelect::AddExisting(),system-info-posix.cppGPU queryI did not extend this to the
Multiview::~Multiview()loop that callsobs_source_dec_showing()on weak-referenced sources above the graphicscleanup -- that already goes through
OBSGetStrongRef(), which returnsnull once the underlying source is gone, so it's safe by a different,
pre-existing mechanism and out of scope for this fix.
Why the fix fixes it
With all three parts in place, the same repro (fresh config,
SIGTERMat 3s) was run 39 times total across three test rounds against builds
containing progressively more of this branch: zero crashes, and the
process exits cleanly within a few seconds every time (even racing a
from-scratch first-run wizard, which is the worst case observed). The
obs_initialized()checks mean the crashing lines are never reached oncelibobs is down; the
obs &&check inobs_enter_graphics()/obs_leave_graphics()is a second, independent line of defense at thelayer that actually dereferences the pointer, protecting any other caller
that hits the same shutdown ordering, not just these three call sites.
How Has This Been Tested?
source with Nix (
nixpkgs'sobs-studioderivation,srcoverriddento a local checkout of each state), so all are genuine local source
builds, not substituted binaries.
SIGTERMcycles (fresh config each time)produced the
SIGSEGVabove.SIGTERMcycles produced no crash.SIGTERMcycles,each with a completely fresh config directory (forcing the auto-config
wizard every time), produced no crash and a clean exit every time.
obs-websocket, normal window close, multiview/projector windows,screenshots) -- all three guards are no-ops on the happy path where
obsis still alive.Types of changes
Checklist: