Skip to content

bpo-30599: Fix a refleak in test_threaded_import #2001

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

Closed
wants to merge 1 commit into from
Closed

bpo-30599: Fix a refleak in test_threaded_import #2001

wants to merge 1 commit into from

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jun 8, 2017

Fix a reference leaks in test_threaded_import.

Replace "import random" with "import types" to avoid the following call of random.py, since "atfork" callbacks cannot be unregistered:

os.register_at_fork(_inst.seed, when='child')

Check if types.ModuleType attribute is available, rather than trying to call random.randrange().

@vstinner vstinner added the tests Tests in the Lib/test dir label Jun 8, 2017
@vstinner
Copy link
Member Author

vstinner commented Jun 8, 2017

@pitrou: It seems like you wrote some of test_threaded_import tests, especially test_side_effect_import(): commit ea3eb88, http://bugs.python.org/issue9260.

Do you think that testing the availability of the types.ModuleType attribute is enough to check for non-regression?

@pitrou
Copy link
Member

pitrou commented Jun 8, 2017

I don't think test_side_effect_import would be a problem, it only does a regular import.

# complains several times about module random having no attribute
# randrange, and then Python hangs.
# complains several times about module types having no attribute
# ModuleType, and then Python hangs.
Copy link
Member

Choose a reason for hiding this comment

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

Is it something that actually happens?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can only say that the comment was written 16 years ago by @tim-one, commit aa22223... The import machinery was deeply modified since this commit was made... I'm not sure that the test still makes sense :-p

@serhiy-storchaka
Copy link
Member

Could you use less common module (e.g. colorsys)? Unloading a module that is used by say unittest or libregrtest can cause problems. In particular types is directly imported by modulefinder, so importing it after importing modulefinder doesn't have any effect.

@pitrou
Copy link
Member

pitrou commented Jun 8, 2017

Or, better, write a custom module that does a simplified version of what random does at import...

@vstinner
Copy link
Member Author

vstinner commented Jun 8, 2017

Or, better, write a custom module that does a simplified version of what random does at import...

Since I don't undertand why was the import problem, I don't see how to write a module which would reproduce the bug.

Fix reference leaks in test_threaded_import.

Replace "import random" with "import colorsys" to avoid the following
call of random.py, since "atfork" callbacks cannot be unregistered:

    os.register_at_fork(_inst.seed, when='child')

Check that calling colorsys.rgb_to_hls() doesn't crash, rather than
trying to call random.randrange().
@vstinner
Copy link
Member Author

vstinner commented Jun 9, 2017

Serhiy: "Could you use less common module (e.g. colorsys)?"

Ok, done. I like colorsys since it has no dependency and its code is non-trivial. So we can keep a test trying to call a function (colorsys.rgb_to_hls()) of an imported module, rather than just testing if an attribute is available (types.ModuleType).

Does it look better now?

@pitrou
Copy link
Member

pitrou commented Jun 9, 2017

I like colorsys since it has no dependency and its code is non-trivial.

The problem is this isn't exercising anything anymore.

@pitrou
Copy link
Member

pitrou commented Jun 9, 2017

Since I don't undertand why was the import problem, I don't see how to write a module which would reproduce the bug.

Typically import a bunch of modules at top-level (just copy the imports done at the top of random.py), then create a singleton instance of something and bind one of its methods at the top-level, like random.py does. Then in the test, check you can call the top-level method.

@vstinner
Copy link
Member Author

vstinner commented Jun 9, 2017

Typically import a bunch of modules at top-level (just copy the imports done at the top of random.py), then create a singleton instance of something and bind one of its methods at the top-level, like random.py does. Then in the test, check you can call the top-level method.

Hum, instead of cloning random.py "just to remove the os.register_at_fork() call", I chose a different approach which I already mention: #2029 only mocks os.register_at_fork() and so is less risky.

@vstinner
Copy link
Member Author

Superseded by the PR #2029 which has been merged.

@vstinner vstinner closed this Jun 13, 2017
@vstinner vstinner deleted the test_side_effect_import branch June 13, 2017 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants