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

bpo-36867: Make semaphore_tracker track other system resources. #13222

Merged
merged 20 commits into from
May 10, 2019

Conversation

pierreglaser
Copy link
Contributor

@pierreglaser pierreglaser commented May 9, 2019

Primary Purpose

This PR proposes to extend the semaphore_tracker range of action to other system resources.
Currently in python3.8, if a process that created a shared_memory object gets violently terminated (with kill for example), the memory segment will not be properly released until the next machine reboot. This will be problematic for long-running clusters used by many different users.

The primary reason of this PR is thus to extend the semaphore_tracker to track also shared_memory segments. The semaphore_tracker module gets consequently renamed resource_tracker.

Extension to a potentially public API

Additionally, the design of the resource_tracker is more generic, and possibly suggests a new public API to enable the tracking other named-resources. For now, the private _CLEANUP_FUNCS dict references all the types that can possibly be tracked.

To start tracking a new type, the resource_tracker only needs to know which cleanup routine to apply to each leaked instance of this type after the python session ends. The public API would thus consist of a function like resource_tracker.register_resource_type(resource_type, cleanup_func), where:

  • resource_type is a string
  • cleanup_func must be a callable taking a single string as an argument.

Under the hood, register_resource_type would populate the CLEANUP_FUNCS with the new (type, cleanup_func) record.

Operating System Availability

To ease the review process of this PR. the resource_tracker is now POSIX only. I however have a functional windows implementation that I will provide upon demand :)

pinging @pitrou @ogrisel @tomMoral

@pablogsal this may interest you as well :)

https://bugs.python.org/issue36867

@pitrou
Copy link
Member

pitrou commented May 9, 2019

You may want to take a look at the CI failures.

@pierreglaser
Copy link
Contributor Author

Yes: turns out we don't want a Finalizer for SharedMemory ATM (as opposed to SemLock)

Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

This PR looks mostly good to me. I have just one comment and a question.

Lib/multiprocessing/resource_tracker.py Outdated Show resolved Hide resolved
Lib/multiprocessing/resource_tracker.py Outdated Show resolved Hide resolved
@pierreglaser
Copy link
Contributor Author

pierreglaser commented May 10, 2019

Some more comment about OS availability: On windows, it looks that even with no resource_tracker, abruptly terminating process does not leak shared_memory. So as long as the resource_tracker only tracks SemLocks and shared_memory objects, it is useless for windows.
The tests added in pierreglaser#3 make sure no shared memory leakage happens on all platform, independently of the resource_tracker.

@pitrou
Copy link
Member

pitrou commented May 10, 2019

Indeed, it seems that shared memory on Windows is more cleverly implemented than on Unix.

@pitrou
Copy link
Member

pitrou commented May 10, 2019

cc @applio just in case he wants to confirm.

Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

A couple more nits :-)

Lib/test/_test_multiprocessing.py Outdated Show resolved Hide resolved
Lib/multiprocessing/resource_tracker.py Outdated Show resolved Hide resolved
Lib/multiprocessing/resource_tracker.py Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

@pierreglaser
Copy link
Contributor Author

I have made the requested changes; please review again.
(just making the bot happy)

@bedevere-bot
Copy link

Thanks for making the requested changes!

@pitrou: please review the changes made to this pull request.

@pierreglaser
Copy link
Contributor Author

Also @pitrou, before potentially merging, any opinion on pierreglaser#3? It adds shared-memory leaking tests for all platforms, independently of whether the resource_tracker exist for this platform or not.

@pierreglaser
Copy link
Contributor Author

Note also that there is still work to do to properly release shared memory segments created with a SharedMemoryManager after an abrupt interruption of a python session, but this is independent of this PR (but not independent of #12483)

@pitrou pitrou merged commit f22cc69 into python:master May 10, 2019
@pitrou
Copy link
Member

pitrou commented May 10, 2019

Thanks @pierreglaser !

@pierreglaser
Copy link
Contributor Author

Thanks @pitrou for the infallible reviews ;)

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.

4 participants