Skip to content

PYTHON-5492 Fix handling of MaxTimeMSExpired responses #2477

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 3 commits into from
Aug 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 .evergreen/scripts/install-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ if ! command -v just &>/dev/null; then
_TARGET="--target x86_64-pc-windows-msvc"
fi
_BIN_DIR=$PYMONGO_BIN_DIR
mkdir -p ${_BIN_DIR}
echo "Installing just..."
mkdir -p "$_BIN_DIR" 2>/dev/null || true
curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash -s -- $_TARGET --to "$_BIN_DIR" || {
Expand All @@ -59,6 +60,7 @@ fi
# Ensure uv is installed.
if ! command -v uv &>/dev/null; then
_BIN_DIR=$PYMONGO_BIN_DIR
mkdir -p ${_BIN_DIR}
echo "Installing uv..."
# On most systems we can install directly.
curl -LsSf https://astral.sh/uv/install.sh | env UV_INSTALL_DIR="$_BIN_DIR" INSTALLER_NO_MODIFY_PATH=1 sh || {
Expand Down
5 changes: 5 additions & 0 deletions pymongo/asynchronous/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
_SDAMStatusMessage,
)
from pymongo.message import _convert_exception, _GetMore, _OpMsg, _Query
from pymongo.pool_shared import _get_timeout_details, format_timeout_details
from pymongo.response import PinnedResponse, Response

if TYPE_CHECKING:
Expand Down Expand Up @@ -224,6 +225,10 @@ async def run_operation(
if use_cmd:
first = docs[0]
await operation.client._process_response(first, operation.session) # type: ignore[misc, arg-type]
# Append timeout details to MaxTimeMSExpired responses.
if first.get("code") == 50:
timeout_details = _get_timeout_details(conn.opts) # type:ignore[has-type]
first["errmsg"] += format_timeout_details(timeout_details) # type:ignore[index]
Copy link
Member

@ShaneHarvey ShaneHarvey Aug 18, 2025

Choose a reason for hiding this comment

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

This will fail if the document_class is RawBSONDocument:

>>> from bson.raw_bson import *
>>> c = MongoClient(document_class=RawBSONDocument)
>>> _ = c.t.t.insert_many([{'x':i} for i in range(1000000)])
>>> c.t.t.find_one({'x': -1}, maxTimeMS=1)
...
  File "/Users/shane/git/mongo-python-driver/pymongo/synchronous/server.py", line 231, in run_operation
    first["errmsg"] += format_timeout_details(timeout_details)  # type:ignore[index]
    ~~~~~^^^^^^^^^^
TypeError: 'RawBSONDocument' object does not support item assignment

_check_command_response(first, conn.max_wire_version)
except Exception as exc:
duration = datetime.now() - start
Expand Down
5 changes: 5 additions & 0 deletions pymongo/synchronous/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
_SDAMStatusMessage,
)
from pymongo.message import _convert_exception, _GetMore, _OpMsg, _Query
from pymongo.pool_shared import _get_timeout_details, format_timeout_details
from pymongo.response import PinnedResponse, Response
from pymongo.synchronous.helpers import _handle_reauth

Expand Down Expand Up @@ -224,6 +225,10 @@ def run_operation(
if use_cmd:
first = docs[0]
operation.client._process_response(first, operation.session) # type: ignore[misc, arg-type]
# Append timeout details to MaxTimeMSExpired responses.
if first.get("code") == 50:
timeout_details = _get_timeout_details(conn.opts) # type:ignore[has-type]
first["errmsg"] += format_timeout_details(timeout_details) # type:ignore[index]
_check_command_response(first, conn.max_wire_version)
except Exception as exc:
duration = datetime.now() - start
Expand Down
1 change: 0 additions & 1 deletion test/asynchronous/test_pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ async def find_one():
# maxConnecting = unbounded: 30+ connections in ~0.140+ seconds
print(len(pool.conns))

@flaky(reason="PYTHON-5492")
@async_client_context.require_failCommand_appName
async def test_csot_timeout_message(self):
client = await self.async_rs_or_single_client(appName="connectionTimeoutApp")
Expand Down
1 change: 0 additions & 1 deletion test/test_pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ def find_one():
# maxConnecting = unbounded: 30+ connections in ~0.140+ seconds
print(len(pool.conns))

@flaky(reason="PYTHON-5492")
@client_context.require_failCommand_appName
def test_csot_timeout_message(self):
client = self.rs_or_single_client(appName="connectionTimeoutApp")
Expand Down
Loading