Bug 102775

Summary: Loader: Store WeakRef CachedResourceHandle in CachedResourceLoader
Product: WebKit Reporter: pdeng6 <pan.deng>
Component: Page LoadingAssignee: Vsevolod Vlasov <vsevik>
Status: UNCONFIRMED    
Severity: Normal CC: abarth, apavlov, benjamin, cmarcelo, dglazkov, japhet, keishi, koivisto, loislo, ojan.autocc, pfeldman, pmuellr, scottmg, vsevik, web-inspector-bugs, webkit.review.bot, yurys
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Attachments:
Description Flags
This pic shows expected result, BF4DD16647C553D78FAAED29C99B5174.cache.js available.
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch abarth: review-

pdeng6
Reported 2012-11-19 23:57:38 PST
Created attachment 175160 [details] This pic shows expected result, BF4DD16647C553D78FAAED29C99B5174.cache.js available. How to Reproduce 1, Launch chromium, and load page https://code.google.com/p/chromium/source/search?q=file%3Aeventhandler.cpp 2, after page full loaded, open Developer Tools, click Resources Panel 3, 'BF4DD16647C553D78FAAED29C99B5174.cache.js' is not available in Frames->search->cs_frame->Scripts. while, it's been loaded. the reason is CachedResource of 'BF4DD16647C553D78FAAED29C99B5174.cache.js' is removed by CachedResourceLoader::garbageCollectDocumentResources before inspector-frontend ask for resources of frame. attachment is an expected result.
Attachments
This pic shows expected result, BF4DD16647C553D78FAAED29C99B5174.cache.js available. (97.40 KB, image/png)
2012-11-19 23:57 PST, pdeng6
no flags
Patch (3.22 KB, patch)
2012-11-25 19:42 PST, pdeng6
no flags
Patch (31.56 KB, patch)
2012-11-30 00:55 PST, pdeng6
no flags
Patch (28.66 KB, patch)
2012-12-07 03:10 PST, pdeng6
no flags
Patch (29.29 KB, patch)
2012-12-13 05:07 PST, pdeng6
no flags
Patch (18.80 KB, patch)
2013-02-28 06:46 PST, pdeng6
no flags
Patch (18.47 KB, patch)
2013-03-01 00:08 PST, pdeng6
no flags
Patch (16.57 KB, patch)
2013-03-05 00:00 PST, pdeng6
abarth: review-
pdeng6
Comment 1 2012-11-25 19:42:52 PST
Brent Fulgham
Comment 2 2012-11-25 20:12:30 PST
Comment on attachment 175915 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=175915&action=review > Source/WebCore/ChangeLog:8 > + The root cause of bug 102775: m_documentResources is info pool for Inspector Resource Panel, some CachedResourceHandles are removed from m_documentResources in order to delete strong-refs to CachedResources they hold, so they won't be presented in Resource Panel even they are not actually deleted. In stead of removing from m_documentResources, this change unregisters handles to weak-ref them, and weak-ref handle will be deleted when CachedResource destroyed. This line should be broken at roughly 80 columns, like the other ChangeLog entries.
Pavel Feldman
Comment 3 2012-11-25 22:06:32 PST
Comment on attachment 175915 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=175915&action=review > Source/WebCore/loader/cache/CachedResourceLoader.cpp:719 > + for (DocumentResourceMap::iterator it = m_documentResources.begin(); it != m_documentResources.end(); ++it) Typically, we don't apply intrusive changes like that for the sake of inspector. Is is fixing anything other than inspector bug? Have you consulted with the garbageCollectDocumentResources owners? Adding Antti.
Vsevolod Vlasov
Comment 4 2012-11-25 23:20:18 PST
Comment on attachment 175915 [details] Patch I don't think this patch makes sense as currently resource is only removed from m_documentResources when it has one (last) handle. Hence weak-ref'ing will immediately remove the resource anyway.
pdeng6
Comment 5 2012-11-26 01:59:14 PST
(In reply to comment #4) > (From update of attachment 175915 [details]) > I don't think this patch makes sense as currently resource is only removed from m_documentResources when it has one (last) handle. Hence weak-ref'ing will immediately remove the resource anyway. thanks, this patch need improvement. while, I think the idea is ok as a workaround. if we wek-ref the resource Handle, the resource won't be removed immediately if it is in memcache.(this is the case inspector should represent it in panel I think) another fix may be: a WeakRefResourceHandle Class to satisfy m_documentResources in CachedResourceLoader, how do you think of it? :)
pdeng6
Comment 6 2012-11-26 06:27:10 PST
@japhet I find following in CachedResource.h CachedResourceLoader* m_owningCachedResourceLoader; // only non-0 for resources that are not in the cache the reason of setting m_owningCachedResourceLoader to 0 is preventing duplicate memoryUsage report? or other? thanks a lot.
pdeng6
Comment 7 2012-11-30 00:55:11 PST
WebKit Review Bot
Comment 8 2012-11-30 03:18:26 PST
Comment on attachment 176916 [details] Patch Attachment 176916 [details] did not pass chromium-ews (chromium-xvfb): Output: http://queues.webkit.org/results/15060236 New failing tests: http/tests/css/cross-fade-reload.html
pdeng6
Comment 9 2012-12-07 03:10:33 PST
WebKit Review Bot
Comment 10 2012-12-07 05:15:01 PST
Comment on attachment 178189 [details] Patch Attachment 178189 [details] did not pass chromium-ews (chromium-xvfb): Output: http://queues.webkit.org/results/15191209 New failing tests: http/tests/cookies/js-get-and-set-http-only-cookie.html
pdeng6
Comment 11 2012-12-13 05:07:40 PST
Pavel Feldman
Comment 12 2012-12-14 00:19:10 PST
(In reply to comment #0) > Created an attachment (id=175160) [details] > This pic shows expected result, BF4DD16647C553D78FAAED29C99B5174.cache.js available. > > How to Reproduce > 1, Launch chromium, and load page https://code.google.com/p/chromium/source/search?q=file%3Aeventhandler.cpp > 2, after page full loaded, open Developer Tools, click Resources Panel > 3, 'BF4DD16647C553D78FAAED29C99B5174.cache.js' is not available in Frames->search->cs_frame->Scripts. while, it's been loaded. That's working as intended. Inspector does not intend to hold the resources that would otherwise be collected until its front-end is opened. Fixing this will classify as a memory leak.
Pavel Feldman
Comment 13 2012-12-14 00:58:13 PST
Comment on attachment 179253 [details] Patch Clearing r? from the closed bug.
pdeng6
Comment 14 2012-12-14 01:37:15 PST
(In reply to comment #12) > (In reply to comment #0) > > Created an attachment (id=175160) [details] [details] > > This pic shows expected result, BF4DD16647C553D78FAAED29C99B5174.cache.js available. > > > > How to Reproduce > > 1, Launch chromium, and load page https://code.google.com/p/chromium/source/search?q=file%3Aeventhandler.cpp > > 2, after page full loaded, open Developer Tools, click Resources Panel > > 3, 'BF4DD16647C553D78FAAED29C99B5174.cache.js' is not available in Frames->search->cs_frame->Scripts. while, it's been loaded. > That's working as intended. Inspector does not intend to hold the resources that would otherwise be collected until its front-end is opened. Pavel, thanks for your review :) Previous workaround: CachedResourceHandle may be removed from CachedResourceLoader even its CachedResource will be still in memcache, the iniatior of gc this is a timer of CachedResourceLoader, not really GC, I thinks it is not easy to understand even resources can be removed after that. WeakHandle solution: WeakCachedResourceHandle stored in CachedResourceLoader, their resources can be removed by really GC(GCed resource will notify its weakHandles 'die'). when inspector request resources from CachedResourceLoader, WeakCachedResourceHandle will be checked if "died" or not, if it's died, it won't be represented in front-end. that's to say, inspecotr represents 'resource' accroding to the resource live or not. any problem? >Fixing this will classify as a memory leak. I don't understand this, do you mean WeakCachedResourceHandle is leak?
Pavel Feldman
Comment 15 2012-12-14 01:57:18 PST
I see. In that case, it should be formulated as a more generic issue. Now I see that your change makes almost no changes to the inspector, but rather changes loaders. I'd suggest that you change the bug title, its component and reach out to loader infrastructure reviewers for the feedback.
pdeng6
Comment 16 2012-12-14 03:10:57 PST
(In reply to comment #15) > I see. In that case, it should be formulated as a more generic issue. Now I see that your change makes almost no changes to the inspector, but rather changes loaders. I'd suggest that you change the bug title, its component and reach out to loader infrastructure reviewers for the feedback. thanks for your suggestion:)
pdeng6
Comment 17 2012-12-14 03:59:18 PST
Currently, Strong referred CachedResourceHandles are stored in CachedResourceLoader, so corresponding CachedResources won’t be GCed unless the strong handles are removed. As a workaround, a timer is used in CachedResourceLoader, it will do the GC job, remove CachedResourceHandle which is only-handle one, and try to release its resource, even there is no GC need. This solution did solve memory leak problem, while, generally, I think the initiator of GC should be MemoryCache rather than timer in CachedResourceLoader. Furthermore, web inspector is a victim. Sometimes resource of a simple page won’t be presented in ResourcePanel, because the timer fired and removed the harmless CachedResourceHandle, although the resource still lives in MemoryCache. In this patch, WeakCachedResourceHandles are stored in CachedResourceLoader, timer is removed, MemoryCache init the GC, and living resource will be presented in web inspector. @Antti @japhet, could you please help review this patch? :) Thanks! Pan 
pdeng6
Comment 18 2013-02-28 06:46:12 PST
pdeng6
Comment 19 2013-02-28 06:48:24 PST
Comment on attachment 190715 [details] Patch Introduce WeakPtr in WTF to update this patch.
Adam Barth
Comment 20 2013-02-28 11:37:39 PST
Comment on attachment 190715 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=190715&action=review > Source/WTF/wtf/WeakPtr.h:118 > + // It somewhat breaks the type system, however to allow transfer of ownership > + // out of a const WeakPtrFactory is really a need. > + WeakPtrFactory<T>(const WeakPtrFactory<T>& factory) : m_ref(factory.leakRef()) { } I don't think we want to break the type system in this way.
pdeng6
Comment 21 2013-03-01 00:08:34 PST
pdeng6
Comment 22 2013-03-01 00:13:18 PST
(In reply to comment #20) > (From update of attachment 190715 [details]) > View in context: https://bugs.webkit.org/attachment.cgi?id=190715&action=review > > > Source/WTF/wtf/WeakPtr.h:118 > > + // It somewhat breaks the type system, however to allow transfer of ownership > > + // out of a const WeakPtrFactory is really a need. > > + WeakPtrFactory<T>(const WeakPtrFactory<T>& factory) : m_ref(factory.leakRef()) { } > > I don't think we want to break the type system in this way. Thanks, a "transfer" method added to avoid this. how do you think of this? :) Pan
Adam Barth
Comment 23 2013-03-01 09:42:15 PST
Comment on attachment 190896 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=190896&action=review This patch makes the lifetime of CachedResources too complicated. WeakPtr is a new concept in WebCore. One of the risks of introducing WeakPtr.h into the project is that folks will use it to build complex ownership relationships. We avoided adding it for a long to for just that reason. Now that we have it, we're planning to use it sparingly, at first only for cross-thread communication where RefPtr and OwnPtr are either inappropriate or lead to worse problems. Overall, this patch feels like a band-aid that layers on more complexity instead of rationalizing the ownership semantics of CachedResources. > Source/WTF/wtf/WeakPtr.h:72 > + void reset(T* ptr) > + { > +#ifndef NDEBUG > + m_boundThread = currentThread(); > +#endif > + m_ptr = ptr; > + } If we're going to write m_ptr, we need to assert that we're on the bound thread. > Source/WTF/wtf/WeakPtr.h:120 > + m_ref = WeakReference<T>::create(0); ditto
pdeng6
Comment 24 2013-03-05 00:00:50 PST
pdeng6
Comment 25 2013-03-05 00:05:55 PST
(In reply to comment #23) > (From update of attachment 190896 [details]) > View in context: https://bugs.webkit.org/attachment.cgi?id=190896&action=review > > This patch makes the lifetime of CachedResources too complicated. > > WeakPtr is a new concept in WebCore. One of the risks of introducing WeakPtr.h into the project is that folks will use it to build complex ownership relationships. We avoided adding it for a long to for just that reason. Now that we have it, we're planning to use it sparingly, at first only for cross-thread communication where RefPtr and OwnPtr are either inappropriate or lead to worse problems. > > Overall, this patch feels like a band-aid that layers on more complexity instead of rationalizing the ownership semantics of CachedResources. Thanks for your comments. This time, CachedResourceLoader is notified by SubresourceLoader to reset weakPtr when 304 comes, then ownership of WeakFactory and lifetime of CachedResource won't be hurt. :) Pan
Adam Barth
Comment 26 2013-03-05 15:24:58 PST
Comment on attachment 191417 [details] Patch I don't think we should use WeakPtr in the loader yet. The loader's lifetime management is already very complex and error-prone. We don't have enough experience with WeakPtr as a project yet to make good decisions about how to use WeakPtr in such complex environments.
Note You need to log in before you can comment on or make changes to this bug.