Skip to content

Commit ad70bc6

Browse files
committed
Update LoadExtension.cs
1 parent eab642d commit ad70bc6

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

plugins/magic.lambda.sqlite/magic.lambda.sqlite/LoadExtension.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* Magic Cloud, copyright (c) 2023 Thomas Hansen. See the attached LICENSE file for details. For license inquiries you can send an email to thomas@ainiro.io
33
*/
44

5+
using System.Linq;
56
using System.Threading.Tasks;
67
using magic.node;
8+
using magic.node.extensions;
79
using magic.signals.contracts;
8-
using magic.data.common.helpers;
910
using magic.lambda.sqlite.helpers;
10-
using magic.node.extensions;
11-
using System.Linq;
11+
using System.Threading;
1212

1313
namespace magic.lambda.sqlite
1414
{
@@ -18,6 +18,9 @@ namespace magic.lambda.sqlite
1818
[Slot(Name = "sqlite.load-extension")]
1919
public class LoadExtension : ISlotAsync
2020
{
21+
// Ensuring synchronized access.
22+
readonly static SemaphoreSlim _semaphore = new (1,1);
23+
2124
/// <summary>
2225
/// Handles the signal for the class.
2326
/// </summary>
@@ -26,13 +29,20 @@ public class LoadExtension : ISlotAsync
2629
/// <returns>An awaitable task.</returns>
2730
public async Task SignalAsync(ISignaler signaler, Node input)
2831
{
29-
var connection = signaler.Peek<SqliteConnectionWrapper>("sqlite.connect").Connection;
30-
connection.LoadExtension(
31-
input.Children.FirstOrDefault(x => x.Name == "file")?.GetEx<string>() ??
32-
throw new HyperlambdaException("No [file] argument provided to [sqlite.load-extension]"),
33-
input.Children.FirstOrDefault(x => x.Name == "proc")?.GetEx<string>() ??
34-
throw new HyperlambdaException("No [proc] argument provided to [sqlite.load-extension]")
35-
);
32+
await _semaphore.WaitAsync();
33+
try
34+
{
35+
var connection = signaler.Peek<SqliteConnectionWrapper>("sqlite.connect").Connection;
36+
connection.LoadExtension(
37+
input.Children.FirstOrDefault(x => x.Name == "file")?.GetEx<string>() ??
38+
throw new HyperlambdaException("No [file] argument provided to [sqlite.load-extension]"),
39+
input.Children.FirstOrDefault(x => x.Name == "proc")?.GetEx<string>() ??
40+
throw new HyperlambdaException("No [proc] argument provided to [sqlite.load-extension]"));
41+
}
42+
finally
43+
{
44+
_semaphore.Release();
45+
}
3646
}
3747
}
3848
}

0 commit comments

Comments
 (0)