-
Notifications
You must be signed in to change notification settings - Fork 29
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
Fix finalizers when Python futures are enabled #318
Conversation
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.
I think this looks good with one question about lock ordering
shared_from_this().get()); | ||
std::lock_guard<std::mutex> lock(_futuresPoolMutex); | ||
PyGILState_STATE state = PyGILState_Ensure(); | ||
decltype(_futuresPool) newFuturesPool; |
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.
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?
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.
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.
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. |
/merge |
The Python futures pool retains a reference to
_notifier
, which prevents further cleanup of theucxx::{python::,}Worker
. Thus a newclearFuturesPool
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 globalProgressTasks
dictionary. Having the progress tasks as a member attribute ofApplicationContext
forces us to keep a reference to it that prevents from properly cleaning up in theweakref.finalize
. Using an external object allows us to prevent the self-reference and clean it up properly when no further references toApplicationContext
exist orucxx.reset()
is called.