The API currently allows both mutating the document from the thread it was parsed in and iterating over the same document from other threads. Though from the libxml2 docs on thread safety, it is not entirely clear to me whether the library supports this use-case, I am under the impression that (without extra synchronization by the application) it does not.
Example:
let doc = Parser::default()
.parse_string(r"<html><head></head><body></body></html>")
.unwrap();
let root = doc.get_root_readonly().unwrap();
std::thread::spawn(move || {
loop {
std::thread::sleep(Duration::from_secs(1));
root.get_child_elements();
}
});
loop {
std::thread::sleep(Duration::from_secs(1));
doc.as_node().new_child(None, "test").unwrap();
}
To avoid this, the read-only API could be adapted to consume the Document in the conversion to a read-only version, unwrapping the contained Rc as a runtime check of exclusive ownership. I believe that would be sufficient to ensure the mutable and read-only APIs cannot be used at the same time on the same document.
The API currently allows both mutating the document from the thread it was parsed in and iterating over the same document from other threads. Though from the libxml2 docs on thread safety, it is not entirely clear to me whether the library supports this use-case, I am under the impression that (without extra synchronization by the application) it does not.
Example:
To avoid this, the read-only API could be adapted to consume the
Documentin the conversion to a read-only version, unwrapping the containedRcas a runtime check of exclusive ownership. I believe that would be sufficient to ensure the mutable and read-only APIs cannot be used at the same time on the same document.