PYTHON-5168 Use logging for client background task errors#2166
Merged
ShaneHarvey merged 7 commits intomongodb:masterfrom Mar 5, 2025
Merged
PYTHON-5168 Use logging for client background task errors#2166ShaneHarvey merged 7 commits intomongodb:masterfrom
ShaneHarvey merged 7 commits intomongodb:masterfrom
Conversation
Member
Author
|
Hmm it looks like this change somehow makes It could be a coincidence but it happened 3 times in the last PR patch. |
Member
Author
|
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 connectLooks like a race in the new test. |
Member
Author
|
@NoahStapp this is ready for review now. |
NoahStapp
requested changes
Mar 5, 2025
| def _log_client_error() -> None: | ||
| logger = _CLIENT_LOGGER | ||
| if logger: | ||
| logger.exception("MongoClient background task encountered an error:") |
Contributor
There was a problem hiding this comment.
Is this method supposed to also print the actual exception?
Member
Author
There was a problem hiding this comment.
logger.exception already includes the full traceback (see the PR description for an example).
Contributor
There was a problem hiding this comment.
Can you add a short comment saying that here for clarity?
NoahStapp
approved these changes
Mar 5, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PYTHON-5168 Use logging for client background task errors
Old behavior we printed to sys.stderr:
New behavior we log via logging.exception():