Skip to content

refactor: move electron::api::GlobalShortcut to cppgc - #50192

Merged
jkleinsc merged 11 commits into
mainfrom
refactor/electron-api-global-shortcut-cppgc
Apr 14, 2026
Merged

jkleinsc merged 11 commits into
mainfrom
refactor/electron-api-global-shortcut-cppgc

Conversation

@ckerr

@ckerr ckerr commented Mar 10, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Migrate electron::api::GlobalShortcut from gin_helper::DeprecatedWrappable to gin::Wrappable.

Part of #47922.

This is probably going to jinx CI, but... this is one of the easier refactors so far.

CC @deepak1556 @codebytere

Checklist

Release Notes

Notes: none.

@ckerr
ckerr requested a review from a team as a code owner March 10, 2026 19:27
@ckerr ckerr added semver/patch backwards-compatible bug fixes no-backport labels Mar 10, 2026
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Mar 10, 2026
@ckerr ckerr mentioned this pull request Mar 10, 2026
2 tasks done
@ckerr
ckerr marked this pull request as draft March 11, 2026 15:20
@ckerr

ckerr commented Mar 11, 2026

Copy link
Copy Markdown
Member Author

This is probably going to jinx CI, but... this is one of the easier refactors so far.

😠

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Mar 11, 2026
@ckerr
ckerr marked this pull request as ready for review March 11, 2026 22:56
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Mar 11, 2026
@deepak1556

deepak1556 commented Mar 12, 2026 •

Copy link
Copy Markdown
Member

I saw the crash in this PR before the recent change, it hints to me as a bug in V8. Logging the header in cppgc::internal::ConservativeTracingVisitor::TraceConservatively based on the crashed stack

