Skip to content

Commit

Permalink
Merge pull request #70984 from CyrusNajmabadi/tornChecksumWrite
Browse files Browse the repository at this point in the history
Ensure no torn writes when reading/initializing our solution checksums
  • Loading branch information
RikkiGibson authored Nov 28, 2023
2 parents e0ec250 + 749f09f commit 984df16
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/Workspaces/Core/Portable/Workspace/Solution/DocumentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
Expand Down Expand Up @@ -156,6 +153,9 @@ internal sealed class DocumentAttributes(
bool isGenerated,
bool designTimeOnly)
{
/// <summary>
/// Lock on <see langword="this"/> to ensure safe reading/writing of this field.
/// </summary>
private Checksum? _lazyChecksum;

/// <summary>
Expand Down Expand Up @@ -258,7 +258,16 @@ public static DocumentAttributes ReadFrom(ObjectReader reader)
}

public Checksum Checksum
=> _lazyChecksum ??= Checksum.Create(this, static (@this, writer) => @this.WriteTo(writer));
{
get
{
// Ensure reading/writing from the nullable checksum is atomic.
lock (this)
{
return _lazyChecksum ??= Checksum.Create(this, static (@this, writer) => @this.WriteTo(writer));
}
}
}
}
}
}
16 changes: 14 additions & 2 deletions src/Workspaces/Core/Portable/Workspace/Solution/ProjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

Expand Down Expand Up @@ -497,6 +496,10 @@ internal sealed class ProjectAttributes(
public Guid TelemetryId { get; } = telemetryId;

private StrongBox<(string?, string?)>? _lazyNameAndFlavor;

/// <summary>
/// Lock on <see langword="this"/> to ensure safe reading/writing of this field.
/// </summary>
private Checksum? _lazyChecksum;

/// <summary>
Expand Down Expand Up @@ -649,7 +652,16 @@ public static ProjectAttributes ReadFrom(ObjectReader reader)
}

public Checksum Checksum
=> _lazyChecksum ??= Checksum.Create(this, static (@this, writer) => @this.WriteTo(writer));
{
get
{
// Ensure reading/writing from the nullable checksum is atomic.
lock (this)
{
return _lazyChecksum ??= Checksum.Create(this, static (@this, writer) => @this.WriteTo(writer));
}
}
}
}
}
}
18 changes: 13 additions & 5 deletions src/Workspaces/Core/Portable/Workspace/Solution/SolutionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Remote;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
Expand Down Expand Up @@ -96,6 +92,9 @@ internal SolutionInfo WithTelemetryId(Guid telemetryId)
/// </summary>
internal sealed class SolutionAttributes(SolutionId id, VersionStamp version, string? filePath, Guid telemetryId)
{
/// <summary>
/// Lock on <see langword="this"/> to ensure safe reading/writing of this field.
/// </summary>
private Checksum? _lazyChecksum;

/// <summary>
Expand Down Expand Up @@ -160,7 +159,16 @@ public static SolutionAttributes ReadFrom(ObjectReader reader)
}

public Checksum Checksum
=> _lazyChecksum ??= Checksum.Create(this, static (@this, writer) => @this.WriteTo(writer));
{
get
{
// Ensure reading/writing from the nullable checksum is atomic.
lock (this)
{
return _lazyChecksum ??= Checksum.Create(this, static (@this, writer) => @this.WriteTo(writer));
}
}
}
}
}
}

0 comments on commit 984df16

Please sign in to comment.