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

Fix finalizers when Python futures are enabled #318

Merged

Conversation

pentschev
Copy link
Member

The Python futures pool retains a reference to _notifier, which prevents further cleanup of the ucxx::{python::,}Worker. Thus a new clearFuturesPool method is introduced to allow clearing the pool so that these references are dropped, which is called before the Python _notifierThread exits.

Additionally, move ApplicationContext.progress_tasks to a global ProgressTasks dictionary. Having the progress tasks as a member attribute of ApplicationContext forces us to keep a reference to it that prevents from properly cleaning up in the weakref.finalize. Using an external object allows us to prevent the self-reference and clean it up properly when no further references to ApplicationContext exist or ucxx.reset() is called.

@pentschev pentschev added bug Something isn't working non-breaking Introduces a non-breaking change labels Nov 12, 2024
@pentschev pentschev requested review from a team as code owners November 12, 2024 14:30
Copy link
Contributor

@wence- wence- left a comment

Choose a reason for hiding this comment

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

I think this looks good with one question about lock ordering

Comment on lines +80 to +83
shared_from_this().get());
std::lock_guard<std::mutex> lock(_futuresPoolMutex);
PyGILState_STATE state = PyGILState_Ensure();
decltype(_futuresPool) newFuturesPool;
Copy link
Contributor

Choose a reason for hiding this comment

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

note: I guess this also applies to populateFuturesPool as well.

This function takes two locks, so we need to think about lock ordering. Is there a scenario where a thread holding the GIL might be attempting to call getFuture (which will need to acquire _futuresPoolMutex), while a different thread not holding the GIL has called clearFuturesPool and therefore holds the lock => deadlock?

Or is it the case that only one python thread is allowed to talk to these worker functions?

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently yes, only one Python thread is supported (aka, tested/used). However, this is a very much valid concern as we have almost everything in place for multi-thread Python. I've filed #325 to track that and fix it in a follow-up PR.

@pentschev
Copy link
Member Author

Thanks Lawrence for this review as well. As mentioned above I've filed an issue to think better about the double lock case, it shouldn't be a problem for our current use cases though so I'll go ahead and merge this for now.

@pentschev
Copy link
Member Author

/merge

@rapids-bot rapids-bot bot merged commit c4e8e04 into rapidsai:branch-0.41 Nov 15, 2024
68 checks passed
@pentschev pentschev deleted the fix-python-future-finalizer branch November 15, 2024 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working non-breaking Introduces a non-breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants