kaizendorks / kaizendorks/pymongo_inmemory

MongoClient causes pymongo ServerSelectionTimeoutError error

Open
#63 4 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Python
Stars
53
Forks
18
PR merge metrics
No merged PRs in 30d

Description

**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

```python
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"}
```

```python
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 = ]>>
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: ]>

../../.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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with pymongo_inmemory/_pim.py and mongod.py, especially MongoClient initialization, mongod.start, and is_healthy, then run the supplied test.py reproduction with the stated Ubuntu 20, MongoDB 4.4, and PyMongo 4.3.3 settings. Trace why the server remains unreachable on Arch Linux. Done means the MongoClient fixture starts successfully and the database test completes without ServerSelectionTimeoutError.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.