Repository navigation
Access browsing context safely - #15906
Conversation
|
Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @emilio (or someone else) soon. |
|
|
||
| // Steps 1. and 2. | ||
| if self.browsing_context().is_discarded() { | ||
| if self.safe_browsing_context().map(|c| c.is_discarded()).unwrap_or(true) { |
There was a problem hiding this comment.
nit: you can use map_or(true, |c| c.is_discarded())
|
r? @asajeffrey |
2bc2a5d to
3ece595
Compare
|
My take on how we got here... The only place we're creating a The only thing I can think of is that we're accessing the Since it is possible for (e.g.) a This PR will certainly help for that! We could be more drastic, and just make @mchv: thanks for looking into this! |
asajeffrey
left a comment
There was a problem hiding this comment.
Looks good! Just some minor nits.
| &self.image_cache_thread | ||
| } | ||
|
|
||
| /// Retrieve unsafely the browsing context |
There was a problem hiding this comment.
Add a comment, something like "This can panic if it is called after the browsing context has been discarded".
| fn GetParent(&self) -> Option<Root<BrowsingContext>> { | ||
| // Steps 1. and 2. | ||
| if self.browsing_context().is_discarded() { | ||
| if self.safe_browsing_context().map_or(true, |c| c.is_discarded()) { |
There was a problem hiding this comment.
Can this be map_or(true, BrowsingContext::is_discarded)?
There was a problem hiding this comment.
No it can't, because BrowsingContext::is_discarded takes a &BrowsingContext and the contents of the option is a Root<BrowsingContext>.
There was a problem hiding this comment.
Oh that's annoying, oh well.
| fn GetTop(&self) -> Option<Root<BrowsingContext>> { | ||
| // Steps 1. and 2. | ||
| if self.browsing_context().is_discarded() { | ||
| if self.safe_browsing_context().map_or(true, |c| c.is_discarded()) { |
| self.browsing_context.get().unwrap() | ||
| } | ||
|
|
||
| pub fn safe_browsing_context(&self) -> Option<Root<BrowsingContext>> { |
There was a problem hiding this comment.
The only place this method is used outside of dom::window is in Document::browsing_context, which checks whether self.has_browsing_context is true.
Why is self.has_browsing_context ever true without a corresponding browsing context in the document's window?
There was a problem hiding this comment.
The problem is that document.has_browsing_context is immutable, so if a document originally had a browsing context, but then that browsing context was discarded, we don't update the document. We could make document.has_browsing_context mutable, or we could just remove it and replace it by a method which checks to see if the window exists and has a browsing context.
There was a problem hiding this comment.
Given a document can now lose its browsing context, has_browsing_context should probably not be immutable.
There was a problem hiding this comment.
The problem is we're being a bit vague about the distinction between not having a browsing context, and having one that's been discarded. Sigh.
| fn GetParent(&self) -> Option<Root<BrowsingContext>> { | ||
| // Steps 1. and 2. | ||
| if self.browsing_context().is_discarded() { | ||
| if self.safe_browsing_context().map_or(true, |c| c.is_discarded()) { |
There was a problem hiding this comment.
No it can't, because BrowsingContext::is_discarded takes a &BrowsingContext and the contents of the option is a Root<BrowsingContext>.
| &self.image_cache_thread | ||
| } | ||
|
|
||
| /// Retrieve unsafely the browsing context |
There was a problem hiding this comment.
Don't use the word "unsafely", which has a precise meaning in Rust.
| self.browsing_context.get().unwrap() | ||
| } | ||
|
|
||
| pub fn safe_browsing_context(&self) -> Option<Root<BrowsingContext>> { |
There was a problem hiding this comment.
Please rename method to something like maybe_browsing_context or something like that. Safety is a different concept.
Current browsing context accessor in Document unwrap the browsing context option which prevents document to handle correctly the case when there is no browsing context.
3ece595 to
f8c7235
Compare
|
@nox @asajeffrey I have updated the code based on your comments. Let me know if you think we need other changes or add tests before being ready to merge. |
|
LGTM. @bors-servo r+ |
|
📌 Commit f8c7235 has been approved by |
Access browsing context safely Current browsing context accessor in `Document` unwraps the browsing context `Option` which prevents document to handle correctly the case when there is no browsing context. This is the reason servo panics with `session-history.max-length=1` (#15877). As it is my first contribution, I added a `safe` method to retrieve the browsing context rather than change the existing method, but I am happy to change if you think this is the right approach. I did not as well replace all existing method call to the `safe` method, to focus on fixing the issue. If someone can give me a bit of guidance for the test, I will try to contribute one. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [X] These changes fix #15877. - [ ] There are tests for these changes <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- 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/15906) <!-- Reviewable:end -->
|
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css, mac-rel-wpt1, mac-rel-wpt2, windows-gnu-dev, windows-msvc-dev |
|
Yay! |
Current browsing context accessor in
Documentunwraps the browsing contextOptionwhich prevents document to handle correctly the case when there is no browsing context.This is the reason servo panics with
session-history.max-length=1(#15877).As it is my first contribution, I added a
safemethod to retrieve the browsing context rather than change the existing method, but I am happy to change if you think this is the right approach. I did not as well replace all existing method call to thesafemethod, to focus on fixing the issue.If someone can give me a bit of guidance for the test, I will try to contribute one.
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is