Skip to content

Commit

Permalink
Create a new JSON serializer instance to avoid thread-safety issues
Browse files Browse the repository at this point in the history
  • Loading branch information
wassim-k committed Jul 25, 2024
1 parent 0d8b400 commit f7ba253
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 45 deletions.
38 changes: 0 additions & 38 deletions Orleans.Providers.MongoDB.sln
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestGrainInterfaces", "libs
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TestFSharp", "libs\orleans\test\Grains\TestFSharp\TestFSharp.fsproj", "{8A652779-85EF-48E2-A639-1EED3CE2C39C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Orleans.CodeGenerator.MSBuild.Bootstrap", "libs\orleans\src\BootstrapBuild\Orleans.CodeGenerator.MSBuild.Bootstrap\Orleans.CodeGenerator.MSBuild.Bootstrap.csproj", "{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Orleans.CodeGenerator.MSBuild", "libs\orleans\src\Orleans.CodeGenerator.MSBuild\Orleans.CodeGenerator.MSBuild.csproj", "{A145AFFC-E0CF-4861-AB0C-427C7670FF37}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "TestFSharpInterfaces", "libs\orleans\test\Misc\TestFSharpInterfaces\TestFSharpInterfaces.fsproj", "{A4F61392-36A3-457C-80D0-9CDC48F5922F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFSharpGrainInterfaces", "libs\orleans\test\Grains\TestFSharpGrainInterfaces\TestFSharpGrainInterfaces.csproj", "{43BD15AA-E53D-4FB5-8E04-765EC86797F5}"
Expand Down Expand Up @@ -505,38 +501,6 @@ Global
{8A652779-85EF-48E2-A639-1EED3CE2C39C}.Release|x64.Build.0 = Release|Any CPU
{8A652779-85EF-48E2-A639-1EED3CE2C39C}.Release|x86.ActiveCfg = Release|Any CPU
{8A652779-85EF-48E2-A639-1EED3CE2C39C}.Release|x86.Build.0 = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|ARM.ActiveCfg = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|ARM.Build.0 = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|x64.ActiveCfg = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|x64.Build.0 = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|x86.ActiveCfg = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Debug|x86.Build.0 = Debug|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|Any CPU.Build.0 = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|ARM.ActiveCfg = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|ARM.Build.0 = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|x64.ActiveCfg = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|x64.Build.0 = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|x86.ActiveCfg = Release|Any CPU
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0}.Release|x86.Build.0 = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|ARM.ActiveCfg = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|ARM.Build.0 = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|x64.ActiveCfg = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|x64.Build.0 = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|x86.ActiveCfg = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Debug|x86.Build.0 = Debug|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|Any CPU.Build.0 = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|ARM.ActiveCfg = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|ARM.Build.0 = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|x64.ActiveCfg = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|x64.Build.0 = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|x86.ActiveCfg = Release|Any CPU
{A145AFFC-E0CF-4861-AB0C-427C7670FF37}.Release|x86.Build.0 = Release|Any CPU
{A4F61392-36A3-457C-80D0-9CDC48F5922F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4F61392-36A3-457C-80D0-9CDC48F5922F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4F61392-36A3-457C-80D0-9CDC48F5922F}.Debug|ARM.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -726,8 +690,6 @@ Global
{0ECCC344-21E8-4AD7-9CB9-933054200000} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
{CC76035A-056B-43F3-8FFB-92DE66A19305} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
{8A652779-85EF-48E2-A639-1EED3CE2C39C} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
{7735FCC6-4D1F-4FCA-8D8F-44515EF558C0} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
{A145AFFC-E0CF-4861-AB0C-427C7670FF37} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
{A4F61392-36A3-457C-80D0-9CDC48F5922F} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
{43BD15AA-E53D-4FB5-8E04-765EC86797F5} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
{9BCF7A12-6A5B-43FF-828A-A804F9B9DBB4} = {00BCCB49-80FC-48E4-8A2B-89E0F6313457}
Expand Down
2 changes: 1 addition & 1 deletion Orleans.Providers.MongoDB/Orleans.Providers.MongoDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageTags>Orleans OrleansProviders MongoDB</PackageTags>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TargetFrameworks>net8.0</TargetFrameworks>
<Version>8.0.3</Version>
<Version>8.0.4</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ namespace Orleans.Providers.MongoDB.StorageProviders.Serializers
{
public class JsonGrainStateSerializer : IGrainStateSerializer
{
private readonly JsonSerializer serializer;
private JsonSerializerSettings _jsonSettings;

public JsonGrainStateSerializer(IOptions<JsonGrainStateSerializerOptions> options, IServiceProvider serviceProvider)
{
var jsonSettings = OrleansJsonSerializerSettings.GetDefaultSerializerSettings(serviceProvider);
options.Value.ConfigureJsonSerializerSettings(jsonSettings);
serializer = JsonSerializer.CreateDefault(jsonSettings);
_jsonSettings = OrleansJsonSerializerSettings.GetDefaultSerializerSettings(serviceProvider);
options.Value.ConfigureJsonSerializerSettings(_jsonSettings);
}

public T Deserialize<T>(BsonValue value)
{
using var jsonReader = new JTokenReader(value.ToJToken());
return serializer.Deserialize<T>(jsonReader);
// Creating a new serializer instance to avoid thread-safety issue: https://github.com/JamesNK/Newtonsoft.Json/issues/1452
var jsonSerializer = JsonSerializer.CreateDefault(_jsonSettings);
return jsonSerializer.Deserialize<T>(jsonReader);
}

public BsonValue Serialize<T>(T state)
{
return JObject.FromObject(state, serializer).ToBson();
var jsonSerializer = JsonSerializer.CreateDefault(_jsonSettings);
return JObject.FromObject(state, jsonSerializer).ToBson();
}
}
}

0 comments on commit f7ba253

Please sign in to comment.