Repository navigation
Unify the task source and task canceller API - #21804
Conversation
|
Heads up! This PR modifies the following files:
|
|
Thanks! I will look at this later this week! |
|
Is it just me, or is the Should we perhaps remove the cc @cbrewster @jdm |
| } | ||
| } | ||
|
|
||
| pub struct TaskManagement<T: TaskSource>(pub T, pub TaskCanceller); |
There was a problem hiding this comment.
It'd be possible to unify *_task_source in such a way that we would
have only one method.
Have you considered making this a struct that would completely take over the task management from a Window?
Perhaps something like:
struct TaskManager {
task_cancellers: DomRefCell<HashMap<TaskSourceName, Arc<AtomicBool>>>,
task_sources: DomRefCell<HashMap<TaskSourceName, Box<TaskSource>>>
}
impl TaskManager {
fn new(task_sources: HashMap<TaskSourceName, Box<TaskSource>>) -> Self;
fn task_source_with_canceller(&self, name: TaskSourceName) -> (Box<TaskSource>, TaskCanceller);
fn task_source(&self, name: TaskSourceName)-> Box<TaskSource>;
fn task_canceller(&self, name: TaskSourceName)-> TaskCanceller;
}Perhaps it could be initialized when a new page loads, passed to Window::new, and basically replace all the task source related methods and fields on Window with one public task_manager field?
one of the
TaskSource implementations is special:
history_traversal_task_source. Not wanting to over complicate things,
I decided to leave the structure this way.
Perhaps if you were to implement TaskSource for HistoryTraversalTaskSource(or remove it if others confirm this is a good idea, see my other comment), it could work without requiring too much complication?
There was a problem hiding this comment.
I thought of that, but not knowing what to do with HistoryTraversalTaskSource made me chose something simpler. Truth being, I'd love to move responsibilities outside of Window (it's the most overwhelming file in the source code) :). Your proposal of adding a "fake" implementation for HistoryTraversalTaskSource sounds good to me and addresses the concern I had. Will address your feedback and push the new version in the next days.
Thanks for the suggestion.
There was a problem hiding this comment.
Sounds good, you could also do an actually 'real' implementation, since we might actually need it later for #21815
I think basically a copy/paste from any of the other task-source, and adding a new ScriptThreadEventCategory, should do the trick...
There was a problem hiding this comment.
Unfortunately, TaskSource can't be made a trait object :(.
I do have the HistoryTraversalTaskSource implementation. Should I just do that? Or maybe we can somehow modify TaskSource to allow it to make a trait object?
There was a problem hiding this comment.
I guess I could also make a less elegant...
struct TaskManager {
task_cancellers: DomRefCell<HashMap<TaskSourceName, Arc<AtomicBool>>>,
*_task_sources: DomRefCell<*TaskSource>,
}
Although we would still have a lot of methods.
There was a problem hiding this comment.
Of course! Here it's:
error[E0038]: the trait `task_source::TaskSource` cannot be made into an object
--> components/script/task_manager.rs:119:5
|
119 | pub fn task_source(&self, name: TaskSourceName) -> Box<TaskSource> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `task_source::TaskSource` cannot be made into an object
|
= note: method `queue_with_canceller` has generic type parameters
= note: method `queue` has generic type parameters
= note: the trait cannot contain associated consts like `NAME
There was a problem hiding this comment.
I see, so that's not going to work :)
I haven't found another way to avoid the repetition of methods, have you?
In any case, introducing a TaskManager that will lighten up Window on methods and fields is probably progress.
Thanks for pushing to additional commits, I will review them as is unless we come up with a better design...
There was a problem hiding this comment.
I tried to trick the compiler in a couple naive ways (i.e. using & mut instead of Box, wrapping the TaskSource in a structure that is object safe, etc.). As expected, the compiler outsmarted me.
Another thing I considered was just living with the duplication, but create a couple macros so it's less apparent. I didn't want to go through that route, because I didn't see any macro in the rest of the code and I know some projects are (rightfully) cautious about them. If this were a personal project, though, and the choice depended only on me, I'd probably do that.
There was a problem hiding this comment.
We do use macros when it makes sense in Servo's codebase.
There was a problem hiding this comment.
Good to know! Will push (yet another) commit with how it would look like with macros. Thanks!
|
We don’t use the history task source, I tried using it so we would line up with the spec a bit more, but it ends up queuing a message on the task source to send a message to the constellation, which doesn’t make much sense. I would be okay with removing the history task source, but it really depends on how much we care about history spec compliance. |
|
@cbrewster Thanks for the info! I'll open a separate issue to discuss this... @AgustinCB I think in the context of this PR, we could implement a EDIT: I think an actual task source implementation similar to the others is probably better, since I just realized the history-traversal task queue and task source aren't completely related, see #21815 Could please do this implementation of |
No Taskcluster jobs started for this pull requestThe `allowPullRequests` configuration for this repository (in `.taskcluster.yml` on the
default branch) does not allow starting tasks for this pull request. |
fe67621 to
abe1a44
Compare
No Taskcluster jobs started for this pull requestThe `allowPullRequests` configuration for this repository (in `.taskcluster.yml` on the
default branch) does not allow starting tasks for this pull request. |
abe1a44 to
abdcefa
Compare
gterzian
left a comment
There was a problem hiding this comment.
Looks great! Just a few nits really
| base_context.set_state_attribute(AudioContextState::Suspended); | ||
| let window = DomRoot::downcast::<Window>(context.global()).unwrap(); | ||
| window.dom_manipulation_task_source().queue_simple_event( | ||
| window.task_manager().dom_manipulation_task_source().queue_simple_event( |
There was a problem hiding this comment.
nit: I'm wondering if task_source could be used instead here.
There was a problem hiding this comment.
Not really, since task_source is borrowed in line 131.
| base_context.set_state_attribute(AudioContextState::Closed); | ||
| let window = DomRoot::downcast::<Window>(context.global()).unwrap(); | ||
| window.dom_manipulation_task_source().queue_simple_event( | ||
| window.task_manager().dom_manipulation_task_source().queue_simple_event( |
There was a problem hiding this comment.
In here, it's borrowed in line 193.
| let global = this.global(); | ||
| let window = global.as_window(); | ||
| window.dom_manipulation_task_source().queue_simple_event( | ||
| window.task_manager().dom_manipulation_task_source().queue_simple_event( |
| this.state.set(AudioContextState::Running); | ||
| let window = DomRoot::downcast::<Window>(this.global()).unwrap(); | ||
| window.dom_manipulation_task_source().queue_simple_event( | ||
| window.task_manager().dom_manipulation_task_source().queue_simple_event( |
There was a problem hiding this comment.
It's also borrowed before.
To be fair, this parts also bothered me. I suspect that there's some way in which this code can be refactor to call less clones. But I'm not sure it's immediately obvious.
Maybe I can offer you to open an issue to track the tech debt?
There was a problem hiding this comment.
Ok, thanks for the info. I'm not sure at this point how to make a potential issue actionable, so I will not open one. If you happen to have a clearer idea, it could be useful if you were to open one indeed...
| win.dom_manipulation_task_source() | ||
| .queue_simple_event(target, atom!("close"), &win); | ||
| win.task_manager().dom_manipulation_task_source() | ||
| .queue_simple_event(target, atom!("close"), &win); |
There was a problem hiding this comment.
nit: This is what rustfmt did with it(actually it appears the indent here is slightly different, the idea is 4 space ident I believe, starting at win.task_manager()):
// Step 5
win.task_manager()
.dom_manipulation_task_source()
.queue_simple_event(target, atom!("close"), &win);| let context = Arc::new(Mutex::new(HTMLMediaElementContext::new(self))); | ||
| let (action_sender, action_receiver) = ipc::channel().unwrap(); | ||
| let window = window_from_node(self); | ||
| let (task_source, canceller) = document.window().task_manager().networking_task_source_with_canceller(); |
There was a problem hiding this comment.
nit: this one looks a little too long(you can check with ./mach test-tidy)
| self.task_canceller(TaskSourceName::DOMManipulation) | ||
| .wrap_task(task), | ||
| self.task_manager.task_canceller(TaskSourceName::DOMManipulation) | ||
| .wrap_task(task), |
There was a problem hiding this comment.
nit: looks like too much indent like above
| self.task_canceller(TaskSourceName::DOMManipulation) | ||
| .wrap_task(task), | ||
| self.task_manager.task_canceller(TaskSourceName::DOMManipulation) | ||
| .wrap_task(task), |
There was a problem hiding this comment.
nit: too much indent like above
| @@ -0,0 +1,130 @@ | |||
| /* This Source Code Form is subject to the terms of the Mozilla Public | |||
There was a problem hiding this comment.
Since this is a completely new file, you can safely run ./mach rustfmt -d components/script/task_manager.rs and apply the changes to the commit (requires installing first if you haven't, see https://github.com/rust-lang-nursery/rustfmt)
| T: TaskOnce + 'static, | ||
| { | ||
| let msg = MainThreadScriptMsg::Common(CommonScriptMsg::Task( | ||
| ScriptThreadEventCategory::DocumentEvent, |
There was a problem hiding this comment.
I think you want to add a new ScriptThreadEventCategory::HistoryEvent(in the same commit)
4ebc260 to
c8268f6
Compare
|
Ok, looks good to me. I think you can squash it into two commits, one fro the history task source changes, and one for the rest. Thanks for tackling this! @jdm r? |
jdm
left a comment
There was a problem hiding this comment.
I agree with all of gterzian's earlier comments, so thank you for addressing them. Nice work on this!
| @@ -173,21 +165,7 @@ pub struct Window { | |||
| #[ignore_malloc_size_of = "trait objects are hard"] | |||
| script_chan: MainThreadScriptChan, | |||
| #[ignore_malloc_size_of = "task sources are hard"] | |||
There was a problem hiding this comment.
Let's remove this annotation and add any required annotations inside of TaskManager instead.
There was a problem hiding this comment.
Sorry, I'm blocked in this I dunno how to get past: What do I have to do to have ignore_malloc_size_of in the TaskManager? I keep getting this error:
error[E0658]: The attribute `ignore_malloc_size_of` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642)
--> components/script/task_manager.rs:50:5
|
50 | #[ignore_malloc_size_of = "task sources are hard"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(custom_attribute)] to the crate attributes to enable
And I don't know how to solve it (I already tried the suggestion).
Thanks!
There was a problem hiding this comment.
Did you add #[derive(MallocSizeOf)] to the TaskManager struct as well?
c8268f6 to
5954474
Compare
|
@bors-servo r+ |
|
📌 Commit 5954474 has been approved by |
There was a problem hiding this comment.
I think the problem we've experienced where what was previously an intermittent problem suddenly failed consistently, with tasks not being cancelled when clear_js_runtime has been called, is because despite the latest changes in this PR, that function(via ignore_all_events) is still using the ignore_further_async_events field of Window, which isn't tied to scheduled tasks anymore, since those are now schedule via the TaskManager which has it's own task_cancellers: DomRefCell<HashMap<TaskSourceName, Arc<AtomicBool>>>.
Fixing the above should put the situation back to the previous 'intermittent test failure'...
| } | ||
|
|
||
| pub fn task_canceller(&self, name: TaskSourceName) -> TaskCanceller { | ||
| let mut flags = self.ignore_further_async_events.borrow_mut(); |
There was a problem hiding this comment.
The ignore_further_async_events field of Window is still used in a few places, such as ignore_all_events, this makes the task cancelling inside ignore_all_events not have effect on tasks that have been scheduled via the new TaskManager, since the Arc<AtomicBool> are not shared between ignore_further_async_events and TaskManager.task_cancellers.
So we need to remove ignore_further_async_events from Window, and make methods like Window.ignore_all_events used the TaskManager instead.
There was a problem hiding this comment.
Wow, there's no way I'd have seen that. Thank you so much.
One question, before I implement that, is there an (easy) way to properly fix this? I.e. no failure at all. Or would it fall outside the scope of this pr?
There was a problem hiding this comment.
You're welcome.
I'm not sure if there is an easy way to fix the intermittent failure, although once the above is fixed in one commit, you could re-try, in a separate commit, the suggestion I made at #21804 (comment) (although since it is intermittent, you might have a hard time actually knowing for sure whether it helped or not).
|
☔ The latest upstream changes (presumably #22086) made this pull request unmergeable. Please resolve the merge conflicts. |
And remove the method in window that returns it, because it isn't used so far.
5079c12 to
3fb8d4d
Compare
|
Arf, this is finally fixed. Sorry for taking so long with it. |
|
@bors-servo try=wpt |
…try> Unify the task source and task canceller API To do so, I created a struct `TaskManagement(TaskSource, TaskCanceller)` and made `*_task_source` return that instead of just the task source. Next, I refactored all places in which `task_canceller` by basically removing them in favour of a previously called `*_task_source`. I tried to make `task_canceller` a private method in `Window`, with the hope of enforcing the use of `*_task_source`. However, it's used in components/script/dom/globalscope.rs:575 in such a way that will make it harder to avoid. I decided to leave it that way. It'd be possible to unify `*_task_source` in such a way that we would have only one method. However, I decided not to do it because one of the `TaskSource` implementations is special: `history_traversal_task_source`. Not wanting to over complicate things, I decided to leave the structure this way. --- - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #21154 (github issue number if applicable). - [x] These changes do not require tests because it's refactoring code that should already be tested. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21804) <!-- Reviewable:end -->
|
☀️ Test successful - linux-rel-css, linux-rel-wpt |
|
Some |
I moved away from the `Window` struct all the logic to handle task sources, into a new struct called `TaskManager`. In a happy world, I'd be able to just have there two functions, of the types: ```rust fn task_source<T: TaskSource>(&self, name: TaskSourceName) -> Box<T> fn task_source_with_canceller<T: TaskSource>(&self, name: TaskSourceName) -> (Box<T>, TaskSourceCanceller) ``` And not so much duplicated code. However, because TaskSource can't be a trait object (because it has generic type parameters), that's not possible. Instead, I decided to reduce duplicated logic through macros. For reasons[1], I have to pass both the name of the function with canceller and the name of the function without, as I'm not able to concatenate them in the macro itself. I could probably use `concat_idents` to create both types already defined and reduce the amount of arguments by one, but that macro is nightly only. At the same time, not being able to declare macros inside `impl` forces me to pass `self` as an argument. All this makes this solution more verbose than it would be ideally. It does reduce duplication, but it doesn't reduce the size of the file. [1](rust-lang/rust#29599)
3fb8d4d to
75eb94a
Compare
|
Thanks! I addressed both issues and rebased. |
|
@bors-servo r=gterzian |
|
📌 Commit 75eb94a has been approved by |
…terzian Unify the task source and task canceller API To do so, I created a struct `TaskManagement(TaskSource, TaskCanceller)` and made `*_task_source` return that instead of just the task source. Next, I refactored all places in which `task_canceller` by basically removing them in favour of a previously called `*_task_source`. I tried to make `task_canceller` a private method in `Window`, with the hope of enforcing the use of `*_task_source`. However, it's used in components/script/dom/globalscope.rs:575 in such a way that will make it harder to avoid. I decided to leave it that way. It'd be possible to unify `*_task_source` in such a way that we would have only one method. However, I decided not to do it because one of the `TaskSource` implementations is special: `history_traversal_task_source`. Not wanting to over complicate things, I decided to leave the structure this way. --- - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #21154 (github issue number if applicable). - [x] These changes do not require tests because it's refactoring code that should already be tested. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21804) <!-- Reviewable:end -->
|
☀️ Test successful - android, android-mac, android-x86, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, magicleap, status-taskcluster, windows-msvc-dev |
To do so, I created a struct
TaskManagement(TaskSource, TaskCanceller)and made*_task_sourcereturn that instead of justthe task source.
Next, I refactored all places in which
task_cancellerby basicallyremoving them in favour of a previously called
*_task_source.I tried to make
task_cancellera private method inWindow, with thehope of enforcing the use of
*_task_source. However, it's used incomponents/script/dom/globalscope.rs:575 in such a way that will make it
harder to avoid. I decided to leave it that way.
It'd be possible to unify
*_task_sourcein such a way that we wouldhave only one method. However, I decided not to do it because one of the
TaskSourceimplementations is special:history_traversal_task_source. Not wanting to over complicate things,I decided to leave the structure this way.
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThese changes fix Unify Window task sources and task cancellers API #21154 (github issue number if applicable).
These changes do not require tests because it's refactoring code that should already be tested.
This change is