Skip to content

PYTHON-5488 append_metadata should not add duplicates #2461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pymongo/pool_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ def __init__(

def _update_metadata(self, driver: DriverInfo) -> None:
"""Updates the client's metadata"""
if driver.name.lower() in self.__metadata["driver"]["name"].lower().split("|"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to check for driver.name being None before calling lower().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the type of DriverInfo.name shouldn't be none. Would the check here be enforcing that the caller is following that rule?

Copy link
Contributor

@NoahStapp NoahStapp Aug 8, 2025

Choose a reason for hiding this comment

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

We check on line 393 that driver.name exists, so extending the same check here makes sense. We don't explicitly prevent driver.name from being None, so I'd prefer to err on the safe side to prevent errors being thrown by misbehaving users.

return

metadata = copy.deepcopy(self.__metadata)
if driver.name:
Expand Down
40 changes: 40 additions & 0 deletions test/asynchronous/test_client_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ async def test_doesnt_update_established_connections(self):
self.assertIsNone(self.handshake_req)
self.assertEqual(listener.event_count(ConnectionClosedEvent), 0)

async def test_duplicate_driver_name_no_op(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we refactor check_metadata_added to support checking that the new metadata is the same as the old metadata? Most of this method is just duplicate code from check_metadata_added.

client = await self.async_rs_or_single_client(
"mongodb://" + self.server.address_string,
maxIdleTimeMS=1,
)
client.append_metadata(DriverInfo("library", "1.2", "Library Platform"))
# send initial metadata
name, version, platform, metadata = await self.send_ping_and_get_metadata(client, True)
# wait for connection to become idle
await asyncio.sleep(0.005)

# add new metadata
client.append_metadata(DriverInfo("framework", None, None))
new_name, new_version, new_platform, new_metadata = await self.send_ping_and_get_metadata(
client, True
)
self.assertEqual(new_name, f"{name}|framework")
self.assertEqual(new_version, version)
self.assertEqual(new_platform, platform)

metadata.pop("driver")
metadata.pop("platform")
new_metadata.pop("driver")
new_metadata.pop("platform")
self.assertEqual(metadata, new_metadata)

# wait for connection to become idle
await asyncio.sleep(0.005)
# add same metadata again
client.append_metadata(DriverInfo("Framework", None, None))
(
same_name,
same_version,
same_platform,
same_metadata,
) = await self.send_ping_and_get_metadata(client, True)
self.assertEqual(new_name, same_name)
self.assertEqual(new_version, same_version)
self.assertEqual(new_platform, same_platform)


if __name__ == "__main__":
unittest.main()
40 changes: 40 additions & 0 deletions test/test_client_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ def test_doesnt_update_established_connections(self):
self.assertIsNone(self.handshake_req)
self.assertEqual(listener.event_count(ConnectionClosedEvent), 0)

def test_duplicate_driver_name_no_op(self):
client = self.rs_or_single_client(
"mongodb://" + self.server.address_string,
maxIdleTimeMS=1,
)
client.append_metadata(DriverInfo("library", "1.2", "Library Platform"))
# send initial metadata
name, version, platform, metadata = self.send_ping_and_get_metadata(client, True)
# wait for connection to become idle
time.sleep(0.005)

# add new metadata
client.append_metadata(DriverInfo("framework", None, None))
new_name, new_version, new_platform, new_metadata = self.send_ping_and_get_metadata(
client, True
)
self.assertEqual(new_name, f"{name}|framework")
self.assertEqual(new_version, version)
self.assertEqual(new_platform, platform)

metadata.pop("driver")
metadata.pop("platform")
new_metadata.pop("driver")
new_metadata.pop("platform")
self.assertEqual(metadata, new_metadata)

# wait for connection to become idle
time.sleep(0.005)
# add same metadata again
client.append_metadata(DriverInfo("Framework", None, None))
(
same_name,
same_version,
same_platform,
same_metadata,
) = self.send_ping_and_get_metadata(client, True)
self.assertEqual(new_name, same_name)
self.assertEqual(new_version, same_version)
self.assertEqual(new_platform, same_platform)


if __name__ == "__main__":
unittest.main()
Loading