Open Bug 1712784 Opened 4 years ago Updated 2 years ago

Creating GeckoSessions and loading URLs from non-main threads

Categories

(GeckoView :: General, enhancement, P3)

Unspecified
All
enhancement

Tracking

(Performance Impact:medium)

Performance Impact medium

People

(Reporter: sebastian, Unassigned)

References

Details

(Keywords: perf:pageload, Whiteboard: [geckoview:m98?])

A-C does all its state transitions on a separate thread. Whenever a state change causes a follow-up action in Gecko(View), we need to switch to the main thread to perform this (e.g. creating a GeckoSession or calling loadUri() on an existing session.). This adds quite some complexity to our code base (context switches, synchronization, deadlock prevention, preventing double dispatching). We could reduce a large part of this complexity if we could call those GeckoView APIs from a background thread.

Currently those fail with the following exceptions:

Creating a GeckoSession on a background thread:

E  Caused by: java.lang.IllegalThreadStateException: Expected thread 2 ("main"), but running on thread 51 ("BrowserStore-thread-1")
E      at org.mozilla.gecko.util.ThreadUtils.assertOnThreadComparison(ThreadUtils.java:110)
E      at org.mozilla.gecko.util.ThreadUtils.assertOnThread(ThreadUtils.java:86)
E      at org.mozilla.gecko.util.ThreadUtils.assertOnUiThread(ThreadUtils.java:77)
E      at org.mozilla.geckoview.GeckoSession.getSurfaceBounds(GeckoSession.java:5393)
E      at org.mozilla.geckoview.Autofill$Support.getDummyAutofillRect(Autofill.java:1221)
E      at org.mozilla.geckoview.Autofill$Session.getDefaultDimensions(Autofill.java:229)
E      at org.mozilla.geckoview.Autofill$Session.clear(Autofill.java:237)
E      at org.mozilla.geckoview.Autofill$Session.<init>(Autofill.java:223)
E      at org.mozilla.geckoview.Autofill$Support.<init>(Autofill.java:975)
E      at org.mozilla.geckoview.GeckoSession.<init>(GeckoSession.java:1218)
[..]

Loading a URI on a background thread:

E  Caused by: java.lang.IllegalThreadStateException: Must have a Handler
E      at org.mozilla.geckoview.GeckoResult.then(GeckoResult.java:534)
E      at org.mozilla.geckoview.GeckoResult.accept(GeckoResult.java:494)
E      at org.mozilla.geckoview.GeckoResult.getOrAccept(GeckoResult.java:513)
E      at org.mozilla.geckoview.GeckoResult.getOrAccept(GeckoResult.java:498)
E      at org.mozilla.geckoview.GeckoSession.load(GeckoSession.java:1765)
E      at mozilla.components.browser.engine.gecko.GeckoEngineSession.loadUrl(GeckoEngineSession.kt:174)

I briefly discussed this with Agi before his PTO. Filling this to continue the discussing and opening it up.

Severity: -- → S3
Priority: -- → P3

We ran again into an issue where we wanted to optimize the load of a URL by calling into GeckoView earlier. However the context switches made this quite hard to do.

@agi: I remember back when I filed this issue, you looked into this. Do you think being able to load URLs from other threads would be possible?

Flags: needinfo?(agi)

I looked at this again, and it should be possible to do.

I have a question though, do you also need to set delegates in the thread, or just create the session? Setting the delegates in a thread might be a little more complicated because of synchronization and stuff like that.

Flags: needinfo?(agi) → needinfo?(s.kaspari)
Depends on: 1737824

I see, yeah loading URLs should be the easiest thing to do here, let's start with that.

Whiteboard: [qf] → [qf:p2:pageload]
Whiteboard: [qf:p2:pageload] → [qf:p2:pageload][geckoview:m98?]
Performance Impact: --- → P2
Keywords: perf:pageload
Whiteboard: [qf:p2:pageload][geckoview:m98?] → [geckoview:m98?]

Tasks and enhancements should have severity N/A.

Severity: S3 → N/A
You need to log in before you can comment on or make changes to this bug.