Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bulk Copy Async deadlock with custom IDataReader using SqlDataReader internally. #779

Merged
merged 2 commits into from Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1159,6 +1159,11 @@ private Task ReadFromRowSourceAsync(CancellationToken cts)
}
else
{ // This will call Read for DataRows, DataTable and IDataReader (this includes all IDataReader except DbDataReader)
// Release lock to prevent possible deadlocks
SqlInternalConnectionTds internalConnection = _connection.GetOpenTdsConnection();
bool semaphoreLock = internalConnection._parserLock.CanBeReleasedFromAnyThread;
internalConnection._parserLock.Release();

_hasMoreRowToCopy = false;
try
{
Expand All @@ -1175,6 +1180,11 @@ private Task ReadFromRowSourceAsync(CancellationToken cts)
throw;
}
}
finally
{
_insideRowsCopiedEvent = false;
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
internalConnection._parserLock.Wait(canReleaseFromAnyThread: semaphoreLock);
}
return null;
}
}
Expand Down
Expand Up @@ -1261,7 +1261,7 @@ private Task ReadFromRowSourceAsync(CancellationToken cts)
{
if (_isAsyncBulkCopy && (_DbDataReaderRowSource != null))
{
//This will call ReadAsync for DbDataReader (for SqlDataReader it will be truely async read; for non-SqlDataReader it may block.)
// This will call ReadAsync for DbDataReader (for SqlDataReader it will be truely async read; for non-SqlDataReader it may block.)
return _DbDataReaderRowSource.ReadAsync(cts).ContinueWith((t) =>
{
if (t.Status == TaskStatus.RanToCompletion)
Expand All @@ -1272,7 +1272,12 @@ private Task ReadFromRowSourceAsync(CancellationToken cts)
}, TaskScheduler.Default).Unwrap();
}
else
{ //this will call Read for DataRows, DataTable and IDataReader (this includes all IDataReader except DbDataReader)
{ // This will call Read for DataRows, DataTable and IDataReader (this includes all IDataReader except DbDataReader)
// Release lock to prevent possible deadlocks
SqlInternalConnectionTds internalConnection = _connection.GetOpenTdsConnection();
bool semaphoreLock = internalConnection._parserLock.CanBeReleasedFromAnyThread;
internalConnection._parserLock.Release();

_hasMoreRowToCopy = false;
try
{
Expand All @@ -1291,6 +1296,11 @@ private Task ReadFromRowSourceAsync(CancellationToken cts)
throw;
}
}
finally
{
_insideRowsCopiedEvent = false;
internalConnection._parserLock.Wait(canReleaseFromAnyThread: semaphoreLock);
}
return null;
}
}
Expand Down