Skip to content

API allows concurrent read-only and mutable access to the same document #192

@maartendeprez

Description

@maartendeprez

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions