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

A network-related or instance-specific error occurred while establishing a connection to SQL Server. On AWS Lambda #839

Closed
pnquest opened this issue Dec 4, 2020 · 19 comments

Comments

@pnquest
Copy link

pnquest commented Dec 4, 2020

Describe the bug

With version 2.1.0 of Microsoft.Data.SqlClient attempts to connect to a SQL server database that is not hosted in AWS are met with the below exception.

After failing to find any other cause for this error I rolled back to version 2.0.1 and everything worked fine. I did not see this issue on my local (windows) machine.

Exception message: 

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)

Stack trace:

 at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, SqlAuthenticationMethod authType)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open()

To reproduce

Attempt to open a connection to a sql server database hosted outside aws from a lambda function.

Expected behavior

The connection should work fine, just like it does in package versions previous to this.

Further technical details

Microsoft.Data.SqlClient version: 2.1.0
.NET target: core 3.1
SQL Server version: SQL Server 2017
Operating system: AWS Lambda Runtime

@pnquest
Copy link
Author

pnquest commented Dec 4, 2020

After a bit more investigation I think this is a Linux vs Windows thing, and doesn't have anything to do with AWS, Lambda, or the fact that the database is hosted outside AWS.

@JRahnama
Copy link
Member

JRahnama commented Dec 4, 2020

@pnquest

After a bit more investigation I think this is a Linux vs Windows thing, and doesn't have anything to do with AWS, Lambda, or the fact that the database is hosted outside AWS.

Is the issue still Microsoft.Data.SqlClient related? If yes, can you provide us a sample repro? besides that can you share your connection string properties?

@pnquest
Copy link
Author

pnquest commented Dec 4, 2020

@JRahnama

Is the issue still Microsoft.Data.SqlClient related? If yes, can you provide us a sample repro? besides that can you share your connection string properties?

Yes, It is definitely the library. There doesn't seem to be much to the repro it fails the instant you call Open:

var con = new SqlConnection(conString);
con.Open();

The trick seems to be that it only occurs on certain platforms, and not others (Win 10 is fine, but whatever underpins the lambda runtime, Amazon Linux 2 I assume, is not).

Whatever caused this was introduced in the latest release, every prior version this same code works just fine.

The connection string looks like this:

Server=SERVERNAME.DOMAINNAME\\INSTANCENAME; Database=DB_NAME; Encrypt=yes; TrustServerCertificate=true; User Id={userId}; Password={password}

@Wraith2
Copy link
Contributor

Wraith2 commented Dec 4, 2020

Psychic debugging attempt. Look at the tls versions that the linux machine you're using has setup, recent docker containers on things like alpine have had known problems with not enabling the required version.

@pnquest
Copy link
Author

pnquest commented Dec 5, 2020

@Wraith2 any idea how I would go about checking those from inside a c# program?

@JRahnama Did this latest release drop support for a TLS version? That seems like it would have belonged in a 3.0.0 release rather than 2.1.0.

@JRahnama
Copy link
Member

JRahnama commented Dec 8, 2020

@pnquest we did not drop support for TLS versions. our driver captures the requested TLS from user and passes that to Sql server. Saying that, if anything happens it will happen at server side not at the driver level.
you can enforce TLS version inside your C# application by
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

@pnquest
Copy link
Author

pnquest commented Dec 11, 2020

@JRahnama if that is the case, it seems unlikely that @Wraith2 is correct. The runtime platform did not change, the library did.

@JRahnama
Copy link
Member

@pnquest can you test your application with the latest hotfix release of 2.1.1 to see if the issue comes up?

@pnquest
Copy link
Author

pnquest commented Dec 28, 2020

@JRahnama Unfortunately, it did not solve the issue. The error remains the same.

@cheenamalhotra
Copy link
Member

@pnquest

Could you also try with 2.1.0 preview 1 and 2 versions to help us reach change-set that causes this issue?

@pnquest
Copy link
Author

pnquest commented Dec 30, 2020

@cheenamalhotra I just tried both. Preview 1 works fine, but preview 2 fails.

@MuhKuh7
Copy link

MuhKuh7 commented Jan 20, 2021

I can confirm the same issue running version 2.1.1 in a docker container mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim. It works when downgrading to an older version.

@cheenamalhotra
Copy link
Member

Hi @pnquest

PR #874 will fix this issue it seems since you're connecting to Instance name as well.
Please try with the changes we made in this PR by running test with this NuGet: Build CI Artifacts and let us know.

We shall release this fix in v2.1.2 patch version soon.

@pnquest
Copy link
Author

pnquest commented Jan 25, 2021

@cheenamalhotra Thanks for taking care of this! I unfortunately won't be able to deploy this to our environment if it isn't on nuget. Could you release it as a preview? If not maybe @MuhKuh7 could verify?

@davesmits
Copy link

@cheenamalhotra is there an ETA on when package will be published?

@cheenamalhotra
Copy link
Member

cheenamalhotra commented Feb 1, 2021

Hi @davesmits

We're working on a few important changes also candidate for this release, it's planned to be released as soon as we're ready, but latest by mid-February.

Edit: Timelines are stretched to first week of March, to include a few more important fixes like issue #659 and #926

@tarasverq
Copy link

@cheenamalhotra can I ask you to send artifacts link posted here one more time?

@cheenamalhotra
Copy link
Member

Hi @tarasverq

Sure, I'm uploading NuGet here (rebuilt) you can try:
Microsoft.Data.SqlClient.2.1.0-pull-4c45dce.21057.1.zip

@cheenamalhotra
Copy link
Member

Hi @davesmits @tarasverq

Microsoft.Data.SqlClient v2.1.2 is now released that contains the fix.
Closing issue as resolved.

tothegills added a commit to OctopusDeploy/Nevermore that referenced this issue Sep 29, 2021
tothegills added a commit to OctopusDeploy/Nevermore that referenced this issue Sep 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants