Bug 251223 - AX: WebCore::AXIsolatedTree should use WTF::ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr<>.
Summary: AX: WebCore::AXIsolatedTree should use WTF::ThreadSafeRefCountedAndCanMakeThr...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Accessibility (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Andres Gonzalez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2023-01-26 13:05 PST by Andres Gonzalez
Modified: 2023-01-29 07:57 PST (History)
16 users (show)

See Also:


Attachments
Patch (9.98 KB, patch)
2023-01-26 17:11 PST, Andres Gonzalez
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (10.23 KB, patch)
2023-01-26 18:04 PST, Andres Gonzalez
no flags Details | Formatted Diff | Diff
Patch (9.31 KB, patch)
2023-01-27 14:28 PST, Andres Gonzalez
no flags Details | Formatted Diff | Diff
Patch (9.34 KB, patch)
2023-01-27 14:55 PST, Andres Gonzalez
no flags Details | Formatted Diff | Diff
Patch (8.94 KB, patch)
2023-01-28 12:12 PST, Andres Gonzalez
no flags Details | Formatted Diff | Diff
Patch (8.95 KB, patch)
2023-01-28 19:25 PST, Andres Gonzalez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andres Gonzalez 2023-01-26 13:05:09 PST
Currently it derives from ThreadSafeRefCounted and CanMakeWeakPtr.
Comment 1 Radar WebKit Bug Importer 2023-01-26 13:05:22 PST
<rdar://problem/104710054>
Comment 2 Andres Gonzalez 2023-01-26 13:06:07 PST
rdar://104512153
Comment 3 Andres Gonzalez 2023-01-26 17:11:25 PST
Created attachment 464679 [details]
Patch
Comment 4 Andres Gonzalez 2023-01-26 18:04:15 PST
Created attachment 464681 [details]
Patch
Comment 5 Tyler Wilcock 2023-01-26 18:17:05 PST
Comment on attachment 464681 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=464681&action=review

> COMMIT_MESSAGE:8
> +In order to have a single AXTreeStore that keeps both WeakPtr<AXObjectCache> and ThreadSafeWeakPtr<AXIsolatedTree>, we added the variant AXTreePtr. Note that this avoids having a ThreadSafeWeakPtr<AXObjectCache> which would be an overkill since AXObjectCache is only used on the main thread.

Typo — "which would be an overkill" should be "which would be overkill"

> Source/WebCore/accessibility/isolatedtree/AXIsolatedTree.cpp:780
> -        if (auto tree = AXTreeStore::treeForID(treeID())) {
> +        if (auto* tree = AXTreeStore::isolatedTreeForID(treeID())) {
>              // At this point, there should only be two references left to this tree -- one in the map,
>              // and the `protectedThis` above.
> -            ASSERT(tree->refCount() == 2, "Unexpected refcount before attempting to destroy isolated tree: %d", tree->refCount());
> +            ASSERT(tree->strongRefCount() == 2, "Unexpected refcount before attempting to destroy isolated tree: %d", tree->strongRefCount());

Correct me if I'm wrong, but with this patch we can now make a ThreadSafeWeakPtr<AXIsolatedTree>. That means we can change AXIsolatedObject::m_cachedTree to be a ThreadSafeWeakPtr<AXIsolatedTree> and remove this whole block (and therefore not add a new strongRefCount method).
Comment 6 Alex Christensen 2023-01-26 19:04:45 PST
Comment on attachment 464681 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=464681&action=review

> Source/WTF/wtf/ThreadSafeWeakPtr.h:104
> +    size_t strongRefCount() const

I don't think we should add this.  The value returned is immediately possibly incorrect.  If we do add it, it should be inside ASSERT_ENABLED so we only use it for assertions.
Comment 7 Andres Gonzalez 2023-01-27 14:28:59 PST
Created attachment 464691 [details]
Patch
Comment 8 Andres Gonzalez 2023-01-27 14:36:34 PST
(In reply to Tyler Wilcock from comment #5)
> Comment on attachment 464681 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=464681&action=review
> 
> > COMMIT_MESSAGE:8
> > +In order to have a single AXTreeStore that keeps both WeakPtr<AXObjectCache> and ThreadSafeWeakPtr<AXIsolatedTree>, we added the variant AXTreePtr. Note that this avoids having a ThreadSafeWeakPtr<AXObjectCache> which would be an overkill since AXObjectCache is only used on the main thread.
> 
> Typo — "which would be an overkill" should be "which would be overkill"

Rephrased, hopefully it is clearer and correct.
> 
> > Source/WebCore/accessibility/isolatedtree/AXIsolatedTree.cpp:780
> > -        if (auto tree = AXTreeStore::treeForID(treeID())) {
> > +        if (auto* tree = AXTreeStore::isolatedTreeForID(treeID())) {
> >              // At this point, there should only be two references left to this tree -- one in the map,
> >              // and the `protectedThis` above.
> > -            ASSERT(tree->refCount() == 2, "Unexpected refcount before attempting to destroy isolated tree: %d", tree->refCount());
> > +            ASSERT(tree->strongRefCount() == 2, "Unexpected refcount before attempting to destroy isolated tree: %d", tree->strongRefCount());
> 
> Correct me if I'm wrong, but with this patch we can now make a
> ThreadSafeWeakPtr<AXIsolatedTree>. That means we can change
> AXIsolatedObject::m_cachedTree to be a ThreadSafeWeakPtr<AXIsolatedTree> and
> remove this whole block (and therefore not add a new strongRefCount method).

Upon further investigation and discussion, we agreed in removing the ASSERT to check the reference count, and thus the need to add strongRefCount(), but not changing the strong reference to the Isolated tree in the object.
Comment 9 Alex Christensen 2023-01-27 14:38:17 PST
Comment on attachment 464691 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=464691&action=review

> Source/WebCore/accessibility/AXTreeStore.h:136
> +        [] (ThreadSafeWeakPtr<AXIsolatedTree>& typedTree) -> AXIsolatedTree* { return typedTree.get().get(); },

This needs to return a RefPtr<AXIsolatedTree> otherwise it might immediately be deleted by another thread.
Comment 10 Andres Gonzalez 2023-01-27 14:55:03 PST
Created attachment 464694 [details]
Patch
Comment 11 Andres Gonzalez 2023-01-27 14:56:48 PST
(In reply to Alex Christensen from comment #9)
> Comment on attachment 464691 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=464691&action=review
> 
> > Source/WebCore/accessibility/AXTreeStore.h:136
> > +        [] (ThreadSafeWeakPtr<AXIsolatedTree>& typedTree) -> AXIsolatedTree* { return typedTree.get().get(); },
> 
> This needs to return a RefPtr<AXIsolatedTree> otherwise it might immediately
> be deleted by another thread.

Fixed. Thanks.
Comment 12 Alex Christensen 2023-01-27 16:15:52 PST
Comment on attachment 464694 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=464694&action=review

> Source/WebCore/accessibility/AXTreeStore.h:76
> +        [&] (WeakPtr<AXObjectCache>& typedTree) {

This looks unnecessary.

> Source/WebCore/accessibility/AXTreeStore.h:85
> +        [&] (ThreadSafeWeakPtr<AXIsolatedTree>& typedTree) {

ditto

> Source/WebCore/accessibility/AXTreeStore.h:94
> +        [] (auto&) {

ditto
Comment 13 Alex Christensen 2023-01-27 16:16:02 PST
Close!
Comment 14 Andres Gonzalez 2023-01-28 12:12:54 PST
Created attachment 464704 [details]
Patch
Comment 15 Andres Gonzalez 2023-01-28 12:15:33 PST
(In reply to Alex Christensen from comment #13)
> Close!

(In reply to Alex Christensen from comment #12)
> Comment on attachment 464694 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=464694&action=review
> 
> > Source/WebCore/accessibility/AXTreeStore.h:76
> > +        [&] (WeakPtr<AXObjectCache>& typedTree) {
> 
> This looks unnecessary.

Fixed.
> 
> > Source/WebCore/accessibility/AXTreeStore.h:85
> > +        [&] (ThreadSafeWeakPtr<AXIsolatedTree>& typedTree) {
> 
> ditto

Fixed.
> 
> > Source/WebCore/accessibility/AXTreeStore.h:94
> > +        [] (auto&) {
> 
> ditto

Fixed. Thanks!
Comment 16 Alex Christensen 2023-01-28 15:28:06 PST
Comment on attachment 464704 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=464704&action=review

> Source/WebCore/accessibility/AXTreeStore.h:79
> +        },

This comma needs to be inside ENABLE(ACCESSIBILITY_ISOLATED_TREE)
Comment 17 Andres Gonzalez 2023-01-28 19:25:13 PST
Created attachment 464740 [details]
Patch
Comment 18 EWS 2023-01-29 07:57:53 PST
Committed 259536@main (8f8d02ddc94e): <https://commits.webkit.org/259536@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 464740 [details].