Skip to content

[Bug]: Debugger is unable to pause early in new browser windows #40734

Open

Description

@connor4312

Preflight Checklist

Electron Version

28.0.0

What operating system are you using?

Windows

Operating System Version

Windows 10 22H2

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

  1. Clone https://github.com/connor4312/electron-early-break-bug, and open it in VS Code
  2. In the Debug view, enable break on Caught Exceptions
  3. Hit F5. Open a new window.
  4. Expect to break in renderer.js

Actual Behavior

The runtime does not break at that line, and instead gets a bit broken (see below :P)

Testcase Gist URL

No response

Additional Information

What actually happens is the runtime breaks on internals in the ESM loader. This is not very pretty if it were to be exposed to the user, but it's fine and expected. However, the runtime then rapidly resumes and pauses on multiple exception sites, without the debugger asking it to. Instead, I would expect it to remain paused on the first uncaught exception until a client asks to resume. These are logs from the debugger, found in log.txt (and browsable here) in the example, note that it's receiving debugger state changes but not sending anything:

image

(The rapid pause/resumes are not something the VS Code debugger handles very well, so the UI gets into a torn state)

I observed very similar behavior inside the VS Code codebase when dealing with floating windows and user-defined breakpoints. I haven't attempted to make an isolated case of that, perhaps it's the same root cause.

Unfortunately I had some trouble trying to get a standalone fiddle to work, perhaps there's some timing component that the vscode debugger triggers.

