Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

Fix #15635

Summary

useDb() child connections have their own _lastHeartbeatAt property, but that property is not updated when the client receives a serverHeartbeatSucceeded event. The Connection class only updates the top-level connection's _lastHeartbeatAt. That means useDb() child connections' helpers that rely on _waitForConnect(), like dropDatabase(), will hang after heartbeatFrequencyMS * 2.

I didn't register a separate event listener for each useDb() child connection because of memory leak concerns. With this approach, we don't have to clean up the event listener. Also, this pattern of looping over otherDbs respects the noListener option.

One potential issue: as implemented, noListener means the child connection isn't added to the parent db's otherDbs list, meaning this fix won't work if noListener is set. However, as implemented, the noListener option doesn't work anyway. Setting noListener throws a MongoParseError: option nolistener is not supported error because Mongoose doesn't strip that option out before passing it to MongoDB. So I think the answer here is to just remove the noListener option for mongoose 9.

Examples

@vkarpov15 vkarpov15 added this to the 8.18.2 milestone Sep 22, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug where child database connections created via useDb() don't have their _lastHeartbeatAt property updated when heartbeat events occur, causing operations like dropDatabase() to hang after the heartbeat timeout period.

  • Propagates heartbeat updates from parent connection to all child connections in otherDbs
  • Updates comment to clarify that otherDbs is used for both state changes and heartbeat propagation
  • Adds test case to verify the fix works correctly

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/drivers/node-mongodb-native/connection.js Added heartbeat propagation logic and updated comment
test/connection.test.js Added test case to verify child connections receive heartbeat updates

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@vkarpov15 vkarpov15 merged commit 5c7114e into master Sep 22, 2025
71 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-15635 branch September 23, 2025 06:59
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.

Ready state showing disconnected when using useDb

3 participants