Skip to content

Dispose MongoDb, SLDR in singletons#3924

Merged
imnasnainaec merged 3 commits intomasterfrom
dispose
Aug 11, 2025
Merged

Dispose MongoDb, SLDR in singletons#3924
imnasnainaec merged 3 commits intomasterfrom
dispose

Conversation

@imnasnainaec
Copy link
Collaborator

@imnasnainaec imnasnainaec commented Aug 11, 2025

Resolves #3921

This change is Reviewable

@imnasnainaec imnasnainaec self-assigned this Aug 11, 2025
@codecov
Copy link

codecov bot commented Aug 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.15%. Comparing base (6479723) to head (fdf9fd7).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3924      +/-   ##
==========================================
+ Coverage   74.13%   74.15%   +0.01%     
==========================================
  Files         288      288              
  Lines       10614    10621       +7     
  Branches     1326     1327       +1     
==========================================
+ Hits         7869     7876       +7     
  Misses       2359     2359              
  Partials      386      386              
Flag Coverage Δ
backend 85.43% <100.00%> (+0.02%) ⬆️
frontend 65.58% <ø> (ø)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

// Register concrete types for dependency injection

// Mongo context for use in repo contexts
services.AddSingleton<IMongoDbContext, MongoDbContext>();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would make this Scoped. Otherwise you will be sharing the same client across all requests and I would be quite surprised if the client was threadsafe.

Copy link
Contributor

Choose a reason for hiding this comment

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

It turns out it is and this usage is actually recommended:
https://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/connecting/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Whew! It was gonna be a pain to make it (mostly) Scoped since it's used by the SemanticDomainContext which we've made a Singleton.

@jasonleenaylor
Copy link
Contributor

Backend/Contexts/MongoDbContext.cs line 23 at r1 (raw file):

    {
        _mongoClient.Dispose();
        GC.SuppressFinalize(this);

I would probably set Db to null here, I'm not sure if there are any consequences to using it after the client is disposed, but it would certainly be unintended.

// Register concrete types for dependency injection

// Mongo context for use in repo contexts
services.AddSingleton<IMongoDbContext, MongoDbContext>();
Copy link
Contributor

Choose a reason for hiding this comment

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

It turns out it is and this usage is actually recommended:
https://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/connecting/

Copy link
Contributor

@jasonleenaylor jasonleenaylor left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 19 of 19 files at r1, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @hahn-kev and @imnasnainaec)

Copy link
Collaborator Author

@imnasnainaec imnasnainaec left a comment

Choose a reason for hiding this comment

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

Reviewable status: 18 of 19 files reviewed, 1 unresolved discussion (waiting on @hahn-kev and @jasonleenaylor)

// Register concrete types for dependency injection

// Mongo context for use in repo contexts
services.AddSingleton<IMongoDbContext, MongoDbContext>();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Whew! It was gonna be a pain to make it (mostly) Scoped since it's used by the SemanticDomainContext which we've made a Singleton.

@imnasnainaec imnasnainaec merged commit 4a1e33a into master Aug 11, 2025
17 of 18 checks passed
@imnasnainaec imnasnainaec deleted the dispose branch August 11, 2025 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Contexts not disposing MongoClient

3 participants