Bug 193184 - MessagePort is unexpectedly GC'ed after activity absence
Summary: MessagePort is unexpectedly GC'ed after activity absence
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore JavaScript (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL: https://html.spec.whatwg.org/multipag...
Keywords: InRadar
: 247372 (view as bug list)
Depends on: 181910
Blocks:
  Show dependency treegraph
 
Reported: 2019-01-06 18:26 PST by Yoshiaki Jitsukawa
Modified: 2022-11-29 14:42 PST (History)
14 users (show)

See Also:


Attachments
Patch (test only) (4.03 KB, patch)
2019-01-06 18:43 PST, Yoshiaki Jitsukawa
ews-watchlist: commit-queue-
Details | Formatted Diff | Diff
Archive of layout-test-results from ews122 for ios-simulator-wk2 (2.41 MB, application/zip)
2019-01-06 20:37 PST, EWS Watchlist
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Yoshiaki Jitsukawa 2019-01-06 18:26:38 PST
MessagePort is unexpectedly GC'ed after several GC if there'no activity around the MessagePort, though the MessagePort may be used afterwards.
Comment 1 Yoshiaki Jitsukawa 2019-01-06 18:43:01 PST
Created attachment 358473 [details]
Patch (test only)
Comment 2 Yoshiaki Jitsukawa 2019-01-06 18:53:54 PST
Attachment 358473 [details] is a test for reproduction.

The main frame generates two MessagePorts port1 and port2 from a MessageChannel. port2 is passed to the iframe and bound to a XMLHttpRequest.

 <main frame>
 port1

 <iframe>
 port2 <- xhr

XHR gets a response after one second and then let port2 post a message to port1. However port1 is GC'ed after three times GC and port1 cannot receive the message. (Running GC two times was OK.)
Comment 3 Yoshiaki Jitsukawa 2019-01-06 18:58:58 PST
I suspect this but not sure.
Bug 181910 - Make garbage collection of MessagePort objects be asynchronous
Comment 4 EWS Watchlist 2019-01-06 20:37:26 PST
Comment on attachment 358473 [details]
Patch (test only)

Attachment 358473 [details] did not pass ios-sim-ews (ios-simulator-wk2):
Output: https://webkit-queues.webkit.org/results/10653761

New failing tests:
http/tests/messaging/messageport-gc-after-xhr.html
Comment 5 EWS Watchlist 2019-01-06 20:37:27 PST
Created attachment 358477 [details]
Archive of layout-test-results from ews122 for ios-simulator-wk2

The attached test failures were seen while running run-webkit-tests on the ios-sim-ews.
Bot: ews122  Port: ios-simulator-wk2  Platform: Mac OS X 10.13.6
Comment 6 Radar WebKit Bug Importer 2019-08-08 14:08:05 PDT
<rdar://problem/54095480>
Comment 7 Alexey Proskuryakov 2022-11-02 13:03:38 PDT
*** Bug 247372 has been marked as a duplicate of this bug. ***
Comment 8 Geoffrey Garen 2022-11-02 14:31:19 PDT
It looks like Bug 181910 was a partial implementation that, by design, allowed a MessagePort to be GC'd after receiving its first message. I guess that's consistent with the observation in this test case, that the MessagePort is GC'd after receiving its first message.

It seems like the behavior we want is that a MessagePort should be ineligible for GC (hasPendingActivity() == true) until (a) it is closed OR (b) its ScriptExecutionContext is torn down OR (c) every connected MessagePort's ScriptExecutionContext is torn down.
Comment 9 Chris Dumez 2022-11-03 12:48:31 PDT
MessagePort garbage collection is specified here:
- https://html.spec.whatwg.org/multipage/web-messaging.html#ports-and-garbage-collection

I can look at aligning with the spec.
Comment 10 Chris Dumez 2022-11-03 13:58:57 PDT
Pull request: https://github.com/WebKit/WebKit/pull/6099
Comment 11 youenn fablet 2022-11-04 09:36:59 PDT
This might explain https://github.com/GoogleChromeLabs/comlink/issues/600 and rdar://99145178
Comment 12 EWS 2022-11-04 15:49:27 PDT
Committed 256342@main (a4790ba92ba7): <https://commits.webkit.org/256342@main>

Reviewed commits have been landed. Closing PR #6099 and removing active labels.