fix: crash problem with message_port close event (#41201)

When worker_thread shutdown, it will destory context and close
message_port. In this case, it should not dispatch close event.
Because it forbid script running during NotifyContextDestroyed in
ContextLifecycleNotifier.
Now chromium has implemented close_event and will not crash,
so we remove the patch with #22532 and add one test.
This commit is contained in:
wujinli 2024-02-05 15:38:30 +08:00 коммит произвёл GitHub
Родитель 398ca2a019
Коммит fb888a6989
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 53 добавлений и 32 удалений

Просмотреть файл

@ -47,7 +47,6 @@ feat_add_support_for_overriding_the_base_spellchecker_download_url.patch
feat_enable_offscreen_rendering_with_viz_compositor.patch
gpu_notify_when_dxdiag_request_fails.patch
feat_allow_embedders_to_add_observers_on_created_hunspell.patch
feat_add_onclose_to_messageport.patch
allow_in-process_windows_to_have_different_web_prefs.patch
refactor_expose_cursor_changes_to_the_webcontentsobserver.patch
crash_allow_setting_more_options.patch

Просмотреть файл

@ -1,31 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jeremy Apthorp <nornagon@nornagon.net>
Date: Wed, 4 Mar 2020 11:18:03 -0800
Subject: feat: add onclose to MessagePort
This adds the 'onclose' event to MessagePort. Can be removed once
https://bugs.chromium.org/p/chromium/issues/detail?id=1495616 is fixed.
diff --git a/third_party/blink/renderer/core/messaging/message_port.cc b/third_party/blink/renderer/core/messaging/message_port.cc
index 2e0961fd8c0bc8f9a2a7c124693033eb234928bd..5c54705e3c1766553e9cba13142345485ca44578 100644
--- a/third_party/blink/renderer/core/messaging/message_port.cc
+++ b/third_party/blink/renderer/core/messaging/message_port.cc
@@ -195,6 +195,7 @@ void MessagePort::close() {
Entangle(pipe.TakePort0(), nullptr);
}
closed_ = true;
+ DispatchEvent(*Event::Create(event_type_names::kClose));
}
void MessagePort::OnConnectionError() {
diff --git a/third_party/blink/renderer/core/messaging/message_port.idl b/third_party/blink/renderer/core/messaging/message_port.idl
index 9df43655b569428a6abc54341b4b0023e159f99b..3f1f181d9b8a66997136f870f55c97c08294b6eb 100644
--- a/third_party/blink/renderer/core/messaging/message_port.idl
+++ b/third_party/blink/renderer/core/messaging/message_port.idl
@@ -40,5 +40,5 @@
// event handlers
attribute EventHandler onmessage;
attribute EventHandler onmessageerror;
- [RuntimeEnabled=MessagePortCloseEvent] attribute EventHandler onclose;
+ attribute EventHandler onclose;
};

Просмотреть файл

@ -345,6 +345,59 @@ describe('ipc module', () => {
}})()`);
});
});
describe('when context destroyed', () => {
it('does not crash', async () => {
let count = 0;
const server = http.createServer((req, res) => {
switch (req.url) {
case '/index.html':
res.setHeader('content-type', 'text/html');
res.statusCode = 200;
count = count + 1;
res.end(`
<title>Hello${count}</title>
<script>
var sharedWorker = new SharedWorker('worker.js');
sharedWorker.port.addEventListener('close', function(event) {
console.log('close event', event.data);
});
</script>`);
break;
case '/worker.js':
res.setHeader('content-type', 'application/javascript; charset=UTF-8');
res.statusCode = 200;
res.end(`
self.addEventListener('connect', function(event) {
var port = event.ports[0];
port.addEventListener('message', function(event) {
console.log('Message from main:', event.data);
port.postMessage('Hello from SharedWorker!');
});
});`);
break;
default:
throw new Error(`unsupported endpoint: ${req.url}`);
}
});
const { port } = await listen(server);
defer(() => {
server.close();
});
const w = new BrowserWindow({ show: false });
await w.loadURL(`http://localhost:${port}/index.html`);
expect(w.webContents.getTitle()).to.equal('Hello1');
// Before the fix, it would crash if reloaded, but now it doesn't
await w.loadURL(`http://localhost:${port}/index.html`);
expect(w.webContents.getTitle()).to.equal('Hello2');
// const crashEvent = emittedOnce(w.webContents, 'render-process-gone');
// await crashEvent;
});
});
});
describe('MessageChannelMain', () => {