Skip to content

Commit 81ece7d

Browse files
committed
Lock before writing to potentially shared file
Since there's no guarantee we won't get the same query simultaneously, we need to lock before attempting to write to the temp file to avoid collisions.
1 parent 93a098c commit 81ece7d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/JQ/JQ.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Runtime.InteropServices;
5-
using System.Runtime.Intrinsics.Arm;
65
using System.Security.Cryptography;
76
using System.Text;
87
using System.Threading.Tasks;
@@ -19,6 +18,7 @@ namespace Devlooped;
1918
/// </remarks>
2019
public static class JQ
2120
{
21+
static readonly object syncLock = new();
2222
static readonly string jqpath;
2323

2424
static JQ()
@@ -83,7 +83,13 @@ public static async Task<string> ExecuteAsync(string json, string query)
8383
var hash = BitConverter.ToString(SHA256.HashData(Encoding.UTF8.GetBytes(normalized)));
8484
var queryFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"{hash}.jq");
8585
if (!File.Exists(queryFile))
86-
await File.WriteAllTextAsync(queryFile, normalized);
86+
{
87+
lock (syncLock)
88+
{
89+
if (!File.Exists(queryFile))
90+
File.WriteAllText(queryFile, normalized);
91+
}
92+
}
8793

8894
var jq = await Cli.Wrap(jqpath)
8995
.WithArguments(["-r", "-f", queryFile])

0 commit comments

Comments
 (0)