dotnet / dotnet/runtime

[Proposal] Document that Linq-to-XML is thread-safe for multiple readers

Open
#123,068 4 comments 4 reactions 0 assignees View on GitHub
area-System.Xml documentation help wanted
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

*Note: This proposal was inspired by [the discussion on this StackOverflow question](https://stackoverflow.com/q/79864420/87698). Credit for the ideas mentioned here belongs to the discussion participants.*

### Overview

There are some strong indications that Linq-to-XML (`XElement` etc.) is thread-safe for multiple readers:
* It internally uses [XHashtable](https://github.com/microsoft/referencesource/blob/main/System.Xml.Linq/System/Xml/Linq/XLinq.cs#L564), which seems to be specifically designed to be thread-safe.
* There are (old) examples online of using Linq-to-XML with Parallel Linq.

However, this is not documented anywhere (or, at least, I did not find any official documentation on that matter). My proposal is to document this, [similar to what has been done, for example, on `Dictionary`](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=net-10.0#thread-safety).

### Analysis

Advantage: Apparently, a lot of work went into making Linq-to-XML thread-safe for multiple readers, and it would be a shame if people don't use that feature because it's not documented.

Risk: Any future changes that make reading Linq-to-XML non-thread-safe would be breaking changes. That's a good thing for the users, but obviously more work for the .NET Runtime developers.

### Open questions

* Are the Linq-to-XML classes really thread-safe for multiple readers? I did not do a full code review of all the classes involved.
* Where should this be documented? A "Thread Safety" section of every class involved (XElement, XNode, etc.) or at some central place?
* And the obvious question: Does the .NET Runtime team want this?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.