Repository: electron
Issue: Crash when closing devtools after focus
Labels: platform/macOS,platform/linux,component/devtools,crash :boom:,status/confirmed,component/webcontents,28-x-y,29-x-y

Happens on at least macOS and Linux on current master. Also reproducible on v6.0.4, v5.0.10, and v4.2.9.

Fiddle: https://gist.github.com/6872f27344c011f3565921761b6f8f5f

```js
      const w = new BrowserWindow({show: true})
      const devToolsFocused = emittedOnce(w.webContents, 'devtools-focused')
      w.webContents.openDevTools({ mode: 'detach' })
      w.webContents.inspectElement(100, 100)
      await devToolsFocused
      w.webContents.closeDevTools()
```

Stack trace:

```
Received signal 11 SEGV_MAPERR 000000000020
0   Electron Framework                  0x000000011da29da9 base::debug::CollectStackTrace(void**, unsigned long) + 9
1   Electron Framework                  0x000000011d8d4b53 base::debug::StackTrace::StackTrace() + 19
2   Electron Framework                  0x000000011da29c41 base::debug::(anonymous namespace)::StackDumpSignalHandler(int, __siginfo*, void*) + 2385
3   libsystem_platform.dylib            0x00007fff66ab1b5d _sigtramp + 29
4   ???                                 0x00007f96d05b0f00 0x0 + 140285717450496
5   Electron Framework                  0x00000001192f37bf -[AtomInspectableWebContentsView windowDidBecomeMain:] + 63
6   CoreFoundation                      0x00007fff3a96137a __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
7   CoreFoundation                      0x00007fff3a9612f4 ___CFXRegistrationPost_block_invoke + 63
8   CoreFoundation                      0x00007fff3a96125e _CFXRegistrationPost + 404
9   CoreFoundation                      0x00007fff3a96968d ___CFXNotificationPost_block_invoke + 87
10  CoreFoundation                      0x00007fff3a8d276c -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1834
11  CoreFoundation                      0x00007fff3a8d1a17 _CFXNotificationPost + 840
12  Foundation                          0x00007fff3cb7506b -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
13  AppKit                              0x00007fff380c7a4a -[NSWindow _changeKeyAndMainLimitedOK:] + 972
14  AppKit                              0x00007fff380c7569 -[NSWindow _makeKeyRegardlessOfVisibility] + 76
15  AppKit                              0x00007fff380c7118 -[NSWindow makeKeyAndOrderFront:] + 40
16  Electron Framework                  0x00000001192f3260 -[AtomInspectableWebContentsView setIsDocked:activate:] + 512
17  Electron Framework                  0x00000001192f9600 electron::InspectableWebContentsImpl::SetIsDocked(base::RepeatingCallback<void (base::Value const*)> const&, bool) + 48
18  Electron Framework                  0x00000001192f8ffe electron::InspectableWebContentsImpl::LoadCompleted() + 430
19  Electron Framework                  0x0000000122e20c3d bool (anonymous namespace)::ParseAndHandle<>(base::RepeatingCallback<void ()> const&, base::RepeatingCallback<void (base::Value const*)> const&, base::ListValue const&) + 109
20  Electron Framework                  0x0000000122e20a6a DispatcherImpl::Dispatch(base::RepeatingCallback<void (base::Value const*)> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, base::ListValue const*) + 138
21  Electron Framework                  0x00000001192fabc5 electron::InspectableWebContentsImpl::HandleMessageFromDevToolsFrontend(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 517
22  Electron Framework                  0x00000001192fc3ef base::internal::Invoker<base::internal::BindState<void (electron::InspectableWebContentsImpl::*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&), base::WeakPtr<electron::InspectableWebContentsImpl> >, void (std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>::Run(base::internal::BindStateBase*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 191
23  Electron Framework                  0x000000011ccf01e9 non-virtual thunk to content::DevToolsFrontendHostImpl::DispatchEmbedderMessage(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 105
24  Electron Framework                  0x000000011a2934e4 blink::mojom::DevToolsFrontendHostStubDispatch::Accept(blink::mojom::DevToolsFrontendHost*, mojo::Message*) + 388
25  Electron Framework                  0x000000011ddda238 mojo::InterfaceEndpointClient::HandleValidatedMessage(mojo::Message*) + 1096
26  Electron Framework                  0x000000011ddd9ab6 mojo::FilterChain::Accept(mojo::Message*) + 150
27  Electron Framework                  0x000000011dddb858 mojo::InterfaceEndpointClient::HandleIncomingMessage(mojo::Message*) + 120
28  Electron Framework                  0x000000011e321e79 IPC::(anonymous namespace)::ChannelAssociatedGroupController::AcceptOnProxyThread(mojo::Message) + 761
29  Electron Framework                  0x000000011e3221ff base::internal::Invoker<base::internal::BindState<void (IPC::(anonymous namespace)::ChannelAssociatedGroupController::*)(mojo::Message), scoped_refptr<IPC::(anonymous namespace)::ChannelAssociatedGroupController>, mojo::Message>, void ()>::RunOnce(base::internal::BindStateBase*) + 159
30  Electron Framework                  0x000000011d980943 base::TaskAnnotator::RunTask(char const*, base::PendingTask*) + 355
31  Electron Framework                  0x000000011d9a54ac base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoWorkImpl(base::sequence_manager::LazyNow*, bool*) + 652
32  Electron Framework                  0x000000011d9a5c0e base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::DoDelayedWork(base::TimeTicks*) + 158
33  Electron Framework                  0x000000011da5eec5 base::MessagePumpCFRunLoopBase::RunWork() + 85
34  Electron Framework                  0x000000011d8c9baa base::mac::CallWithEHFrame(void () block_pointer) + 10
35  Electron Framework                  0x000000011da5e68f base::MessagePumpCFRunLoopBase::RunWorkSource(void*) + 63
36  CoreFoundation                      0x00007fff3a91adbb __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
37  CoreFoundation                      0x00007fff3a91ad61 __CFRunLoopDoSource0 + 108
38  CoreFoundation                      0x00007fff3a8fec6b __CFRunLoopDoSources0 + 195
39  CoreFoundation                      0x00007fff3a8fe233 __CFRunLoopRun + 1196
40  CoreFoundation                      0x00007fff3a8fdb35 CFRunLoopRunSpecific + 459
41  HIToolbox                           0x00007fff39bdc96b RunCurrentEventLoopInMode + 292
42  HIToolbox                           0x00007fff39bdc6a5 ReceiveNextEventCommon + 603
43  HIToolbox                           0x00007fff39bdc436 _BlockUntilNextEventMatchingListInModeWithFilter + 64
44  AppKit                              0x00007fff37f76987 _DPSNextEvent + 965
45  AppKit                              0x00007fff37f7571f -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1361
46  AppKit                              0x00007fff37f6f83c -[NSApplication run] + 699
47  Electron Framework                  0x000000011da5ff4c base::MessagePumpNSApplication::DoRun(base::MessagePump::Delegate*) + 348
48  Electron Framework                  0x000000011da5dff8 base::MessagePumpCFRunLoopBase::Run(base::MessagePump::Delegate*) + 136
49  Electron Framework                  0x000000011d9a6232 base::sequence_manager::internal::ThreadControllerWithMessagePumpImpl::Run(bool, base::TimeDelta) + 242
50  Electron Framework                  0x000000011d950a56 base::RunLoop::Run() + 726
51  Electron Framework                  0x000000011c53044d content::BrowserMainLoop::RunMainMessageLoopParts() + 173
52  Electron Framework                  0x000000011c532026 content::BrowserMainRunnerImpl::Run() + 102
53  Electron Framework                  0x000000011c52cc7f content::BrowserMain(content::MainFunctionParams const&) + 287
54  Electron Framework                  0x000000011c3602a5 content::ContentMainRunnerImpl::RunServiceManager(content::MainFunctionParams&, bool) + 949
55  Electron Framework                  0x000000011c35feca content::ContentMainRunnerImpl::Run(bool) + 442
56  Electron Framework                  0x000000011fdb4bb3 service_manager::Main(service_manager::MainParams const&) + 3139
57  Electron Framework                  0x000000011a657ff4 content::ContentMain(content::ContentMainParams const&) + 68
58  Electron Framework                  0x00000001191c3854 AtomMain + 84
59  Electron                            0x000000010c55dc90 main + 240
60  libdyld.dylib                       0x00007fff668c63d5 start + 1
61  ???                                 0x0000000000000003 0x0 + 3
[end of stack trace]
```