#
# Fatal error in ../../v8/src/heap/cppgc/heap-page.h, line 277
# Debug check failed: page->is_large().
#
#
#
#FailureMessage Object: 0x16da23888
----- Native stack trace -----

 1: 0x11df681c8 node::NodePlatform::GetStackTracePrinter()::$_0::__invoke() [/Users/demohan/github/electron-oss/src/out/Testing/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 2: 0x1257acf64 V8_Fatal(char const*, int, char const*, ...) [/Users/demohan/github/electron-oss/src/out/Testing/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 3: 0x1257aca58 v8::base::SetFatalFunction(void (*)(char const*, int, char const*)) [/Users/demohan/github/electron-oss/src/out/Testing/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 4: 0x120a7aac8 cppgc::internal::ObjectView<(cppgc::internal::AccessMode)0>::Size() const [/Users/demohan/github/electron-oss/src/out/Testing/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 5: 0x120a7b184 cppgc::internal::ObjectView<(cppgc::internal::AccessMode)0>::ObjectView(cppgc::internal::HeapObjectHeader const&) [/Users/demohan/github/electron-oss/src/out/Testing/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
 6: 0x120a9c2d8 cppgc::internal::ConservativeTracingVisitor::TraceConservatively(cppgc::internal::HeapObjectHeader const&) [/Users/demohan/github/electron-oss/src/out/Testing/Electron.app/Contents/Frameworks/Electron Framework.framework/Versions/A/Electron Framework]
const bool header_is_large =
   header.IsLargeObject<AccessMode::kNonAtomic>();
 if (V8_LIKELY(header_is_large == page.is_large())) return;
 const char* object_name =
     header
         .GetName(HeapObjectNameForUnnamedObject::kUseClassNameIfSupported)
         .value;

 v8::base::OS::PrintError(
   "\n<--- cppgc header/page mismatch --->\n"
   "header_is_large=%d page_is_large=%d "
   "allocated_size=%zu gc_info_index=%u in_construction=%d marked=%d "
   "object_name=%s\n",
   header_is_large, page.is_large(),
   header.AllocatedSize<AccessMode::kAtomic>(),
   static_cast<unsigned>(header.GetGCInfoIndex<AccessMode::kAtomic>()),
   header.IsInConstruction<AccessMode::kAtomic>(),
   header.IsMarked<AccessMode::kAtomic>(), object_name);

Results in,

header_is_large=1 page_is_large=0 allocated_size=0 gc_info_index=1 in_construction=1 marked=1 object_name=Electron / GlobalShortcut

ConservativeTracingVisitor::TraceConservatively reports the object size as 0 also the value for kLargeObjectSizeInHeader https://source.chromium.org/chromium/chromium/src/+/main:v8/src/heap/cppgc/visitor.cc;l=49 but based on the high half of the header the object is still in construction https://source.chromium.org/chromium/chromium/src/+/main:v8/src/heap/cppgc/heap-object-header.h;l=192-201 so we could be reading stale object size during tracing leading to the header and page mismatch.

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Mar 12, 2026
@ckerr
ckerr force-pushed the refactor/electron-api-global-shortcut-cppgc branch from bca3c2b to 8032a0a Compare March 13, 2026 01:35
@ckerr
ckerr marked this pull request as draft March 13, 2026 15:31
@ckerr

ckerr commented Mar 13, 2026

Copy link
Copy Markdown
Member Author

This needs some more work to reconcile with the weak ptr references introduced in this week's Chromium roll. Working on this today.

@ckerr
ckerr force-pushed the refactor/electron-api-global-shortcut-cppgc branch from 8032a0a to 575c2c1 Compare March 17, 2026 17:36
@ckerr

ckerr commented Mar 18, 2026

Copy link
Copy Markdown
Member Author

This is probably going to jinx CI, but... this is one of the easier refactors so far.

I worked on this more today and it keeps getting more interesting. 😄

The crashes in CI now are, I think, due to the callbacks being registered: they are managed by JS via the gin_helper converter's SafeV8Function which uses V8::Global, ie is managed by JS. But the cppgc-managed GlobalShortcut C++ object has a std::map that holds onto it.

I'm looking at V8::TracedReference to see if there's something that can be done there. I'm also considering splitting the callbacks into two pieces -- a cppgc-managed side that emits a signal, and a JS-managed side that listens for the signal and invokes the JS-managed callback. 😵

@ckerr

ckerr commented Mar 18, 2026

Copy link
Copy Markdown
Member Author

Note: also it looks like maybe GlobalShortcut doesn't work on Wayland? GlobalAcceleratorListener::GetInstance() calls GlboalAcceleratorListenerOzone::Create() which only returns non-nullptr if it can create a listener that has a valid platform_global_shortcut_listener_ field. That field is set by calling GetPlatformGlobalShortcutListener() which creates an instance in X11 but seems to return nullptr on Wayland?

This is consistent with my findings when I run the GloablShortcut tests locally on Wayland -- all the calls to Register() fail because it GlobalAcceleratorListener::GetInstance() fails 😵

@deepak1556

deepak1556 commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

I'm looking at V8::TracedReference to see if there's something that can be done there

That would be right way to go, it will also simplify the refcounted management v8_function_ since we have cppgc::persistent to prepare a GC root and multiple copies of the SafeV8Function can create their own roots leaving V8 to manage the lifetime via the traced reference instead of us having to manage it.

@deepak1556

Copy link
Copy Markdown
Member

@ckerr is the crash you are seeing with callback ?

[91265:0320/123224.287730:FATAL:electron/shell/browser/api/electron_api_global_shortcut.cc:80] NOTREACHED hit. 
0   Electron Framework                  0x00000001264785b8 base::debug::CollectStackTrace(base::span<void const*, 18446744073709551615ul, void const**>) + 28
1   Electron Framework                  0x00000001264680c0 base::debug::StackTrace::StackTrace(unsigned long) + 224
2   Electron Framework                  0x0000000126380e18 logging::LogMessage::Flush() + 156
3   Electron Framework                  0x0000000126380cf4 logging::LogMessage::~LogMessage() + 36
4   Electron Framework                  0x000000012636bc68 logging::(anonymous namespace)::NotReachedLogMessage::~NotReachedLogMessage() + 76
5   Electron Framework                  0x000000012636b328 logging::CheckNoreturnError::~CheckNoreturnError() + 16
6   Electron Framework                  0x000000012636b338 logging::CheckNoreturnError::PCheck(char const*, base::Location const&) + 0
7   Electron Framework                  0x00000001200fef74 electron::api::GlobalShortcut::OnKeyPressed(ui::Accelerator const&) + 156
8   Electron Framework                  0x00000001230210c4 cppgc::internal::HeapVisitor<cppgc::internal::(anonymous namespace)::MutatorThreadSweeper>::Traverse(cppgc::internal::BasePage&) + 556
9   Electron Framework                  0x000000012302051c cppgc::internal::(anonymous namespace)::MutatorThreadSweeper::Sweep(cppgc::internal::(anonymous namespace)::SweepingState&) + 88
10  Electron Framework                  0x0000000123020070 cppgc::internal::Sweeper::SweeperImpl::Finish() + 700
11  Electron Framework                  0x000000012301bea4 cppgc::internal::Sweeper::SweeperImpl::FinishIfRunning() + 256
12  Electron Framework                  0x0000000123005604 cppgc::internal::HeapBase::Terminate() + 320
13  Electron Framework                  0x0000000121ff57c8 v8::internal::CppHeap::~CppHeap() + 52
14  Electron Framework                  0x0000000121ff5920 non-virtual thunk to v8::internal::CppHeap::~CppHeap() + 16
15  Electron Framework                  0x000000012209192c v8::internal::Heap::TearDown() + 268
16  Electron Framework                  0x0000000121f3c53c v8::internal::Isolate::Deinit() + 1104
17  Electron Framework                  0x0000000121f3bfac v8::internal::Isolate::Deinitialize(v8::internal::Isolate*) + 204
18  Electron Framework                  0x0000000121f3bea4 v8::internal::Isolate::Delete(v8::internal::Isolate*) + 20
19  Electron Framework                  0x00000001280f8ca8 gin::IsolateHolder::~IsolateHolder() + 308
20  Electron Framework                  0x0000000120211ea8 electron::JavascriptEnvironment::~JavascriptEnvironment() + 468
21  Electron Framework                  0x00000001201e98e4 electron::ElectronBrowserMainParts::~ElectronBrowserMainParts() + 244
22  Electron Framework                  0x00000001201e9984 electron::ElectronBrowserMainParts::~ElectronBrowserMainParts() + 12
23  Electron Framework                  0x00000001245e8ec4 content::BrowserMainLoop::~BrowserMainLoop() + 580
24  Electron Framework                  0x00000001245e9080 content::BrowserMainLoop::~BrowserMainLoop() + 12
25  Electron Framework                  0x00000001245edf50 content::BrowserMainRunnerImpl::Shutdown() + 244
26  Electron Framework                  0x00000001245e8938 content::BrowserMain(content::MainFunctionParams) + 224
27  Electron Framework                  0x00000001207f58fc content::RunBrowserProcessMain(content::MainFunctionParams, content::ContentMainDelegate*) + 192
28  Electron Framework                  0x00000001207f8324 content::ContentMainRunnerImpl::RunBrowser(content::MainFunctionParams, bool) + 2728
29  Electron Framework                  0x00000001207f7818 content::ContentMainRunnerImpl::Run() + 1448
30  Electron Framework                  0x00000001207f4ddc content::RunContentProcess(content::ContentMainParams, content::ContentMainRunner*) + 1148
31  Electron Framework                  0x00000001207f4fa0 content::ContentMain(content::ContentMainParams) + 112
32  Electron Framework                  0x0000000120098e30 ElectronMain + 120
33  Electron                            0x00000001047a04e0 main + 96
34  dyld                                0x000000018ed17da4 start + 6992
Crash keys:
  "chrome-trace-id" = "11170748724595171198"
  "platform" = "darwin"
  "process_type" = "browser"

@deepak1556

Copy link
Copy Markdown
Member

Nvm I was on a older version of this branch, pushing a lifetime fix.

@deepak1556

Copy link
Copy Markdown
Member

Spent an entire weekend debugging the different DCHECKs we are hitting in this PR during the incremental mark and atomic pause. Got through the weeds of unified heap, expect some interesting writeup today.

@deepak1556
deepak1556 force-pushed the refactor/electron-api-global-shortcut-cppgc branch from a29a6de to 19f4f38 Compare March 23, 2026 23:59
@deepak1556

Copy link
Copy Markdown
Member

V8 team member mentioned that the inheritance rule and other cppgc related compile time verifications are enforced via the clang plugin https://source.chromium.org/chromium/chromium/src/+/main:tools/clang/blink_gc_plugin/ . I will remove the patch as followup once the plugin is enabled.

@deepak1556
deepak1556 marked this pull request as ready for review March 24, 2026 14:37
@deepak1556
deepak1556 marked this pull request as draft March 24, 2026 14:53
@deepak1556

Copy link
Copy Markdown
Member

Okay the blink_gc_plugin is built into the default clang build https://source.chromium.org/chromium/chromium/src/+/main:tools/clang/scripts/build.py;l=1263-1265 (good we don;t need to build our own clang binary). I will enable the plugin before merging this change.

@deepak1556
deepak1556 force-pushed the refactor/electron-api-global-shortcut-cppgc branch from 3fa35aa to 5bfdcc7 Compare April 10, 2026 19:09
ckerr and others added 10 commits April 13, 2026 14:34
copy the lazy-create idom used by electron::api::Screen
safeguard against the callback changing the map, invalidating `cb`
- Setup isolate dispose observer to run destruction sequences
and remove self persistent reference
- Skip NOTREACHED check during destruction, it can happen
as a result of plaform listeners scheduling callbacks when Unregister is invoked.
- Fix the order of unregistration in GlobalShortcut::Unregister
- Add GlobalShortcut::UnregisterAllInternal to avoid any callsites
that can re-enter V8
@deepak1556
deepak1556 force-pushed the refactor/electron-api-global-shortcut-cppgc branch from 7b453f6 to 8ca48f1 Compare April 13, 2026 05:36
@jkleinsc
jkleinsc merged commit 53bf94f into main Apr 14, 2026
108 of 111 checks passed
@jkleinsc
jkleinsc deleted the refactor/electron-api-global-shortcut-cppgc branch April 14, 2026 19:52
@release-clerk

release-clerk Bot commented Apr 14, 2026

Copy link
Copy Markdown

No Release Notes

@trop

trop Bot commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "42-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot added needs-manual-bp/42-x-y and removed target/42-x-y PR should also be added to the "42-x-y" branch. labels Apr 14, 2026
ckerr added a commit that referenced this pull request May 26, 2026
@trop

trop Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

@VerteDinde has manually backported this PR to "42-x-y", please check out #52974

MarshallOfSound pushed a commit that referenced this pull request Aug 19, 2026
refactor: move `electron::api::GlobalShortcut` to cppgc (#50192)

* refactor: migrate electron::api::GlobalShortcut to cppgc

* refactor: lazy-create electron::api::GlobalShortcut

copy the lazy-create idom used by electron::api::Screen

* refactor: use gin::WeakCellFactory in GlobalCallbacks

* fix: make a copy of `callback` before running it

safeguard against the callback changing the map, invalidating `cb`

* chore: reduce unnecessary diffs with main

* fixup! refactor: use gin::WeakCellFactory in GlobalCallbacks

fix: must Trace() the weak cell factory

* fix: destruction order

- Setup isolate dispose observer to run destruction sequences
and remove self persistent reference
- Skip NOTREACHED check during destruction, it can happen
as a result of plaform listeners scheduling callbacks when Unregister is invoked.
- Fix the order of unregistration in GlobalShortcut::Unregister
- Add GlobalShortcut::UnregisterAllInternal to avoid any callsites
that can re-enter V8

* fix: crash during gc from incorrect cppgc object headers

* chore: update patches

* chore: cleanup

* chore: fix lint

---------


(cherry picked from commit 53bf94f)

Co-authored-by: Charles Kerr <[email protected]>
Co-authored-by: deepak1556 <[email protected]>
@trop trop Bot added merged/42-x-y PR was merged to the "42-x-y" branch. and removed in-flight/42-x-y labels Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/42-x-y PR was merged to the "42-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants