-
Notifications
You must be signed in to change notification settings - Fork 286
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
Conversation
…der internally. Fixes dotnet#755
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs
Outdated
Show resolved
Hide resolved
The diff is a bit noisy because the IDE has fixed a lot of comments. It looks like the only change is in the bulk copy and that it saves a property releases the parser lock and then in a finally block waits, should it not re-acquire per your description? |
After debugging this more, here is what I find for general scenarios where we use connection A for SqlBulkCopy instance and connection B for the data source (associated with SqlCommand). The _parserLock is associated with each connection. The lock behavior before the change is like this:
The lock behavior after the change is like this:
It looks like the original design of SqlBulkCopy doesn't expect the same connection to be used for both data source and SqlBulkCopy instance. So far, it looks OK to release the semaphore if we don't actually need it at this point to avoid the deadlock in the single connection edge case. |
It's a weird design but that's how it is. We also discussed this internally, if another thread calls 'Wait' before we do, this thread will be on hold waiting for that thread to release parserlock before proceeding further. |
Fix for issue #755
User-scenario:
Currently, entire driver's async operations are single threaded when it comes to "parser" activity, and every Async operation is done with lock acquired from "SqlInternalConnectionTds._parserLock".
Pseudo scenario:
This PR fixes this behavior as under:
cc @karinazhou