Open Bug 1184729 Opened 9 years ago Updated 2 years ago

Convert Dispatch/DispatchToMainThread/PutEvent uses to already_AddRefed<>

Categories

(Core :: XPCOM, defect)

defect

Tracking

()

ASSIGNED
Tracking Status
firefox42 --- affected

People

(Reporter: jesup, Assigned: jesup)

References

(Depends on 2 open bugs, Blocks 1 open bug)

Details

(Keywords: leave-open)

Attachments

(26 files, 27 obsolete files)

1.35 KB, patch
Details | Diff | Splinter Review
5.03 KB, patch
Details | Diff | Splinter Review
1.42 KB, patch
Details | Diff | Splinter Review
2.32 KB, patch
Details | Diff | Splinter Review
9.01 KB, patch
Details | Diff | Splinter Review
9.59 KB, patch
Details | Diff | Splinter Review
4.08 KB, patch
Details | Diff | Splinter Review
5.48 KB, patch
Details | Diff | Splinter Review
2.57 KB, patch
Details | Diff | Splinter Review
3.05 KB, patch
Details | Diff | Splinter Review
40.21 KB, patch
Details | Diff | Splinter Review
42.22 KB, patch
Details | Diff | Splinter Review
6.15 KB, patch
Details | Diff | Splinter Review
61.72 KB, patch
Details | Diff | Splinter Review
15.96 KB, patch
Details | Diff | Splinter Review
2.08 KB, patch
Details | Diff | Splinter Review
15.66 KB, patch
Details | Diff | Splinter Review
2.47 KB, patch
Details | Diff | Splinter Review
6.72 KB, patch
Details | Diff | Splinter Review
14.78 KB, patch
Details | Diff | Splinter Review
20.37 KB, patch
Details | Diff | Splinter Review
3.03 KB, patch
Details | Diff | Splinter Review
50.89 KB, patch
Details | Diff | Splinter Review
11.16 KB, patch
Details | Diff | Splinter Review
6.84 KB, patch
Details | Diff | Splinter Review
5.06 KB, patch
Details | Diff | Splinter Review
Followup to bug 1155059, now that it's stably on central. This is to convert the rest of the tree (in a series of patches) to use the new/safer API for Dispatch()/DispatchToMainThread() (and PutEvent() if needed). DispatchToCurrentThread() doesn't need to be updated for safety, but changing those to already_AddRefed<> will save extra trips through AddRef/Release. As part of this, we may want to allow (per bug 1183651's discussion) DispatchToMainThread(new Foo(...)) (and Dispatch(new ...))), if we can provide a static assert to block people passing a smart pointer to them. (This would have to land after the tree is converted over, instead of at that time removing Dispatch(raw_ptr...)). I plan to land these as a series of patches to different parts of the tree. Help is welcome! Common conversions include: ========================== nsCOMPtr<nsIRunnable> foo = new bar(..); DispatchToMainThread(foo); -> DispatchToMainThread(do_AddRef(new bar(...))); or (with static assertions) DispatchToMainThread(new bar(...)); Ditto for nsRefPtr<bar> foo = new bar(...); DispatchToMainThread(this); -> DispatchToMainThread(do_AddRef(this)); An unusual one: nsRefPtr<bar> foo = new bar(...); DispatchToMainThread(foo); return foo.forget().take(); -> nsRefPtr<bar> foo = new bar(...); DispatchToMainThread(do_AddRef(foo)); return foo.forget().take(); nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(...); DispatchToMainThread(event); -> nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(...); DispatchToMainThread(event.forget()); (unless I convert NS_NewRunnableMethod() to return already_AddRefed<>, which would be best but seemed a little tricky.) or maybe: (with static_asserts) DisaptchToMainThread(NS_NewRunnableMethod(...)); Also, converting WrapRunnable() to return already_AddRefed<>, and RUN_ON_THREAD() to take it.
This patch blocks calling NS_DispatchToMainThread with any type of smart pointer. I tested this patch on MSVC and Clang, and this patch works as expected on both of them. (reports "attempting to reference a deleted function" / "call to deleted function")
I have done some work on this, so steal this bug. Hopefully you don't mind that. I see another interesting thing, which is the function NS_ProxyRelease. Many places use this function to ensure some object is only released in the main thread. However, they passed in smart pointers, which are implicitly converted to the raw pointer. We probably need to do the same thing for that function.
Assignee: rjesup → quanxunzhen
I'm fine with that; I'll upload my partial patches. Thanks! Regarding nsProxyRelease - yes, that should get rewritten.
BTW, this is currently safe *only* because the event doesn't actually own the reference and instead uses mDoomed->Release() inside of Run(). Since we need to release an arbitrary smartptr (nsRefPtr/nsCOMPtr/RefPtr), it may make sense to keep this part of the impl. nsCOMPtr<nsIRunnable> ev = new nsProxyReleaseEvent(aDoomed); if (!ev) { // we do not release aDoomed here since it may cause a delete on the // wrong thread. better to leak than crash. return NS_ERROR_OUT_OF_MEMORY; } rv = aTarget->Dispatch(ev, NS_DISPATCH_NORMAL); Here we could be deleting ev either on aTarget or on the current thread
Attached patch DispatchToCurrentThread (obsolete) — Splinter Review
Assignee: quanxunzhen → rjesup
Status: NEW → ASSIGNED
Attached patch SyncRunnable (obsolete) — Splinter Review
Attached patch WrapRunnable changes (obsolete) — Splinter Review
Attached patch media/webrtc changes (obsolete) — Splinter Review
Attached patch media/mtransport changes (obsolete) — Splinter Review
Attached patch dom/asmjscache (obsolete) — Splinter Review
Attached patch Possible bugs in dom/* (obsolete) — Splinter Review
Attached patch dom/workers (obsolete) — Splinter Review
Attached patch dom/various (obsolete) — Splinter Review
Attached patch dom/camera (obsolete) — Splinter Review
Attached patch dom/bluetooth (obsolete) — Splinter Review
Attached patch dom/ipc (obsolete) — Splinter Review
NOTE: these were all done assuming we didn't have the static assertions to rely on, so they convert DispatchToMainThread/Dispatch(new foo(...)) to DispatchToMainThread/Dispatch(do_AddRef(new foo(...))). This can be left or it could easily be trimmed back to drop the AddRef.
silly bzexport mixing "export a patch" with "steal the bug". At most it should do it only for bugs owned by nobody
Assignee: rjesup → quanxunzhen
Attached patch dom/events (obsolete) — Splinter Review
Attached patch dom/cache (obsolete) — Splinter Review
Attached patch dom/filesystem (obsolete) — Splinter Review
Attached patch dom/storage (obsolete) — Splinter Review
FYI, I have patches for dom/media that I'll upload as soon as I make sure they compile
Attached patch dom/ipc (obsolete) — Splinter Review
Attachment #8635948 - Attachment is obsolete: true
Attached patch dom/cache (obsolete) — Splinter Review
Attachment #8635977 - Attachment is obsolete: true
Attached patch dom/media/gmp (obsolete) — Splinter Review
Attached patch dom/media/webrtc (obsolete) — Splinter Review
Attached patch dom/media/webspeech (obsolete) — Splinter Review
Assignee: quanxunzhen → rjesup
Attached patch dom/media/mediasource (obsolete) — Splinter Review
Attached patch dom/media/eme (obsolete) — Splinter Review
https://treeherder.mozilla.org/#/jobs?repo=try&revision=8f951c3e394e Ok, all the patches I had have been dumped here. Builds on linux and runs. Try in progress. Note one additional pattern: nsRefPtr<FooRunnable> foo = new FooRunnable(...); thread->Dispatch(foo, ...) return foo.forget(); // or otherwise mess with foo, or foo is an mFoo member, etc -> nsRefPtr<FooRunnable> foo = new FooRunnable(...); thread->Dispatch(do_AddRef(foo.get()), ...); return foo.forget(); Note also the compile-time blocking of DispatchToMainThread() xidorn did needs to be extended to Dispatch() (and all the overloads thereof)
Blocks: 1186012
Depends on: 1186745
Depends on: 1186750
Note: I have updated patches for many of these after some try runs (and no surprise, these have already been bit-rotted, and will be again). I'll probably refresh all of these shortly
Comment on attachment 8635935 [details] [diff] [review] DispatchToCurrentThread Handled now in blocking bug 1186745
Attachment #8635935 - Attachment is obsolete: true
Attachment #8635936 - Attachment is obsolete: true
Attachment #8635937 - Attachment is obsolete: true
Attachment #8635938 - Attachment is obsolete: true
Attachment #8635939 - Attachment is obsolete: true
Attachment #8635940 - Attachment is obsolete: true
Attachment #8635941 - Attachment is obsolete: true
Attachment #8635942 - Attachment is obsolete: true
Attachment #8635943 - Attachment is obsolete: true
Attachment #8635944 - Attachment is obsolete: true
Attachment #8635945 - Attachment is obsolete: true
Attachment #8635946 - Attachment is obsolete: true
Attachment #8635947 - Attachment is obsolete: true
Attached patch dom/ipcSplinter Review
Attachment #8636578 - Attachment is obsolete: true
Attachment #8635976 - Attachment is obsolete: true
Attachment #8636579 - Attachment is obsolete: true
Attachment #8635978 - Attachment is obsolete: true
Attachment #8635979 - Attachment is obsolete: true
Attachment #8636581 - Attachment is obsolete: true
Attachment #8636589 - Attachment is obsolete: true
Attachment #8636582 - Attachment is obsolete: true
Attachment #8636583 - Attachment is obsolete: true
Attachment #8636585 - Attachment is obsolete: true
Attachment #8636586 - Attachment is obsolete: true
Attachment #8636587 - Attachment is obsolete: true
Local mochitest runs no longer leak; I have some tries running larger sets of mochitests to verify. Apply in the order in the bug, though very few are if any are directly dependent on other patches.
Looks like https://treeherder.mozilla.org/#/jobs?repo=try&revision=f10a2fd5d4c7 does not leak, and includes the media/mtransport changes, but not any of the DOM changes. https://treeherder.mozilla.org/#/jobs?repo=try&revision=3d016b61b1ac which has through dom/various does leak
Depends on: 1226073
Depends on: 1237213
The leave-open keyword is there and there is no activity for 6 months. :jesup, maybe it's time to close this bug?
Flags: needinfo?(rjesup)
Flags: needinfo?(rjesup)

The leave-open keyword is there and there is no activity for 6 months.
:jesup, maybe it's time to close this bug?

Flags: needinfo?(rjesup)

The leave-open keyword is there and there is no activity for 6 months.
:jesup, maybe it's time to close this bug?

Flags: needinfo?(rjesup)

The leave-open keyword is there and there is no activity for 6 months.
:jesup, maybe it's time to close this bug?

Flags: needinfo?(rjesup)
Flags: needinfo?(rjesup)

The leave-open keyword is there and there is no activity for 6 months.
:jesup, maybe it's time to close this bug?

Flags: needinfo?(rjesup)
Flags: needinfo?(rjesup)

The leave-open keyword is there and there is no activity for 6 months.
:jesup, maybe it's time to close this bug?

Flags: needinfo?(rjesup)
Flags: needinfo?(rjesup)

The leave-open keyword is there and there is no activity for 6 months.
:jesup, maybe it's time to close this bug?
For more information, please visit auto_nag documentation.

Flags: needinfo?(rjesup)

The leave-open keyword is there and there is no activity for 6 months.
:jesup, maybe it's time to close this bug?
For more information, please visit auto_nag documentation.

Flags: needinfo?(rjesup)
Severity: normal → S3
Flags: needinfo?(rjesup)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: