Skip to content

Commit b72a461

Browse files
committed
Fix KeyNotFoundException in GetAndRemoveStream. Fixes #757
1 parent 6d392fb commit b72a461

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlBulkLoader.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.IO;
56
using System.Text;
67
using System.Threading;
@@ -205,8 +206,8 @@ internal async ValueTask<int> LoadAsync(IOBehavior ioBehavior, CancellationToken
205206
}
206207
finally
207208
{
208-
if (closeStream)
209-
((IDisposable) GetAndRemoveSource(FileName!)).Dispose();
209+
if (closeStream && TryGetAndRemoveSource(FileName!, out var source))
210+
((IDisposable) source).Dispose();
210211

211212
if (closeConnection)
212213
Connection.Close();
@@ -235,10 +236,24 @@ internal static object GetAndRemoveSource(string sourceKey)
235236
{
236237
lock (s_lock)
237238
{
238-
var stream = s_sources[sourceKey];
239+
var source = s_sources[sourceKey];
239240
s_sources.Remove(sourceKey);
240-
return stream;
241+
return source;
241242
}
242243
}
244+
245+
internal static bool TryGetAndRemoveSource(string sourceKey, [NotNullWhen(true)] out object? source)
246+
{
247+
lock (s_lock)
248+
{
249+
if (s_sources.TryGetValue(sourceKey, out source))
250+
{
251+
s_sources.Remove(sourceKey);
252+
return true;
253+
}
254+
}
255+
256+
return false;
257+
}
243258
}
244259
}

0 commit comments

Comments
 (0)