-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
PYTHON-5168 Use logging for client background task errors #2166
Conversation
Hmm it looks like this change somehow makes
It could be a coincidence but it happened 3 times in the last PR patch. |
One more failure: [2025/03/04 22:25:45.106] _____________ TestClient.test_background_connections_log_on_error ______________
[2025/03/04 22:25:45.106] self = <test.test_client.TestClient testMethod=test_background_connections_log_on_error>
[2025/03/04 22:25:45.106] def test_background_connections_log_on_error(self):
[2025/03/04 22:25:45.106] with self.assertLogs("pymongo.client", level="ERROR") as cm:
[2025/03/04 22:25:45.106] client = self.rs_or_single_client(minPoolSize=1)
[2025/03/04 22:25:45.106] # Create a single connection in the pool.
[2025/03/04 22:25:45.106] client.admin.command("ping")
[2025/03/04 22:25:45.106] # Cause new connections to fail.
[2025/03/04 22:25:45.106] pool = get_pool(client)
[2025/03/04 22:25:45.106] def fail_connect(*args, **kwargs):
[2025/03/04 22:25:45.106] raise Exception("failed to connect")
[2025/03/04 22:25:45.106] pool.connect = fail_connect
[2025/03/04 22:25:45.106] # Un-patch Pool.connect to break the cyclic reference.
[2025/03/04 22:25:45.106] self.addCleanup(delattr, pool, "connect")
[2025/03/04 22:25:45.106] pool.reset_without_pause()
[2025/03/04 22:25:45.106] wait_until(lambda: len(cm.records) > 0, "start creating connections")
[2025/03/04 22:25:45.106] log_output = "".join(cm.output)
[2025/03/04 22:25:45.106] > self.assertIn("failed to connect", log_output)
[2025/03/04 22:25:45.106] E AssertionError: 'failed to connect' not found in ''
[2025/03/04 22:25:45.106] test/test_client.py:1770: AssertionError
[2025/03/04 22:25:45.106] ------------------------------ Captured log call -------------------------------
[2025/03/04 22:25:45.106] ERROR pymongo.client:logger.py:102 MongoClient background task encountered an error:
[2025/03/04 22:25:45.106] Traceback (most recent call last):
[2025/03/04 22:25:45.106] File "/Users/ec2-user/data/mci/059a7dc71a0205aac5d7674a44a4bfa3/src/pymongo/synchronous/mongo_client.py", line 2067, in _process_periodic_tasks
[2025/03/04 22:25:45.106] self._topology.update_pool()
[2025/03/04 22:25:45.106] File "/Users/ec2-user/data/mci/059a7dc71a0205aac5d7674a44a4bfa3/src/pymongo/synchronous/topology.py", line 690, in update_pool
[2025/03/04 22:25:45.106] server.pool.remove_stale_sockets(generation)
[2025/03/04 22:25:45.106] File "/Users/ec2-user/data/mci/059a7dc71a0205aac5d7674a44a4bfa3/src/pymongo/synchronous/pool.py", line 1213, in remove_stale_sockets
[2025/03/04 22:25:45.106] conn = self.connect()
[2025/03/04 22:25:45.107] File "/Users/ec2-user/data/mci/059a7dc71a0205aac5d7674a44a4bfa3/src/test/test_client.py", line 1760, in fail_connect
[2025/03/04 22:25:45.107] raise Exception("failed to connect")
[2025/03/04 22:25:45.107] Exception: failed to connect Looks like a race in the new test. |
@NoahStapp this is ready for review now. |
def _log_client_error() -> None: | ||
logger = _CLIENT_LOGGER | ||
if logger: | ||
logger.exception("MongoClient background task encountered an error:") |
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.
Is this method supposed to also print the actual exception?
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.
logger.exception
already includes the full traceback (see the PR description for an example).
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.
Can you add a short comment saying that here for clarity?
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.
Done.
PYTHON-5168 Use logging for client background task errors
Old behavior we printed to sys.stderr:
New behavior we log via logging.exception():