question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot access a disposed object.\nObject name: 'System.Net.Sockets.Socket

See original GitHub issue

Hi Team, I am using EF core 3.1.19 to connect to AWS RDS MYSQL database. The packages used are below .

   <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.19" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.19" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.19" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.19" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.7" />

and MYSQL Connector used is 0.69.10 version

I am getting the below exception intermittently while doing some db operation . The inner exception states that Cannot access a disposed object.\nObject name: 'System.Net.Sockets.Socket

I recently migrated from EF core 2.7 which was working fine there.

Exception Details

Message": "Connect Timeout expired.",
  "Source": "MySqlConnector",
  "StackTraceString": "   at MySqlConnector.Core.ServerSession.OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken)\n   at MySqlConnector.Core.ServerSession.ConnectAsync(ConnectionSettings cs, Int32 startTickCount, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 368\n   at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, Int32 startTickCount, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ConnectionPool.cs:line 143\n   at MySqlConnector.Core.ConnectionPool.GetSessionAsync(MySqlConnection connection, Int32 startTickCount, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ConnectionPool.cs:line 143\n   at MySql.Data.MySqlClient.MySqlConnection.CreateSessionAsync(ConnectionPool pool, Int32 startTickCount, Nullable`1 ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySql.Data.MySqlClient/MySqlConnection.cs:line 758",
  "RemoteStackTraceString": null,
  "RemoteStackIndex": 0,
  "HResult": -2147467259,
  "HelpURL": null
},
{
  "Depth": 4,
  "ClassName": "System.ObjectDisposedException",
  "Message": "Cannot access a disposed object.\nObject name: 'System.Net.Sockets.Socket'.",
  "Source": "System.Net.Sockets",
  "StackTraceString": "   at System.Net.Sockets.Socket.BeginConnect(IPAddress address, Int32 port, AsyncCallback requestCallback, Object state)\n   at System.Net.Sockets.TcpClient.BeginConnect(IPAddress address, Int32 port, AsyncCallback requestCallback, Object state)\n   at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1,TArg2](Func`5 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, TArg2 arg2, Object state, TaskCreationOptions creationOptions)\n   at System.Net.Sockets.TcpClient.ConnectAsync(IPAddress address, Int32 port)\n   at MySqlConnector.Core.ServerSession.OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer loadBalancer, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/ServerSession.cs:line 907",

Further technical details

MySQL version: 5.7.33 Operating system: Linux 64 AWS ECS Pomelo.EntityFrameworkCore.MySql version:3.2.7 Microsoft.AspNetCore.App vesion: 3.1

Other details about my project setup: connection string Server=serverurl;Port=3306;Database=DBName;User=;Password=**;Allow User Variables=True;SslMode=Required

i am registering the DB context as InstancePerLifetimeScope.

Please help me on this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
bgraingercommented, Jan 15, 2022

FWIW the Inner Exception was removed in https://github.com/mysql-net/MySqlConnector/issues/1035 (>= 1.3.13) to avoid the confusing error message in the OP.

1reaction
PaulPBastiancommented, Jan 17, 2022

Was each of your in parallel running threads using their own DbContext instance?

Yes @lauxjpn , Each concurrent jobs will be running on separate thread and all db operation will happen under a lifetimescope instance created for each job like below

using (var childScope = _scope.BeginLifetimeScope())
{ 
  _logger.Debug("Executing job");
}

and dbcontext is resistered as

builder.Register({dbconext}).InstancePerLifetimeScope();

let me check the number of open connections

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot access a disposed object in System.Net.Sockets. ...
You're closing your connection when you send data on the first btnSend_Click method. If you omit this from your method, it may solve...
Read more >
Object name: 'System.Net.Sockets.Socket'.) Cannot access ...
In case of timeout happened it leaves one of the task in unobserved state, same time implicitely calling the UdpClient.Dispose(), which later ...
Read more >
Cannot access a disposed object. Object name: 'System. ...
You do a Client.Close() that renders the Client useless for later use. You have to re-initialize it, or keep it open.
Read more >
What's wrong with this code, i keep getting this error: An ...
ObjectDisposedException : Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket. public class StoreController : Controller
Read more >
c# System.ObjectDisposedException: 'Cannot access a ...
but i having issue i trying to get the validate db to work for the life of me i cant i get the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found