Closed Bug 1634846 Opened 5 years ago Closed 4 years ago

MessageChannel can't work with TaskQueue

Categories

(Core :: IPC, enhancement)

enhancement

Tracking

()

RESOLVED FIXED
mozilla80
Tracking Status
firefox80 --- fixed

People

(Reporter: jya, Assigned: jya)

References

(Blocks 2 open bugs, Regression)

Details

(Keywords: regression)

Attachments

(3 files, 3 obsolete files)

MessageChannel::Open takes an nsIEventTarget ;

When opening, it dispatches a task on this nsIEventTarget that calls MessageChannel::OnOpenAsSlave [1]

When OnOpenAsSlave runs, it stores the actual PRThread the task is running on [2]. And then in various places will assert that the current worker thread is the same as the actual thread it's running on [3].

When using a ThreadPool or a TaskQueue, you're never guaranteed that the actual thread the code is running on will always be the same.

So at present, while MessageChannel takes a nsIEventTarget as constructor, this nsIEventTarget can only ever be a nsThread.

[1] https://searchfox.org/mozilla-central/source/ipc/glue/MessageChannel.cpp#874
[2] https://searchfox.org/mozilla-central/source/ipc/glue/MessageChannel.cpp#904
[3] https://searchfox.org/mozilla-central/source/ipc/glue/MessageChannel.h#550

Blocks: 1260828

The severity field is not set for this bug.
:jld, could you have a look please?

For more information, please visit auto_nag documentation.

Flags: needinfo?(jld)

MessageChannel (and IPDL in general) inherently needs a serial event target, so it will never work with ThreadPool. But in principle it (and the associated checks that things are happening on the correct thread) could be generalized to work with abstract threads, like TaskQueue.

Type: defect → enhancement
Flags: needinfo?(jld)
Summary: MessageChannel can't work with ThreadPool nor TaskQueue. → MessageChannel can't work with abstract threads

(In reply to Jed Davis [:jld] ⟨⏰|UTC-6⟩ ⟦he/him⟧ from comment #2)

MessageChannel (and IPDL in general) inherently needs a serial event target, so it will never work with ThreadPool. But in principle it (and the associated checks that things are happening on the correct thread) could be generalized to work with abstract threads, like TaskQueue.

TaskQueue is a nsISerialEventTarget object.

The main issue is what MessageChannel is doing to ensure you're on the right thread; It simply compares pointers of the PR_Thread which is the obviously wrong thing to do.

It could store the original nsISerialEventTarget is use is IsOnThread() method instead.

This would make it work with TaskQueue.

Assignee: nobody → jyavenard
Depends on: 1637500
Blocks: 1647112

(In reply to Jed Davis [:jld] ⟨⏰|UTC-6⟩ ⟦he/him⟧ from comment #2)

MessageChannel (and IPDL in general) inherently needs a serial event target, so it will never work with ThreadPool. But in principle it (and the associated checks that things are happening on the correct thread) could be generalized to work with abstract threads, like TaskQueue.

I should have provided more details about the bug originally mentioning ThreadPool.

This is the only implementation we have that is a nsIEventTarget and not a nsISerialEventTarget ;

The prototyping of IToplevelProtocol methods are to use nsIEventTarget ; which is nonsensical really as it must be a nsISerialEventTarget.

See Also: → 1647133
Summary: MessageChannel can't work with abstract threads → MessageChannel can't work with TaskQueue
Blocks: 1647628

And the less use of MessageLoop, the better.

We now pass the target nsISerialEventTarget all the way down to MessageChannel::CommonThreadOpenInit so that we don't have to rely on GetCurrentSerialEventTarget() to return the proper object (see bug 1637500).

Even when bug 1637500 will be fully fixed, it avoids any ambiguities as to what target thread we want to use.

Depends on D80654

We no longer rely of having a message loop for the worker thread.

Attachment #9158813 - Attachment is obsolete: true
Depends on: 1648031

NS_INLINE_DECL_REFCOUNTING macro doesn't properly work when the object is used on a thread that isn't backed by a single PRThread (such as TaskQueue). See bug 1648031.

The resolution of this issue is rather complex, and outside the scope of this series of change.

So for now, we create a new macro NS_INLINE_DECL_REFCOUNTING_ONEVENTTHREAD which will use a different mechanism to ensure the thread-safe usage of a class.

There's a small race that can happen when the remote decoder gets shutdown during xpcom shutdown; that would cause GetCurrentSerialEventTarget to return null. Leading to an assertion failure in ActorLifecycleProxy thread-safety check when PRemoteDecoderManagerParent gets destroyed.

So we use a background taskqueue instead and cleanup a bit the threading code in there allowed thanks to the TaskQueue ability to not require an explicit shutdown.

The STS dispatched the first event before setting mThread member. It was possible that the STS::Run() task got started before mThread was set and causing one of the nsISerialEventTarget methods to read mThread/mInitialised/mShuttingDown value as null or zero and they would have returned an error.

We rewrite the access to mThread to be lockless.

Depends on: 1648898

Comment on attachment 9159699 [details]
Bug 1634846 - P5. Fix data race on STS loop start. r?valentin,honza

Revision D81334 was moved to bug 1648898. Setting attachment 9159699 [details] to obsolete.

Attachment #9159699 - Attachment is obsolete: true
Blocks: 1649294

Comment on attachment 9159605 [details]
Bug 1634846 - P4. Make RemoteDecoder use a background taskqueue. r?mattwoodrow

Revision D81287 was moved to bug 1649294. Setting attachment 9159605 [details] to obsolete.

Attachment #9159605 - Attachment is obsolete: true
Pushed by jyavenard@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/d539408a0048 P1. Remove unused method. r=nika,jld https://hg.mozilla.org/integration/autoland/rev/bca79526745d P2. Make ipc's MessageChannel works with TaskQueue, r=nika https://hg.mozilla.org/integration/autoland/rev/5863f9c5229f P3. Get around NS_INLINE_DECL_REFCOUNTING not working with TaskQueue. r=nika,froydnj
Flags: needinfo?(jyavenard)
Pushed by jyavenard@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/681e77b6fe1b P1. Remove unused method. r=nika,jld https://hg.mozilla.org/integration/autoland/rev/ffa41c2b08b3 P2. Make ipc's MessageChannel works with TaskQueue, r=nika https://hg.mozilla.org/integration/autoland/rev/132e64564bdd P3. Get around NS_INLINE_DECL_REFCOUNTING not working with TaskQueue. r=nika,froydnj
Has Regression Range: --- → yes
Keywords: regression
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: