Skip to content

Commit 5a75828

Browse files
authored
Merge pull request #145 from valeriob/bugfix_windows_auto_resize_works_only_once
Fixes windows exception when AutoResizeWindows = true : NativeLibrary…
2 parents 9483983 + 63e38e7 commit 5a75828

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/LightningDB.Tests/EnvironmentTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,16 @@ public void CreateEnvironmentWithAutoResize()
153153
{
154154
using (var env = new LightningEnvironment(_path, new EnvironmentConfiguration
155155
{
156-
MapSize = 10 * 1024 * 1024,
156+
MapSize = 1048576,
157+
AutoResizeWindows = true,
158+
}))
159+
{
160+
env.Open();
161+
}
162+
163+
using (var env = new LightningEnvironment(_path, new EnvironmentConfiguration
164+
{
165+
MapSize = 1048576,
157166
AutoResizeWindows = true,
158167
}))
159168
{

src/LightningDB/Native/Lmdb.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,24 @@ internal static extern MDBResultCode mdb_env_open(IntPtr env, string path, Envir
164164

165165
#if NETCOREAPP3_1 || NET5_0
166166

167+
static bool _shouldSetDllImportResolver = true;
168+
static object _syncRoot = new object();
169+
167170
public static void LoadWindowsAutoResizeLibrary()
168171
{
169-
NativeLibrary.SetDllImportResolver(System.Reflection.Assembly.GetExecutingAssembly(), DllImportResolver);
172+
if (_shouldSetDllImportResolver)
173+
{
174+
lock (_syncRoot)
175+
{
176+
if (_shouldSetDllImportResolver)
177+
{
178+
NativeLibrary.SetDllImportResolver(System.Reflection.Assembly.GetExecutingAssembly(), DllImportResolver);
179+
_shouldSetDllImportResolver = false;
180+
}
181+
}
182+
}
170183
}
171-
184+
172185
private static IntPtr DllImportResolver(string libraryName, System.Reflection.Assembly assembly, DllImportSearchPath? searchPath)
173186
{
174187
if (libraryName == MDB_DLL_NAME)

0 commit comments

Comments
 (0)