Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(wgc): register onFrameArrived's in-flight guard before touching the frame pool

abduznikecf4589c2ce4
diff --git a/electron/native/wgc-capture/src/wgc_session.cpp b/electron/native/wgc-capture/src/wgc_session.cpp
index ecc49303c..53928963f 100644
--- a/electron/native/wgc-capture/src/wgc_session.cpp
+++ b/electron/native/wgc-capture/src/wgc_session.cpp
@@ -325,28 +325,12 @@ void WgcSession::setFrameCallback(FrameCallback callback) {
void WgcSession::onFrameArrived(
wgcap::Direct3D11CaptureFramePool const& sender,
wf::IInspectable const&) {
- auto frame = sender.TryGetNextFrame();
- if (!frame) {
- return;
- }
-
- auto surface = frame.Surface();
- auto access = surface.as<::Windows::Graphics::DirectX::Direct3D11::IDirect3DDxgiInterfaceAccess>();
- Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
- HRESULT hr = access->GetInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(texture.GetAddressOf()));
- if (FAILED(hr) || !texture) {
- return;
- }
-
// Scoped rather than a bare decrement at the end, for two reasons: a
// callback that left by exception would otherwise strand
// quiesceLegacyCallback()'s drain forever, and the guard has to outlive
- // frame.Close() -- dropping the count first would let quiesce return and
- // close the frame pool while this handler is still closing a frame that
- // pool owns. Counted unconditionally (not only when callback is
- // non-null): the no-callback path still calls frame.Close() below, and
- // that call needs to be covered by the drain too, or quiesceLegacyCallback()
- // could return while this handler is still inside it.
+ // every pool-owned object this handler touches -- dropping the count
+ // first would let quiesce return and close the frame pool while this
+ // handler still holds a reference into it.
struct InFlightGuard {
std::atomic<int>& counter;
~InFlightGuard() {
@@ -354,17 +338,42 @@ void WgcSession::onFrameArrived(
}
};
+ // Captured and counted before TryGetNextFrame(), not after: this handler
+ // starts touching the pool (TryGetNextFrame, Surface(), GetInterface())
+ // immediately below, and none of that is safe to run concurrently with
+ // framePool_.Close(). Counting only after those calls succeeded left a
+ // window where quiesceLegacyCallback() could see callbacksInFlight_ == 0
+ // and return while this handler was still mid-frame -- registering the
+ // guard first, before anything pool-related, closes that window instead
+ // of narrowing it.
FrameCallback callback;
{
std::scoped_lock lock(callbackMutex_);
callback = frameCallback_;
// Counted under the same lock quiesceLegacyCallback() clears the
// callback under, so once it has cleared it no new handler can start
- // and the counter it then drains cannot go back up.
+ // and the counter it then drains cannot go back up. Counted
+ // unconditionally (not only when callback is non-null): a handler
+ // that observes a cleared callback still touches the frame pool
+ // below and needs to be covered by the drain too.
callbacksInFlight_ += 1;
}
InFlightGuard guard{callbacksInFlight_};
+ auto frame = sender.TryGetNextFrame();
+ if (!frame) {
+ return;
+ }
+
+ auto surface = frame.Surface();
+ auto access = surface.as<::Windows::Graphics::DirectX::Direct3D11::IDirect3DDxgiInterfaceAccess>();
+ Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
+ HRESULT hr = access->GetInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(texture.GetAddressOf()));
+ if (FAILED(hr) || !texture) {
+ frame.Close();
+ return;
+ }
+
if (callback) {
callback(texture.Get(), timeSpanToHns(frame.SystemRelativeTime()));
}