Skip to content
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-4782 Fix deadlock and blocking behavior in _ACondition.wait #1875

Merged
merged 12 commits into from
Sep 30, 2024

Conversation

ShaneHarvey
Copy link
Member

@ShaneHarvey ShaneHarvey commented Sep 23, 2024

PYTHON-4782 Fix deadlock and blocking behavior in _ACondition.wait

TODO:

@ShaneHarvey
Copy link
Member Author

Hmm, these changes are causing a few test regressions. I believe it's due to the new behavior of server selection because of the topology lock.wait(). Previously, server selection would block the loop entirely for 500ms on each iteration where a server could not be selected. Now, we correctly wait without blocking the loop.

@ShaneHarvey ShaneHarvey marked this pull request as ready for review September 26, 2024 18:45
@ShaneHarvey
Copy link
Member Author

Requesting review now, I'll keep investigating the test failures.

await asyncio.wait_for(fut, timeout)
return True
except asyncio.TimeoutError:
return False # Return false on timeout for sync pool compat.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need to acquire the lock if we timeout here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the API contract for wait() says you MUST hold the lock before calling and you MUST still hold the lock when it returns, even on timeout.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, so you always have to acquire the lock if you call wait and don't raise an error?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment linking to the API contract here? It would be good to make it more understandable for readers unfamiliar with it.

Copy link
Member Author

Choose a reason for hiding this comment

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

There's already this comment:

# Must re-acquire lock even if wait is cancelled.

loop = asyncio.get_running_loop()
fut = loop.create_future()
self._waiters.append((loop, fut))
self.release()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this just to ensure we don't hold the lock while waiting for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes without releasing the lock, this code would deadlock since nothing would be able to notify the waiter.

err = None
while True:
try:
await self.acquire()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this possibly loop forever?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes but only if something else holds the lock forever.

Copy link
Contributor

Choose a reason for hiding this comment

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

And we're asserting that this new code can't do that, got it!

NoahStapp
NoahStapp previously approved these changes Sep 26, 2024
Copy link
Contributor

@NoahStapp NoahStapp left a comment

Choose a reason for hiding this comment

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

Code looks good, approved contingent on tests!

@blink1073
Copy link
Member

The failures seem relevant: test.asynchronous.test_cursor.TestCursor.test_to_list_csot_applied

@blink1073
Copy link
Member

It is the same failure across several tasks that all previously passed.

@ShaneHarvey
Copy link
Member Author

Yes I'm looking into test_to_list_csot_applied today.

@ShaneHarvey
Copy link
Member Author

ShaneHarvey commented Sep 27, 2024

I'm running a patch with both #1870 and this PR to see if the failures will work themselves out: https://spruce.mongodb.com/version/66f712658ea18700077002df/tasks

Edit: this one also has some failures. I pushed a fix for test_reconnect. Although I am a little concerned by these failures. One explanation is that an AsyncMongoClient takes much longer to process SDAM updates (eg on init or when rediscovering a server after an error).

@ShaneHarvey
Copy link
Member Author

Bad timing for docker to start failing...

  ERROR: failed to solve: docker:stable: failed to resolve source metadata for docker.io/library/docker:stable: failed to authorize: failed to fetch oauth token: unexpected status from GET request to https://auth.docker.io/token?scope=repository%3Alibrary%2Fdocker%3Apull&service=registry.docker.io: 401 Unauthorized

@ShaneHarvey
Copy link
Member Author

New patch with both #1870 and this PR: https://spruce.mongodb.com/version/66f73f178ea187000770b298/

@ShaneHarvey
Copy link
Member Author

This is ready for another look.

Copy link
Contributor

@NoahStapp NoahStapp left a comment

Choose a reason for hiding this comment

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

Increased timeouts make sense, still seeing test failures--do you think they're unrelated?

@blink1073
Copy link
Member

The Windows failures seem relevant, since they're in test.asynchronous.test_locks.TestConditionStdlib.

@ShaneHarvey
Copy link
Member Author

I fixed the remaining test failures.

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM!

@ShaneHarvey ShaneHarvey merged commit 821811e into mongodb:master Sep 30, 2024
29 of 30 checks passed
blink1073 pushed a commit that referenced this pull request Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants