Skip to content

MongoClient causes pymongo ServerSelectionTimeoutError error #63

@yCobanoglu

Description

@yCobanoglu

Describe the bug
pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused.
This used to work in the past due to (probably ?!) System update suddenly stopped working.
For generic linux with version 4 everything works as expected.

To Reproduce
Steps to reproduce the behavior:
Packages needed: pytest==7.2.0, pymongo==4.3.3
ENV Variables:
PYMONGOIM__MONGO_VERSION=4.4;
PYMONGOIM__OPERATING_SYSTEM=ubuntu;
PYMONGOIM__OS_VERSION=20

import uuid
import pytest
from pymongo_inmemory import MongoClient
from pymongo.database import Database

COLLECTION = "test"

class MongoCollection:
    def __init__(self, database: Database, collection: str):
        self.__database = database
        self.__collection = collection

    def __get_collection(self):
        return self.__database[self.__collection]

    def insert_one(self, document):
        return self.__get_collection().insert_one(document).inserted_id

    def find_one(self, query, *args, **kwargs):
        return self.__get_collection().find_one(query, *args, **kwargs)


class MongoRepository:
    def __init__(self, database: Database) -> None:
        self._collection = MongoCollection(database, COLLECTION)

    def create(self, test) -> None:
        self._collection.insert_one(test)

    def find(self, query):
        return self._collection.find_one(query)

@pytest.fixture
def mongo_client() -> MongoClient:
    return MongoClient()

@pytest.fixture
def database(mongo_client: MongoClient):
    database = str(uuid.uuid4())
    yield mongo_client[database]
    mongo_client.drop_database(database)

@pytest.fixture
def repository(database):
    return MongoRepository(database)

def test_create(database, repository):
    repository.create({"_id":"id-0", "test": "test"})
    assert database[COLLECTION].find_one({"_id": "id-0"}) == {"_id":"id-0", "test": "test"}
test setup failed
@pytest.fixture
    def mongo_client() -> MongoClient:
>       return MongoClient()

test.py:12: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../.venv/lib/pypy3.9/site-packages/pymongo_inmemory/_pim.py:9: in __init__
    self._mongod.start()
../../.venv/lib/pypy3.9/site-packages/pymongo_inmemory/mongod.py:127: in start
    while not self.is_healthy:
../../.venv/lib/pypy3.9/site-packages/pymongo_inmemory/mongod.py:165: in is_healthy
    status = db.command("serverStatus")
../../.venv/lib/pypy3.9/site-packages/pymongo/_csot.py:105: in csot_wrapper
    return func(self, *args, **kwargs)
../../.venv/lib/pypy3.9/site-packages/pymongo/database.py:805: in command
    with self.__client._socket_for_reads(read_preference, session) as (
../../.venv/lib/pypy3.9/site-packages/pymongo/mongo_client.py:1296: in _socket_for_reads
    server = self._select_server(read_preference, session)
../../.venv/lib/pypy3.9/site-packages/pymongo/mongo_client.py:1257: in _select_server
    server = topology.select_server(server_selector)
../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:272: in select_server
    server = self._select_server(selector, server_selection_timeout, address)
../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:261: in _select_server
    servers = self.select_servers(selector, server_selection_timeout, address)
../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:223: in select_servers
    server_descriptions = self._select_servers_loop(selector, server_timeout, address)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <Topology <TopologyDescription id: 63a05700067dbc0bfabfdd47, topology_type: Unknown, servers: [<ServerDescription ('12....1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 111] Connection refused')>]>>
selector = Primary(), timeout = 30, address = None

    def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = time.monotonic()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector
        )
    
        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
>               raise ServerSelectionTimeoutError(
                    "%s, Timeout: %ss, Topology Description: %r"
                    % (self._error_message(selector), timeout, self.description)
                )
E               pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 63a05700067dbc0bfabfdd47, topology_type: Unknown, servers: [<ServerDescription ('127.0.0.1', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27017: [Errno 111] Connection refused')>]>

../../.venv/lib/pypy3.9/site-packages/pymongo/topology.py:238: ServerSelectionTimeoutError

Context:

  • OS: Arch Linux
  • Version of pymongo_inmemory 0.2.11
  • Version of mongo you are downloading 4.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions