-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-5306 - Fix use of public MongoClient attributes before connection #2285
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aa86a3a
PYTHON-5306 - Fix use of public MongoClient attributes before connection
NoahStapp c185c95
Address review
NoahStapp 763e78c
Merge branch 'master' into PYTHON-5306
NoahStapp 66b7e8d
Fix topology_description
NoahStapp 5f7f84a
Fix SRV test
NoahStapp ed3dea5
Remove type: ignore
NoahStapp 99802be
Address review
NoahStapp 14bb119
Create TopologySettings always
NoahStapp c1e6684
Update changelog
NoahStapp 2a63a3b
Merge branch 'master' into PYTHON-5306
NoahStapp 835e560
Update changelog
NoahStapp e5e6dc8
Trailing quote
NoahStapp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,7 @@ | |
) | ||
from pymongo.read_preferences import ReadPreference, _ServerMode | ||
from pymongo.results import ClientBulkWriteResult | ||
from pymongo.server_description import ServerDescription | ||
from pymongo.server_selectors import writable_server_selector | ||
from pymongo.server_type import SERVER_TYPE | ||
from pymongo.topology_description import TOPOLOGY_TYPE, TopologyDescription | ||
|
@@ -779,7 +780,7 @@ def __init__( | |
keyword_opts["document_class"] = doc_class | ||
self._resolve_srv_info: dict[str, Any] = {"keyword_opts": keyword_opts} | ||
|
||
seeds = set() | ||
self._seeds = set() | ||
is_srv = False | ||
username = None | ||
password = None | ||
|
@@ -804,18 +805,18 @@ def __init__( | |
srv_max_hosts=srv_max_hosts, | ||
) | ||
is_srv = entity.startswith(SRV_SCHEME) | ||
seeds.update(res["nodelist"]) | ||
self._seeds.update(res["nodelist"]) | ||
username = res["username"] or username | ||
password = res["password"] or password | ||
dbase = res["database"] or dbase | ||
opts = res["options"] | ||
fqdn = res["fqdn"] | ||
else: | ||
seeds.update(split_hosts(entity, self._port)) | ||
if not seeds: | ||
self._seeds.update(split_hosts(entity, self._port)) | ||
if not self._seeds: | ||
raise ConfigurationError("need to specify at least one host") | ||
|
||
for hostname in [node[0] for node in seeds]: | ||
for hostname in [node[0] for node in self._seeds]: | ||
if _detect_external_db(hostname): | ||
break | ||
|
||
|
@@ -838,7 +839,7 @@ def __init__( | |
srv_service_name = opts.get("srvServiceName", common.SRV_SERVICE_NAME) | ||
|
||
srv_max_hosts = srv_max_hosts or opts.get("srvmaxhosts") | ||
opts = self._normalize_and_validate_options(opts, seeds) | ||
opts = self._normalize_and_validate_options(opts, self._seeds) | ||
|
||
# Username and password passed as kwargs override user info in URI. | ||
username = opts.get("username", username) | ||
|
@@ -857,7 +858,7 @@ def __init__( | |
"username": username, | ||
"password": password, | ||
"dbase": dbase, | ||
"seeds": seeds, | ||
"seeds": self._seeds, | ||
"fqdn": fqdn, | ||
"srv_service_name": srv_service_name, | ||
"pool_class": pool_class, | ||
|
@@ -873,8 +874,7 @@ def __init__( | |
self._options.read_concern, | ||
) | ||
|
||
if not is_srv: | ||
self._init_based_on_options(seeds, srv_max_hosts, srv_service_name) | ||
self._init_based_on_options(self._seeds, srv_max_hosts, srv_service_name) | ||
|
||
self._opened = False | ||
self._closed = False | ||
|
@@ -975,6 +975,7 @@ def _init_based_on_options( | |
srv_service_name=srv_service_name, | ||
srv_max_hosts=srv_max_hosts, | ||
server_monitoring_mode=self._options.server_monitoring_mode, | ||
topology_id=self._topology_settings._topology_id if self._topology_settings else None, | ||
) | ||
if self._options.auto_encryption_opts: | ||
from pymongo.asynchronous.encryption import _Encrypter | ||
|
@@ -1205,6 +1206,16 @@ def topology_description(self) -> TopologyDescription: | |
|
||
.. versionadded:: 4.0 | ||
""" | ||
if self._topology is None: | ||
servers = {(host, port): ServerDescription((host, port)) for host, port in self._seeds} | ||
return TopologyDescription( | ||
TOPOLOGY_TYPE.Unknown, | ||
servers, | ||
None, | ||
None, | ||
None, | ||
self._topology_settings, | ||
) | ||
return self._topology.description | ||
|
||
@property | ||
|
@@ -1218,6 +1229,8 @@ def nodes(self) -> FrozenSet[_Address]: | |
to any servers, or a network partition causes it to lose connection | ||
to all servers. | ||
""" | ||
if self._topology is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the docstring defines, we expect |
||
return frozenset() | ||
description = self._topology.description | ||
return frozenset(s.address for s in description.known_servers) | ||
|
||
|
@@ -1576,6 +1589,8 @@ async def address(self) -> Optional[tuple[str, int]]: | |
|
||
.. versionadded:: 3.0 | ||
""" | ||
if self._topology is None: | ||
await self._get_topology() | ||
topology_type = self._topology._description.topology_type | ||
if ( | ||
topology_type == TOPOLOGY_TYPE.Sharded | ||
|
@@ -1598,6 +1613,8 @@ async def primary(self) -> Optional[tuple[str, int]]: | |
.. versionadded:: 3.0 | ||
AsyncMongoClient gained this property in version 3.0. | ||
""" | ||
if self._topology is None: | ||
await self._get_topology() | ||
return await self._topology.get_primary() # type: ignore[return-value] | ||
|
||
@property | ||
|
@@ -1611,6 +1628,8 @@ async def secondaries(self) -> set[_Address]: | |
.. versionadded:: 3.0 | ||
AsyncMongoClient gained this property in version 3.0. | ||
""" | ||
if self._topology is None: | ||
await self._get_topology() | ||
return await self._topology.get_secondaries() | ||
|
||
@property | ||
|
@@ -1621,6 +1640,8 @@ async def arbiters(self) -> set[_Address]: | |
connected to a replica set, there are no arbiters, or this client was | ||
created without the `replicaSet` option. | ||
""" | ||
if self._topology is None: | ||
await self._get_topology() | ||
return await self._topology.get_arbiters() | ||
|
||
@property | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the client has not connected yet, return a
TopologyDescription
with placeholder servers derived from the seeds passed to the client's constructor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make sure the returned TopologyDescription has the same _topology_id. Otherwise it could be confusing because _topology_id will change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also can you give an example of what topology_description looks like in the connect=False srv case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<TopologyDescription id: 67ffb68a6e12ca176991f521, topology_type: Unknown, servers: [<ServerDescription (('test1.test.build.10gen.cc', None), 27017) server_type: Unknown, rtt: None>]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please include an example of this new/old behavior in the Jira ticket when closing.