@@ -487,15 +487,20 @@ def collection_names(self, include_system_collections=True):
487
487
will not include system collections (e.g ``system.indexes``)
488
488
"""
489
489
with self .__client ._socket_for_reads (
490
- ReadPreference .PRIMARY ) as (sock_info , slave_okay ):
490
+ ReadPreference .PRIMARY ) as (sock_info , slave_okay ):
491
491
492
+ wire_version = sock_info .max_wire_version
492
493
results = self ._list_collections (sock_info , slave_okay )
493
- names = [result ["name" ] for result in results ]
494
- if sock_info .max_wire_version <= 2 :
495
- # MongoDB 2.4 and older return index namespaces and collection
496
- # namespaces prefixed with the database name.
497
- names = [n [len (self .__name ) + 1 :] for n in names
498
- if n .startswith (self .__name + "." ) and "$" not in n ]
494
+
495
+ # Iterating the cursor to completion may require a socket for getmore.
496
+ # Ensure we do that outside the "with" block so we don't require more
497
+ # than one socket at a time.
498
+ names = [result ["name" ] for result in results ]
499
+ if wire_version <= 2 :
500
+ # MongoDB 2.4 and older return index namespaces and collection
501
+ # namespaces prefixed with the database name.
502
+ names = [n [len (self .__name ) + 1 :] for n in names
503
+ if n .startswith (self .__name + "." ) and "$" not in n ]
499
504
500
505
if not include_system_collections :
501
506
names = [name for name in names if not name .startswith ("system." )]
0 commit comments