Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 9, 2025

MongoDB sessions were not being properly cleaned up after transactions, causing session leaks. Sessions were only ended in close(), not after commitTransaction() or abortTransaction().

Changes

Session lifecycle fixes:

  • commitTransaction() and abortTransaction() now call session.endSession() and clear the session reference
  • getSession() now properly stores newly created sessions in this.session
  • All autoCommit/autoAbort code paths updated to end sessions after operations

Type safety:

  • Changed session: ClientSession to session?: ClientSession to eliminate unsafe type casts

Usage

The getSession() method is already public and can be used for custom operations within transactions:

const adapter = await MongooseAdapter.newSyncedAdapter('mongodb://...');
await adapter.getTransaction();

// Access session for custom operations
const session = await adapter.getSession();
const CasbinRule = adapter.getCasbinRule();
await CasbinRule.collection.insertOne({ ptype: 'p', v0: 'alice' }, { session });

await adapter.commitTransaction(); // Session properly ended here

Added comprehensive documentation and examples for manual transaction control and session lifecycle patterns.

Original prompt

This section details on the original issue you should resolve

<issue_title>[feature] better session management for newSyncedAdapter()</issue_title>
<issue_description>Hi!
I am a MongoDB novice and would like to ask two questions. Why doesn’t the newSyncedAdapter API return the current session or call the current session.endSession?
Because I want to perform some operations before submitting the transaction and found that I need to manage the session myself, which doesn’t seem reasonable. Also, I see that people online will call session.endSession after a transaction is completed, but this library only calls session.endSession in the close method. Other APIs do not call session.endSession after the session is committed or discarded. Is there any problem with this? Hope to get your help.</issue_description>

Comments on the Issue (you are @copilot in this section)

@hsluoyz @CheaterScript hi, can you make a PR to return the current session in the function? @hsluoyz @kohtala hi, can you make a PR to fix it?

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz
Copy link
Member

hsluoyz commented Dec 9, 2025

@nodece @Shivansh-yadav13 please review

@hsluoyz hsluoyz marked this pull request as ready for review December 9, 2025 13:12
Copilot AI and others added 4 commits December 9, 2025 13:24
- Call session.endSession() after commitTransaction() and abortTransaction()
- Clear session reference after ending to prevent reuse of closed sessions
- Fix getSession() to properly store new sessions
- Apply session cleanup in both manual and auto commit/abort scenarios

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
- Document synced adapter usage and transaction control
- Add examples for manual transaction management
- Explain session lifecycle and automatic cleanup
- Show how to use getSession() for custom operations

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
- Add examples for manual transaction control
- Add examples for auto-commit mode
- Add examples showing session lifecycle
- Demonstrate how to use getSession() for custom operations

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
- Make session property optional (session?: ClientSession)
- Remove unsafe 'as any' type casts
- Use proper undefined assignment

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Enhance session management in newSyncedAdapter API Fix MongoDB session lifecycle management in synced adapter Dec 9, 2025
Copilot AI requested a review from hsluoyz December 9, 2025 13:39
@hsluoyz hsluoyz closed this Dec 9, 2025
@hsluoyz hsluoyz reopened this Dec 9, 2025
@hsluoyz hsluoyz closed this Dec 9, 2025
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

Successfully merging this pull request may close these issues.

[feature] better session management for newSyncedAdapter()

3 participants