Activity

  1. moved this to 👀 Unsorted Items in 28-x-yon Dec 11, 2023
  2. moved this from 👀 Unsorted Items to 🛑 Blocks Stable in 28-x-yon Jan 3, 2024
  3. moved this from 🛑 Blocks Stable to 👍 Does Not Block Stable in 28-x-yon Jan 3, 2024
  4. added
    has-repro-repo
    on Jan 8, 2024
  5. electron-issue-triage commented on Apr 8, 2024

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  6. connor4312 commented on Apr 8, 2024

    @connor4312
    Author

    bump

  7. electron-issue-triage commented on Jul 8, 2024

    @electron-issue-triage

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!

  8. electron-issue-triage commented on Aug 7, 2024

    @electron-issue-triage

    This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.

  9. 5 remaining items

  10. moved this to 👀 Unsorted Items in 35-x-yon Feb 10, 2025
  11. moved this to 👀 Unsorted Items in 34-x-yon Feb 10, 2025
  12. added
    status/confirmed
    and removed on Feb 10, 2025
  13. deepak1556 commented on Feb 10, 2025

    @deepak1556
    Member

    I tried to repro with the debugger module but haven't been successful to see the flow of pause and resume events that was seen in the above repro with vscode debugger,

    mainWindow.webContents.debugger.on('message', (event, method, params, sessionId) => {
          if (method !== 'Debugger.scriptParsed') {
            console.log('Debugger message : ', method, params, sessionId)
          }
    })
    
    mainWindow.webContents.debugger.sendCommand('Debugger.enable').then(() => {
       mainWindow.webContents.debugger.sendCommand('Debugger.setPauseOnExceptions', {
           state: 'caught'
        })
    })

    Then I realized this might have to do with multiple contexts, I disabled context isolation and let the preload script run in the same context as the renderer, the vscode debugger was able to pause fine.

    I attempted to trace how the flow looks with multiple contexts between devtools and vscode debugger using the following trace args browser,blink,devtools,renderer,disabled-by-default-blink.debug,disabled-by-default-v8.inspector. The main difference I see is the call to Debugger.setBlackBoxExecutionContexts in the case of devtools.

    Image

    I haven't confirmed yet if the execution contexts passed in that call correspond to the preload script context (will do that shortly) but behavior wise I do see that devtools does not set breakpoints for exceptions from the preload context, so I wonder if this could be the reason for the strange behavior in vscode debugger ?

  14. deepak1556 commented on Feb 10, 2025

    @deepak1556
    Member

    Yeah the call was definitely made for the preload script context https://source.chromium.org/chromium/chromium/src/+/main:third_party/devtools-frontend/src/front_end/models/bindings/IgnoreListManager.ts;l=134-149 executionContext.isDefault : false.

    But I think now, this is unrelated to the bug. Went back and changed the ignore list in devtools to include content scripts and devtools is able to break in preload script caught exceptions.

  15. deepak1556 commented on Feb 10, 2025

    @deepak1556
    Member

    Running with a debug build shows the following crash,

    29173:0210/191156.902558:FATAL:document_loader.cc(1337)] Check failed: !frame_->GetPage()->Paused(). 
    0   Electron Framework                  0x0000000125eb9788 base::debug::CollectStackTrace(base::span<void const*, 18446744073709551615ul, void const**>) + 28
    1   Electron Framework                  0x0000000125eaa298 base::debug::StackTrace::StackTrace(unsigned long) + 156
    2   Electron Framework                  0x0000000125dd8468 logging::LogMessage::Flush() + 152
    3   Electron Framework                  0x0000000125dd835c logging::LogMessage::~LogMessage() + 36
    4   Electron Framework                  0x0000000125dbff60 logging::(anonymous namespace)::DCheckLogMessage::~DCheckLogMessage() + 76
    5   Electron Framework                  0x0000000125dbf820 logging::CheckError::~CheckError() + 44
    6   Electron Framework                  0x0000000125dbf880 logging::CheckError::~CheckError() + 12
    7   Electron Framework                  0x00000001299b15bc blink::DocumentLoader::BodyDataReceivedImpl(blink::DocumentLoader::BodyData&) + 568
    8   Electron Framework                  0x00000001299b1838 non-virtual thunk to blink::DocumentLoader::BodyDataReceived(base::span<char const, 18446744073709551615ul, char const*>) + 52
    9   Electron Framework                  0x00000001252ae4d8 blink::NavigationBodyLoader::MainThreadBodyReader::DataReceived(base::span<char const, 18446744073709551615ul, char const*>) + 492
    10  Electron Framework                  0x00000001252abbd4 blink::(anonymous namespace)::ReadFromDataPipeImpl(blink::(anonymous namespace)::BodyReader&, mojo::ScopedHandleBase<mojo::DataPipeConsumerHandle>&, mojo::SimpleWatcher&) + 376
    11  Electron Framework                  0x00000001252ab880 blink::NavigationBodyLoader::ReadFromDataPipe() + 264
    12  Electron Framework                  0x00000001252aa870 blink::NavigationBodyLoader::OnReadable(unsigned int) + 128
    13  Electron Framework                  0x00000001201d97d4 base::RepeatingCallback<void ()>::Run() const & + 128
    14  Electron Framework                  0x00000001201d9718 gin_helper::Invoker<std::__Cr::integer_sequence<unsigned long>>::DispatchToCallback(base::RepeatingCallback<void ()>) + 220
    15  Electron Framework                  0x00000001201d94d8 gin_helper::Dispatcher<void ()>::DispatchToCallbackImpl(gin::Arguments*) + 184
    16  Electron Framework                  0x00000001201d93a8 gin_helper::Dispatcher<void ()>::DispatchToCallback(v8::FunctionCallbackInfo<v8::Value> const&) + 44
    17  ???                                 0x0000000177be1478 0x0 + 6303913080
    18  ???                                 0x0000000177bde9f8 0x0 + 6303902200
    19  ???                                 0x0000000177e30040 0x0 + 6306332736
    20  ???                                 0x0000000177e3319c 0x0 + 6306345372
    21  ???                                 0x0000000177c39914 0x0 + 6304274708
    22  ???                                 0x0000000177bda354 0x0 + 6303884116
    23  Electron Framework                  0x00000001221004a4 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) + 5352
    24  Electron Framework                  0x0000000122101590 v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) + 160
    25  Electron Framework                  0x0000000122101b88 v8::internal::Execution::TryRunMicrotasks(v8::internal::Isolate*, v8::internal::MicrotaskQueue*) + 56
    26  Electron Framework                  0x000000012215fd64 v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) + 328
    27  Electron Framework                  0x000000012215fbbc v8::internal::MicrotaskQueue::PerformCheckpointInternal(v8::Isolate*) + 112
    28  Electron Framework                  0x00000001204945b8 node::InternalCallbackScope::Close() + 316
    29  Electron Framework                  0x00000001204941e8 node::InternalCallbackScope::~InternalCallbackScope() + 20
    30  Electron Framework                  0x000000012055d2fc node::fs::FileHandle::CloseReq::Resolve() + 208
    31  Electron Framework                  0x00000001205700b4 _ZZN4node2fs10FileHandle12ClosePromiseEvEN3$_08__invokeEP7uv_fs_s + 216
    32  Electron Framework                  0x000000012054fb38 node::MakeLibuvRequestCallback<uv_fs_s, void (*)(uv_fs_s*)>::Wrapper(uv_fs_s*) + 112
    33  Electron Framework                  0x00000001201be994 uv__work_done + 184
    34  Electron Framework                  0x00000001201c2170 uv__async_io + 272
    35  Electron Framework                  0x00000001201d3e28 uv__io_poll + 976
    36  Electron Framework                  0x00000001201c2664 uv_run + 408
    37  Electron Framework                  0x00000001203ee10c electron::NodeBindings::UvRunOnce() + 232
    38  Electron Framework                  0x00000001201db6f0 base::OnceCallback<void ()>::Run() && + 108
    39  Electron Framework                  0x0000000125e30160 base::TaskAnnotator::RunTaskImpl(base::PendingTask&) + 272
    40  Electron Framework                  0x0000000125e5fd14 base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWorkImpl(base::LazyNow*) + 1732
    41  Electron Framework                  0x0000000125e5f328 base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWork() + 100
    42  Electron Framework                  0x0000000125de13f0 base::MessagePumpDefault::Run(base::MessagePump::Delegate*) + 188
    43  Electron Framework                  0x0000000125e60b9c base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::Run(bool, base::TimeDelta) + 1088
    44  Electron Framework                  0x0000000125e10454 base::RunLoop::Run(base::Location const&) + 996
    45  Electron Framework                  0x000000012864f49c content::(anonymous namespace)::NestedMessageLoopRunnerImpl::Run() + 292
    46  Electron Framework                  0x000000012aae5598 blink::ClientMessageLoopAdapter::RunLoop(blink::WebLocalFrameImpl*) + 408
    47  Electron Framework                  0x000000012aae3224 blink::WebDevToolsAgentImpl::WaitForDebugger() + 124
    48  Electron Framework                  0x00000001295665e8 non-virtual thunk to blink::InspectorPageAgent::waitForDebugger() + 32
    49  Electron Framework                  0x000000012539f4ac blink::protocol::Page::DomainDispatcherImpl::waitForDebugger(crdtp::Dispatchable const&) + 68
    50  Electron Framework                  0x000000012820b270 crdtp::UberDispatcher::DispatchResult::Run() + 40
    51  Electron Framework                  0x00000001295ace4c blink::DevToolsSession::DispatchProtocolCommandImpl(int, WTF::String const&, base::span<unsigned char const, 18446744073709551615ul, unsigned char const*>) + 712
    52  Electron Framework                  0x00000001295acb2c blink::DevToolsSession::DispatchProtocolCommand(int, WTF::String const&, base::span<unsigned char const, 18446744073709551615ul, unsigned char const*>) + 112
    53  Electron Framework                  0x000000012505708c blink::mojom::blink::DevToolsSessionStubDispatch::Accept(blink::mojom::blink::DevToolsSession*, mojo::Message*) + 372
    54  Electron Framework                  0x000000012624f50c mojo::InterfaceEndpointClient::HandleValidatedMessage(mojo::Message*) + 1692
    55  Electron Framework                  0x00000001262557e0 mojo::MessageDispatcher::Accept(mojo::Message*) + 292
    56  Electron Framework                  0x0000000126251160 mojo::InterfaceEndpointClient::HandleIncomingMessage(mojo::Message*) + 148
    57  Electron Framework                  0x00000001267c1028 IPC::ChannelAssociatedGroupController::AcceptOnEndpointThread(mojo::Message, IPC::(anonymous namespace)::ScopedUrgentMessageNotification) + 604
    58  Electron Framework                  0x00000001267c1b3c base::internal::Invoker<base::internal::FunctorTraits<void (IPC::ChannelAssociatedGroupController::*&&)(mojo::Message, IPC::(anonymous namespace)::ScopedUrgentMessageNotification), IPC::ChannelAssociatedGroupController*&&, mojo::Message&&, IPC::(anonymous namespace)::ScopedUrgentMessageNotification&&>, base::internal::BindState<true, true, false, void (IPC::ChannelAssociatedGroupController::*)(mojo::Message, IPC::(anonymous namespace)::ScopedUrgentMessageNotification), scoped_refptr<IPC::ChannelAssociatedGroupController>, mojo::Message, IPC::(anonymous namespace)::ScopedUrgentMessageNotification>, void ()>::RunOnce(base::internal::BindStateBase*) + 232
    59  Electron Framework                  0x00000001201db6f0 base::OnceCallback<void ()>::Run() && + 108
    60  Electron Framework                  0x0000000125e30160 base::TaskAnnotator::RunTaskImpl(base::PendingTask&) + 272
    61  Electron Framework                  0x0000000125e5fd14 base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWorkImpl(base::LazyNow*) + 1732
    62  Electron Framework                  0x0000000125e5f328 base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWork() + 100
    63  Electron Framework                  0x0000000125de13f0 base::MessagePumpDefault::Run(base::MessagePump::Delegate*) + 188
    64  Electron Framework                  0x0000000125e60aec base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::Run(bool, base::TimeDelta) + 912
    65  Electron Framework                  0x0000000125e10454 base::RunLoop::Run(base::Location const&) + 996
    66  Electron Framework                  0x0000000128655724 content::RendererMain(content::MainFunctionParams) + 1384
    67  Electron Framework                  0x000000012087d964 content::RunOtherNamedProcessTypeMain(std::__Cr::basic_string<char, std::__Cr::char_traits<char>, std::__Cr::allocator<char>> const&, content::MainFunctionParams, content::ContentMainDelegate*) + 668
    68  Electron Framework                  0x000000012087f328 content::ContentMainRunnerImpl::Run() + 1060
    69  Electron Framework                  0x000000012087cda0 content::RunContentProcess(content::ContentMainParams, content::ContentMainRunner*) + 1396
    70  Electron Framework                  0x000000012087cf0c content::ContentMain(content::ContentMainParams) + 112
    71  Electron Framework                  0x00000001201d46e0 ElectronMain + 124
    72  Electron Helper (Renderer)          0x00000001040ccaa0 main + 224
    73  dyld                                0x000000019eef8274 start + 2840
    
  16. deepak1556 commented on Feb 10, 2025

    @deepak1556
    Member

    We receive Page.waitForDebugger from the client which ends up setting the WebScopedPagePauser in https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/exported/web_dev_tools_agent_impl.cc;l=244-265

    However, we seem to continue the execution of preload scripts since the pause is not plumbed into the Node.js integration which inturn causes the suspended document loading to resume before Runtime.runIfWaitingForDebugger is received.

  17. deepak1556 commented on Feb 10, 2025

    @deepak1556
    Member

    Yeah that seems like the issue, as a confirmation I delay the processing of Node.js callbacks when the page is paused and that fixes it

    diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc
    index 8c682aba1c..5fe08b41c6 100644
    --- a/shell/common/node_bindings.cc
    +++ b/shell/common/node_bindings.cc
    @@ -42,6 +42,8 @@
     #include "shell/common/world_ids.h"
     #include "third_party/blink/public/web/web_local_frame.h"
     #include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h"  // nogncheck
    +#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
    +#include "third_party/blink/renderer/platform/scheduler/public/main_thread_scheduler.h"
     #include "third_party/electron_node/src/debug_utils.h"
     #include "third_party/electron_node/src/module_wrap.h"
     
    @@ -821,8 +823,12 @@ void NodeBindings::StartPolling() {
     
       initialized_ = true;
     
    -  // The MessageLoop should have been created, remember the one in main thread.
    -  task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
    +  if (browser_env_ == BrowserEnvironment::kRenderer) {
    +    task_runner_ = blink::ThreadScheduler::Current()->ToMainThreadScheduler()->V8TaskRunner();
    +  } else {
    +    // The MessageLoop should have been created, remember the one in main thread.
    +    task_runner_ = base::SingleThreadTaskRunner::GetCurrentDefault();
    +  }
     
       // Run uv loop for once to give the uv__io_poll a chance to add all events.
       UvRunOnce();
    @@ -860,6 +866,12 @@ void NodeBindings::JoinAppCode() {
     }
     
     void NodeBindings::UvRunOnce() {
    +  if (browser_env_ == BrowserEnvironment::kRenderer &&
    +      blink::ThreadScheduler::Current()->ToMainThreadScheduler()->ShouldPauseTaskQueues()) {
    +    // Tell the worker thread to continue polling.
    +    uv_sem_post(&embed_sem_);
    +    return;
    +  }
       node::Environment* env = uv_env();
     
       // When doing navigation without restarting renderer process, it may happen
  18. nikwen commented on Feb 12, 2025

    @nikwen
    Member

    @deepak1556 For the preload issue you identified, see also #45399.

  19. deepak1556 commented on Feb 12, 2025

    @deepak1556
    Member

    Thanks @nikwen but I think the two issues are unrelated. This one particularly affects esm preload in non sandboxed renderers. I have some more clarity on how the stack #40734 (comment) got to be.

    For me it was confusing how we entered blink::NavigationBodyLoader::OnReadable from gin_helper since we don't have any explicit callbacks set in //electron. Capturing the missing V8 stacks helped clarify, the flow was

    1. Page gets paused via https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/exported/web_dev_tools_agent_impl.cc;l=261 that eventually updates the frame lifecycle state via LocalFrame::OnPageLifecycleStateUpdated and in turn defers the document loading in LocalFrame::SetContextPaused
    2. Node environment begins setup in ElectronRendererClient::DidCreateScriptContext and we also defer the loading of the document to ensure preload scripts gets loaded first
      // Before we load the node environment, let's tell blink to hold off on
      // loading the body of this frame. We will undefer the load once the preload
      // script has finished. This allows our preload script to run async (E.g.
      // with ESM) without the preload being in a race
      render_frame->GetWebFrame()->GetDocumentLoader()->SetDefersLoading(
      blink::LoaderFreezeMode::kStrict);
      and this will eventually call into NavigationBodyLoader::SetDefersLoading which will bail early since the loading state matches that set in step 1)
    3. The interesting part is once we finish loading the node environment which is not paused at this point eventually ends up calling ElectronRendererClient::UndeferLoad via
      }).finally(() => appCodeLoaded!());
      and this is JS that is triggering the loader to resume while the page is still in paused state.

    To properly address this following needs to be validated,

    1. Why does DidInstallConditionalFeatures get triggered while page is in paused state ? Is this from the original about:blank page ? then we are looking at a race condition
    2. Should we observe on the frame lifecycle state when loading preload scripts and how do we manage document loading i-e how to do we ensure the loading unpause happens after the preload script finishes and not by the ScopedPagePauser
  20. moved this from 👀 Unsorted Items to 👍 Does Not Block Stable in 35-x-yon Feb 12, 2025
  21. moved this from 👀 Unsorted Items to 👍 Does Not Block Stable in 34-x-yon Feb 12, 2025
  22. added theissue type on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    34-x-y35-x-ybug 🪲has-repro-repostatus/confirmed

    Type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions