GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — bpo-34572: change _pickle unpickling to use import rather than retrieving from sys.modules by tjb900 · Pull Request #9047 · python/cpython Conversation
|
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for your contribution, we look forward to reviewing it! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
c71f99f to
e47c5e1
Compare
|
Hi! Just wondering if there's something I can do to move this along - either that or be told that I'm completely wrong. We are using pure-python unpickling as a workaround for now, but I'd love to have some hope that we can switch back at some point in future. (I notice the same issue appears to be present in @pitrou/pickle5-backport - which I'm very keen to start using!) Thanks! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@tjb900 I realize that this is a thread safety bug which is notoriously hard to test, but I generally find that pull requests that include tests are much easier to review, because it's easier to get a sense of what behavior it fixes and an assurance that there won't be regressions. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return NULL; | ||
| } | ||
| module = PyImport_Import(module_name); | ||
| if (module == NULL) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that the difference in behavior here occurs when PyImport_Import returns NULL without raising an exception. I'm not really sure why this would occur. Is it safe to simply propagate the NULL up to the next level here?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
IIRC (a big if), the issue wasn't that PyImport_Import returned NULL, it's that PyImport_GetModule can return a module object where the body of the module is still being executed (in another thread), and this partially initialised thing is then used immediately by the thread doing the unpickling.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how this fixes things, though. It seems like the only change in behavior is for the case where PyImport_Import returns NULL without raising an exception. Why would that ever happen, and why was it being explicitly handled here in the first place?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In the failing case before, PyImport_Import was never called (at least, not on the thread which causes the error): GetModule returned non-NULL, and this object fell through to the getattribute, which then (sometimes) failed because that attribute did not exist yet.
On L6626 I changed _GetModule -> _Import. This, I believe, invokes the locking logic described here
Line 145 in cd90f6a
| /* Locking primitives to prevent parallel imports of the same module |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
My mistake, I didn't realize that you were changing the import function. I'm still concerned as to why this was added in the first place, and whether this is a wider problem.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Another nitpick, per PEP 7:
Code structure: one space between keywords like if, for and the following left paren; no spaces inside the paren; braces are required everywhere, even where C permits them to be omitted, but do not add them to code you are not otherwise modifying. All new C code requires braces.
So can you add braces around this?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@pganssle Thanks for checking it out. Test case was attached with the bug report, but I didn't include it in the PR - will look at that ASAP. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Looks to me like @ericsnowcurrently added this line when adding It was added in several places, so I'm wondering if this thread safety bug is a problem for all the uses of |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Hmmm. Line 4160 in ca2d610
I'm still trying to identify at what point this became unsafe (have partially initialised modules always been present in |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
It looks to me like the cpickle logic has always been unsafe, but prior to the introduction of per-module import locks for Python 3.3 in https://bugs.python.org/issue9260 it was likely much harder to hit. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
So if I understand this correctly, the current version of the code likely works the way it does for performance reasons, but unfortunately that comes at the cost of thread safety, in which case I think the current patch is likely the right approach (at least for now - maybe If it's possible to make a reliable but not terribly resource-intensive test for this, I think that would be a big help, but that may be a lot to ask. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@tjb900 Thanks for adding that test. I've verified that this test consistently fails when run without the other changes in this PR, and it runs quickly whether or not it succeeds, so it's a +1 from me! One last nitpick, though is that I think it would be better to put it in with the other pickle tests so that people looking to add or change tests will be able to find it more easily (for example there are three or four different test modules where |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| slow_import_module = """ | ||
| import time | ||
|
|
||
| time.sleep(%(delay)s) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This test relies on sleep to work. I do not feel comfortable with such tests as it may fail or not work as intended almost certainly in the slowest or more exotic buildbots that we have. Check out, for example, the discussion in #10413 .
As an additional note, it also needs the reap_threads decorator.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It will make the test a bit more complicated, but I think you can avoid sleep and make this more deterministic by creating a deliberate deadlock like this:
I think there's still a race condition there that doesn't make it deterministic - namely that the thread which is unpickling the object needs to reach the "access the imported attribute" stage before lock A is released in order for it to be a good test, but I think if you fiddle around with the locks and threads a little bit you can get it to work.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'll be more radical and say I don't think we need a test at all, if we can't make it simpler.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
@pitrou I think removing the test because it's not guaranteed to test the thing it's intended to test would be a mistake. This still successfully tests what it's intending to test with high probability, which means that a regression would be very unlikely to ever hit master. Unlike other tests which cause erroneous failures, this causes erroneous successes, so its flakiness is unlikely to cause any erroneously broken buildbots.
If left in as a "flaky" test, I'd probably view it similar to a hypothesis test, which is that it's a probabilistic test that never has false negatives - if the test succeeds you are only partially certain that you have no bugs, but if it fails you know you have a bug.
Elsewhere you mention adding a comment about why PyImport_GetModule was dropped to prevent regressions - if it's worth a comment (which is not guaranteed to work), it's probably worth at least trying to enforce this with a test. Possibly with a comment that says why the test isn't guaranteed to detect regressions.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Well, there are corner cases that we fix without writing tests for, because they are too tricky to reproduce. I think this one is a bit borderline: it's possible to reproduce, but nevertheless it needs a complex machinery.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I certainly agree that if it's likely to have a negative impact on the test suite for reasons other than that it's not guaranteed to fail in the case of a regression the test should probably be considered optional.
I do think it's worth trying to get a test for this in the test suite, even if just as an exercising in providing a good example of how to test a race condition.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| results = [] | ||
| forget("slowimport") | ||
| self.assertNotIn("slowimport", sys.modules) | ||
| importlib.invalidate_caches() |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Can this affect any of the importlib tests?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Any advice on how I can address this? FWIW I pretty much copied this test from test_threaded_import.py.
I haven't tested without that line, so it may not be necessary. I'll see if the test works (i.e. fails without the patch) reliably without it.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Actually, I don't know why it would affect the importlib tests (I'm the one who added that function IIRC).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| module = PyImport_GetModule(module_name); | ||
| module = PyImport_Import(module_name); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Could you benchmark how slower this is compared with just using PyImport_GetModule?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
git master:
$ ./python -m timeit -s "import pickle, logging; d = pickle.dumps((logging.Logger, logging.Handler, logging.Filter, logging.Formatter))" "pickle.loads(d)"
100000 loops, best of 5: 2.55 usec per loop
This PR:
$ ./python -m timeit -s "import pickle, logging; d = pickle.dumps((logging.Logger, logging.Handler, logging.Filter, logging.Formatter))" "pickle.loads(d)"
50000 loops, best of 5: 4.84 usec per loop
So this is a sizable performance drop, but on a micro-benchmark. I don't think this will be significant in real-world cases. Usually, pickle performance becomes important when large pieces of data are serialized, and by construction you don't get many STACK_GLOBAL opcodes in it.
cc'ing @pierreglaser @ogrisel out of curiosity.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
+1. Also, STACK_GLOBAL should tend to progressively vanish in the pickle string, in favor of BINGET as functions/classes get progressively added to the memo (if used).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
I agree with @pganssle in that this test should be placed with the rest of the pickle tests. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| slow_import_module = """ | ||
| import time | ||
|
|
||
| time.sleep(%(delay)s) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'll be more radical and say I don't think we need a test at all, if we can't make it simpler.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| module = PyImport_GetModule(module_name); | ||
| module = PyImport_Import(module_name); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
git master:
$ ./python -m timeit -s "import pickle, logging; d = pickle.dumps((logging.Logger, logging.Handler, logging.Filter, logging.Formatter))" "pickle.loads(d)"
100000 loops, best of 5: 2.55 usec per loop
This PR:
$ ./python -m timeit -s "import pickle, logging; d = pickle.dumps((logging.Logger, logging.Handler, logging.Filter, logging.Formatter))" "pickle.loads(d)"
50000 loops, best of 5: 4.84 usec per loop
So this is a sizable performance drop, but on a micro-benchmark. I don't think this will be significant in real-world cases. Usually, pickle performance becomes important when large pieces of data are serialized, and by construction you don't get many STACK_GLOBAL opcodes in it.
cc'ing @pierreglaser @ogrisel out of curiosity.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I haven't tried adapting this to actually use pickle, but I can consistently get a failure due to partially initialized modules on the first try with this configuration. It does use |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This other case of
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I have made the requested changes; please review again. Happy to backport to 3.6 and 3.7 also. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks for making the requested changes! @pablogsal: please review the changes made to this pull request. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Please make that a separate issue on bugs.python.org. We'll have to discuss its performance implications separately. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| t1.join() | ||
| t2.join() | ||
|
|
||
| self.assertEqual(len(results), 2) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Perhaps also assert something about the results?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| self.check_unpickling_error(self.truncated_errors, p) | ||
|
|
||
| @reap_threads | ||
| def test_unpickle_module_race(self): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is a neat test :-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@pablogsal Any thoughts on the new tests for this one? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@pablogsal, I think your comments have been addressed. Can you confirm? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This addresses all my concerns! I like |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…ving from sys.modules (pythonGH-9047) Fix C implementation of pickle.loads to use importlib's locking mechanisms, and thereby avoid using partially-loaded modules. (cherry picked from commit 4371c0a) Co-authored-by: tjb900 <[email protected]>
|
GH-11921 is a backport of this pull request to the 3.7 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…ving from sys.modules (GH-9047) (GH-11921) Fix C implementation of pickle.loads to use importlib's locking mechanisms, and thereby avoid using partially-loaded modules. (cherry picked from commit 4371c0a) Co-authored-by: tjb900 <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.