JasperFx / marten

.NET Transactional Document DB and Event Store on PostgreSQL
https://martendb.io
MIT License
2.82k stars 445 forks source link

NpgsqlDataSource not disposed on graceful termination #3175

Closed CptWesley closed 5 months ago

CptWesley commented 5 months ago

It appears that the NpgsqlDataSource is currently not being disposed when the ASP.NET Core application terminates gracefully, this means that the connections held by the pool might not be released. This mostly leads to database connection exhaustion in test projects that rapidly fire up many web applications and terminate them for each test case.

Current workaround: hook the ApplicationStopping lifetime event and dispose the data source manually.

jeremydmiller commented 5 months ago

Are you sure about that? We definitely had this issue in local development, and I was consistently seeing the NpgsqlDataSource being disposed when IHost.Dispose() was called. Got a little more information to share about what you're doing?

CptWesley commented 5 months ago

On further inspection it appears we are creating our own NpgsqlDataSource instance and passing that to marten to use. I'm not sure if that causes the issue. We are only using a single tenant.

I'm also not certain if marten should manage the lifetime of NpgsqlDataSource instances that are passed to it, or whether or not they should be kept alive. Perhaps this could be determined by an optional boolean parameter, leaveOpen, similarly to the parameter with same name of the StreamWriter and StreamReader constructors.