Repository navigation
Use a structured clone holder to store rooted clones - #21218
Conversation
2a6ad46 to
f5b1c03
Compare
|
Wonders! It's not crashing anymore... problem was doing The structured cloning of a blob test is also still passing, so I guess this one is ready for review... |
f5b1c03 to
3ce004c
Compare
KiChjang
left a comment
There was a problem hiding this comment.
So, the StructuredCloneHolder right now would only grow in size, since the caller of StructuredClone::read doesn't actually pop the element off of the holder. I imagine that this is the intended behavior instead?
| if tag == StructuredCloneTags::DomBlob as u32 { | ||
| return read_blob(cx, r) | ||
| let (rooted_blob, js_object) = read_blob(cx, r); | ||
| holder.blobs.borrow_mut().push(rooted_blob); |
There was a problem hiding this comment.
We need to be very careful here, because if holder.blob is being borrowed mutably or immutably elsewhere, this will panic. Specifically, there may be a chance that some code elsewhere borrows it immutably for reading, and then during the read, it performs yet another StructuredCloneRead, which would then run into a panic.
There was a problem hiding this comment.
Should we use try_borrow_mut and do some error handling at this point? What to do if that returns an error? Should we abort the reading and return a ptr::null_mut()?
there may be a chance that some code elsewhere borrows it immutably for reading, and then during the read, it performs yet another StructuredCloneRead
The way I look at this, is that JS_ReadStructuredClone is basically a function call, and the 'callbacks' are a bit of a misnomer since they are simply called at the end of this function call, in case the engine encounters a StructuredCloneTags that doesn't match any of the tags it can handle itself, it then calls the read callback to see if it can handle it.
If my understanding is correct, it means it would be impossible to during the read, perform yet another StructuredCloneRead. Basically, one call to JS_ReadStructuredClone would need to return, which includes having called the 'callbacks', before another one could be executed.
In any case, I think it might still a good idea to use try_borrow_mut and return a ptr::null_mut() if that fails, 'just to be sure'.
What do you think?
|
|
||
| /// A place to keep the clones read into this realm rooted. | ||
| #[ignore_malloc_size_of = "Rc<T> is hard"] | ||
| structured_clone_holder: Rc<StructuredCloneHolder>, |
There was a problem hiding this comment.
I'm actually more interested to know why this has to be an Rc<StructuredCloneHolder> here instead of just DomRefCell<Vec<DomRoot<Blob>>>. In the read_callback function under structuredclone.rs, you can get a reference to the global by simply calling GlobalScope::from_context(cx), so passing in a raw Rc pointer seems unnecessary to me.
There was a problem hiding this comment.
My answer to 'why aRc' would be: 'it was easy to get a raw pointer out of it and cast it to *mut raw::c_void, and then build one back from the raw pointer on the other side'.
Would that be doable with a DomRefCell? (I see that a RefCell has a as_ptr method, and it doesn't seem to be available on the DomRefCell variant.).
Assuming we can get a raw pointer using as_ptr, how would we build it back on the read end from this pointer? I don't see the equivalent of Rc::from_raw on RefCell...
Any ideas?
There was a problem hiding this comment.
Going back to what I suggested -- you already have a *mut JSContext, so you can make use of that to get a GlobalScope out of it, and in your read_callback function, you can simply call methods on GlobalScope to enqueue the DomRoot<Blob> into its data holder; there's no need to pass in a raw Rc pointer just to get a reference to the data holder.
There was a problem hiding this comment.
Awesome, I'll give it a try! (sorry I actually missed your suggestion above on the first read...)
It depends on the problem we're trying to solve, which is not entirely clear to me by the way. I am under the impression that the problem is that previously a Yes it would only grow in size, and I am not sure when would be a good 'time' to potential pop it off and let it drop. My intuition would be 'when the JSobject is garbage collected', so perhaps via an impl of |
Do you mean that we could just pop it off and drop it once the read operation has completed? I was under the impression that we might need to keep the Blob rooted while the JsObject might still be used by the JS code after the read operation has returned... |
70dfe55 to
931e632
Compare
|
@KiChjang Please take another look, I guess the last question is whether we want to pop the blob off of the vec at some point or not... |
44a8c14 to
cce12db
Compare
|
☔ The latest upstream changes (presumably #21111) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@gterzian So, we definitely need to pop the |
|
Huh, I left a detailed code review on this OR, but I can't find any trace of it now. |
|
@KiChjang You're right, and using this new How about adding a @jdm That's a real bummer, sorry to hear you lost that work... |
|
@gterzian I'm not sure what you mean by |
| let blob = Blob::new(&target_global, BlobImpl::new_from_bytes(blob_buffer), type_str); | ||
| return blob.reflector().get_jsobject().get() | ||
| let js_object = blob.reflector().get_jsobject().get(); | ||
| if let Some(mut holder) = target_global.structured_clone_holder() { |
There was a problem hiding this comment.
As pointed out, this change means we leak any blob that is sent through the structured clone algorithm because it is now always reachable from the global. I propose we return to your original design, with an important modification: there's no need to use Rc; we should be able to create the holder on the stack in read_clone, since the holder does not need to exist beyond that stack frame when the cloned values have been read into the rooted rval value.
There was a problem hiding this comment.
Ok, thanks, I will look into it...
|
Yes, that is an accurate summary :) |
cce12db to
55e27c7
Compare
2a91e35 to
5040142
Compare
|
@jdm ready for review |
4621900 to
155df0c
Compare
| unsafe fn read_blob(cx: *mut JSContext, | ||
| r: *mut JSStructuredCloneReader) | ||
| r: *mut JSStructuredCloneReader, | ||
| closure: *mut raw::c_void) |
There was a problem hiding this comment.
Let's make this take &mut StructuredCloneHolder instead. Then we don't need with_structured_clone_holder and can use holder.blob = Some(blob) directly.
| assert!(tag > StructuredCloneTags::Min as u32, "tag should be higher than StructuredCloneTags::Min"); | ||
| if tag == StructuredCloneTags::DomBlob as u32 { | ||
| return read_blob(cx, r) | ||
| return read_blob(cx, r, closure) |
There was a problem hiding this comment.
We can pass &mut *(closure as *mut StructuredCloneHolder) instead.
155df0c to
a5d7cd1
Compare
|
@jdm Ok, comments addressed... |
|
@bors-servo r+ |
|
📌 Commit a5d7cd1 has been approved by |
Use a structured clone holder to store rooted clones <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [ ] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes fix #21164 (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [ ] These changes do not require tests because _____ <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- 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/21218) <!-- Reviewable:end -->
|
☀️ Test successful - android, 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, windows-msvc-dev |
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is