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 | SqlClient-826 Missed synchronization #1029

Merged
merged 24 commits into from May 12, 2023
Merged

Conversation

jinek
Copy link
Contributor

@jinek jinek commented Apr 13, 2021

Fixes #826

Internal method SNITCPHandle.Connect now uses Socket.Select to connect the socket with a timeout or without

Additionally:

SqlClient-826 Missed synchronization

Additionally:

* Exceptions swallowing removed to satisfy CA1031 https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1031
* InternalException refactored to satisfy ca1032 https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1032 (Best practices https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions#include-three-constructors-in-custom-exception-classes )
* InternalException constructor has been changed to public as class is not marked as sealed.
@jinek jinek changed the title Fixes #826 SqlClient-826 Missed synchronization Apr 13, 2021
@jinek
Copy link
Contributor Author

jinek commented Apr 13, 2021

@cheenamalhotra I can't add label "Managed SNI". Could you, please? This comment can be removed then

@jinek jinek marked this pull request as ready for review April 13, 2021 16:22
@JRahnama JRahnama added the Ⓜ️ Managed SNI Use this label if the issue/PR relates to issues in Managed SNI label Apr 13, 2021
@JRahnama
Copy link
Member

JRahnama commented Apr 13, 2021

@jinek we have PR #1016 in progress. There might be some conflicts between these two changes. If that happens, please wait for that PR to merge and pull the changes again.

# Conflicts:
#	src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs
@jinek
Copy link
Contributor Author

jinek commented Apr 13, 2021

@JRahnama I've took into account that PR and merged it. However I allowed myself to slightly fix that PR while keeping the idea of it.
@karinazhou Could you please check this PR to be sure I adopted your idea correctly.

@JRahnama
Copy link
Member

@jinek thanks for contributing into this repository, We have a release scheduled for this week and pretty busy with release activities. We will get back to this next week when we have more time.
Thanks again.

* Complexity of array enumeration changed: n*n in worst case with 2n
* Long to int (timeout) is now checked to avoid timeout errors
# Conflicts:
#	src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs
@jinek
Copy link
Contributor Author

jinek commented Jun 28, 2021

I've updated the PR, @DavoudEshtehari .
But, requesting additional attention to two things:

  1. Here additional improvement can be done. For infinite timeout case we can avoid exception throwing which would give small performance boost, but code would become more complex. Should I add the code for infinite timeout case (it's about 3 additional if blocks)?
  2. I've removed the check of Dns.GetHostAddresses for returning null as seems it does not ever return null. However it's not declared by the documentation of the method and I don't understand where it is supposed always return an array or no. Should I brind back the code considering it returning null? (probably just adding one redundent if block)

Copy link
Member

@DavoudEshtehari DavoudEshtehari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the failed pipelines one more time, and the result remained the same. The reason for these failures as I know is the execution time exceeds the allowed 1 hour. First of all, we need to verify in which situations it happens; Maybe running a benchmark test could help us too!

  1. Here additional improvement can be done. For infinite timeout case we can avoid exception throwing which would give small performance boost, but code would become more complex. Should I add the code for infinite timeout case (it's about 3 additional if blocks)?

Feel free to add your suggested change for further discussions.

  1. I've removed the check of Dns.GetHostAddresses for returning null as seems it does not ever return null. However it's not declared by the documentation of the method and I don't understand where it is supposed always return an array or no. Should I brind back the code considering it returning null? (probably just adding one redundent if block)

Right, I checked the source code of the Dns.GetHostAddresses, it won't return null in case of a free-error execution. So, as you mentioned, null-checking is not necessary.

@jinek
Copy link
Contributor Author

jinek commented Jul 7, 2021

I ran the failed pipelines one more time, and the result remained the same. The reason for these failures as I know is the execution time exceeds the allowed 1 hour. First of all, we need to verify in which situations it happens; Maybe running a benchmark test could help us too!

I have checked the issue: here the connection timeout is set to almost 3 hours (10000 seconds) :


Same time SqlInternalConnectionTds runs retry for all random errors:

In my case the error is "Arithmetic operation resulted in an overflow." because 10000 seconds can not be passed to Socket.Select as it requires the value in microseconds int32: https://github.com/dotnet/runtime/blob/9ce467fe6164e781b67897615ff7dd01820084ff/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs#L2199

@DavoudEshtehari
The question now is if it is fine to limit Connection Timeout to 35 minutes (int.Max microseconds) or must we keep 35000 minutes (int.Max seconds)?
If we have to keep possibility of the connection timeout to be more than 35 minutes than I can implement "async" alternative as mentioned here #826 (comment)

@jinek
Copy link
Contributor Author

jinek commented Jul 7, 2021

Also fixes this: #583

@cheenamalhotra
Copy link
Member

cheenamalhotra commented Jul 7, 2021

Looking at the SocketPal.Select API, it seems they could do well with a long type of microseconds parameter, but because it's int. we have to limit our timeout? (Timeout in milliseconds should always be long/double IMHO)

There's also an open API Proposal from 6 years ago, you might want to ask dotnet/runtime teams to resurrect: dotnet/runtime#14336

Connection timeout cannot be limited as that would be a major issue for customer applications (we know of) running high loads of work and setting timeout to as long as 2 hours.

@jinek
Copy link
Contributor Author

jinek commented Jul 9, 2021

Looking at the SocketPal.Select API, it seems they could do well with a long type of microseconds parameter, but because it's int. we have to limit our timeout? (Timeout in milliseconds should always be long/double IMHO)

There's also an open API Proposal from 6 years ago, you might want to ask dotnet/runtime teams to resurrect: dotnet/runtime#14336

Connection timeout cannot be limited as that would be a major issue for customer applications (we know of) running high loads of work and setting timeout to as long as 2 hours.

Right right, but all this options are time consuming?
Does socket.Connect really have infinite or any big timeout? Im thinking may be it is already limited, i would just play with the math to leave it as is.
Otherwise, i can bring Dispose similar as it was before, but with synchronisation.

# Conflicts:
#	src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs
@codecov
Copy link

codecov bot commented May 8, 2023

Codecov Report

Patch coverage: 90.41% and project coverage change: -0.18 ⚠️

Comparison is base (91694d8) 70.80% compared to head (82ad1c0) 70.63%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1029      +/-   ##
==========================================
- Coverage   70.80%   70.63%   -0.18%     
==========================================
  Files         305      305              
  Lines       61795    61807      +12     
==========================================
- Hits        43756    43657      -99     
- Misses      18039    18150     +111     
Flag Coverage Δ
addons 92.88% <ø> (ø)
netcore 73.43% <90.41%> (-0.12%) ⬇️
netfx 69.22% <ø> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...nt/netcore/src/Microsoft/Data/SqlClient/SqlUtil.cs 39.14% <0.00%> (-0.37%) ⬇️
...e/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs 63.65% <91.66%> (+0.77%) ⬆️

... and 14 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

jinek and others added 6 commits May 10, 2023 13:44
…/InternalException.cs

Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
…ient\SNI\SNITcpHandle.cs(356,82): Error CS1574: XML comment has cref attribute 'ipPreference' that could not be resolved"
reorder & xml doc
jinek and others added 7 commits May 11, 2023 10:01
…ent/SNI/SNITcpHandle.cs

Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
…ent/SNI/SNITcpHandle.cs

Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
…ent/SNI/SNITcpHandle.cs

Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
…ent/SNI/SNITcpHandle.cs

Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
…ent/SNI/SNITcpHandle.cs

Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
…ent/SNI/SNITcpHandle.cs

Co-authored-by: DavoudEshtehari <61173489+DavoudEshtehari@users.noreply.github.com>
Copy link
Member

@DavoudEshtehari DavoudEshtehari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done 🎉

@DavoudEshtehari DavoudEshtehari added this to In progress in SqlClient v5.2 via automation May 11, 2023
SqlClient v5.2 automation moved this from In progress to Reviewer approved May 12, 2023
@JRahnama JRahnama merged commit f478be5 into dotnet:main May 12, 2023
132 checks passed
SqlClient v5.2 automation moved this from Reviewer approved to Done May 12, 2023
@JRahnama JRahnama changed the title SqlClient-826 Missed synchronization Fix | SqlClient-826 Missed synchronization Jun 7, 2023
kant2002 pushed a commit to kant2002/SqlClient that referenced this pull request Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ⓜ️ Managed SNI Use this label if the issue/PR relates to issues in Managed SNI
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Missed synchronization
6 participants