Repository navigation
Closed
Labels
release-blockerstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Description
| BPO | 24667 |
|---|---|
| Nosy | @rhettinger, @larryhastings, @markshannon, @ericsnowcurrently, @1st1, @sigmavirus24, @Lukasa, @xZise, @jayvdb |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/ericsnowcurrently'
closed_at = <Date 2015-08-07.23:51:02.521>
created_at = <Date 2015-07-19.10:40:38.245>
labels = ['type-bug', 'library', 'release-blocker']
title = 'OrderedDict.popitem()/__str__() raises KeyError'
updated_at = <Date 2015-08-09.19:58:37.904>
user = 'https://github.com/xZise'bugs.python.org fields:
activity = <Date 2015-08-09.19:58:37.904>
actor = 'eric.snow'
assignee = 'eric.snow'
closed = True
closed_date = <Date 2015-08-07.23:51:02.521>
closer = 'eric.snow'
components = ['Library (Lib)']
creation = <Date 2015-07-19.10:40:38.245>
creator = 'xZise'
dependencies = []
files = ['40019', '40068', '40069', '40072', '40078', '40127']
hgrepos = []
issue_num = 24667
keywords = ['patch']
message_count = 41.0
messages = ['246937', '246944', '247011', '247012', '247017', '247029', '247039', '247055', '247057', '247074', '247075', '247104', '247112', '247151', '247351', '247355', '247357', '247390', '247391', '247396', '247417', '247542', '247647', '247674', '247682', '247716', '247718', '247732', '247780', '247960', '248006', '248063', '248094', '248095', '248140', '248147', '248236', '248237', '248288', '248316', '248334']
nosy_count = 10.0
nosy_names = ['rhettinger', 'larry', 'Mark.Shannon', 'python-dev', 'eric.snow', 'yselivanov', 'icordasc', 'Lukasa', 'xZise', 'jayvdb']
pr_nums = []
priority = 'release blocker'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue24667'
versions = ['Python 3.5', 'Python 3.6']Metadata
Metadata
Assignees
Labels
release-blockerstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Activity
xZise commentedon Jul 19, 2015
While testing pywikibot using requests and urllib3 on Python 3.6 we got an interesting error:
======================================================================
ERROR: testQueryApiGetter (tests.wikidataquery_tests.TestApiSlowFunctions)
Test that we can actually retreive data and that caching works.
----------------------------------------------------------------------
Now that doesn't make much sense, as OrderedDict.popitem() only returns a KeyError if it's empty but then not with that error message. Unfortunately I can't reproduce it without doing the complete pywikibot test suite. When I only execute the tests where it occurred I don't get any failures. So I modified the output in urllib3 and returned the key as well as the content before popitem(last=False) is called (I shortened the values):
Key: ('https', 'bn.wikipedia.org', 443)
Content: OrderedDict([(('https', 'bn.wikipedia.org', 443), <requests.packages.urllib3.connectionpool.HTTPSConnectionPool object at 0x7fe483d991d0>), (('https', 'bs.wikipedia.org', 443), <…0x7fe483d99470>), (('https', 'ca.wikipedia.org', 443), <…0x7fe483da0e10>), (('https', 'cs.wikipedia.org', 443), <…0x7fe483dfc908>), (('https', 'da.wikipedia.org', 443), <…0x7fe483d98c88>), (('https', 'de.wikipedia.org', 443), <…0x7fe4911e06a0>), (('https', 'diq.wikipedia.org', 443), <…0x7fe483d6b400>), (('https', 'dsb.wikipedia.org', 443), <…0x7fe483d6bcf8>), (('https', 'en.wikipedia.org', 443), <…0x7fe483dc1da0>), (('https', 'eo.wikipedia.org', 443), <…0x7fe483d6cf28>), (('https', 'es.wikipedia.org', 443), <…0x7fe483d6b4a8>), (('https', 'fa.wikipedia.org', 443), <…0x7fe483d27278>), (('https', 'fi.wikipedia.org', 443), <…0x7fe483d6ce10>), (('https', 'fr.wikipedia.org', 443), <…0x7fe483e24cf8>), (('https', 'frr.wikipedia.org', 443), <…0x7fe483d39390>), (('https', 'ga.wikipedia.org', 443), <…0x7fe483c99668>), (('https', 'gl.wikipedia.org', 443), <…0x7fe483ca3f98>), (('https', 'als.wikipedia.org', 443), <…0x7fe483c4fcf8>), (('https', 'hu.wikipedia.org', 443), <…0x7fe483cd09e8>)])
As you can see it is not empty and the key in the KeyError is the first key in the OrderedDict. Also the key there is different from the key in the original exception I noticed so it's not a specific key that failed.
I don't think versions before Python 3.6 are affected as we had tests running on Python 3.5 (before Travis switched to 3.6 recently) and these all worked. Also not all popitem() calls in that line fail. I'm using Python 3.6.0a0 (default:d6c91b8242d2, Jul 18 2015, 16:36:01).
See also: urllib3/urllib3#680 and https://phabricator.wikimedia.org/T106212
xZise commentedon Jul 19, 2015
Looking further into this issue, OrderedDict.pop() using the key returned from the KeyError (using eval(str(error))) also yields a KeyError. And OrderedDict.popitem() does not change the dictionary (so it's not like the KeyError is raised even though it worked).
Also it appears to be empty actually. list(OrderedDict) returns an empty list even though str(OrderedDict) does not. So maybe some operations do only remove entries from one part of the data so that popitem() still thinks it's in the cache. Now I'm not familiar with urllib3 so I'm not sure how that internal OrderedDict is used to narrow down what might cause that issue.
And additionally I tried to output keys(), items() and values() separately and suddenly I get a KeyError even when I just do str():
[-]OrderedDict.popitem() raises KeyError[/-][+]OrderedDict.popitem()/__str__() raises KeyError[/+]ericsnowcurrently commentedon Jul 21, 2015
Just to get this out of the way, are you running your tests against the latest beta (3)? There were some known bugs in earlier betas that have since been fixed.
ericsnowcurrently commentedon Jul 21, 2015
Correct me if I'm wrong but the travis-ci logs [1] seem to indicate it's using Python 3.6.0a0.
[1] https://travis-ci.org/xZise/pywikibot-core/builds/71550286#L144
xZise commentedon Jul 21, 2015
Well as I described in the opening post:
"I don't think versions before Python 3.6 are affected as we had tests running on Python 3.5 (before Travis switched to 3.6 recently) and these all worked."
Now tbh I don't know if a version of 3.5 is affected but was never tested by Travis. And if then the 3.6 branch was branched of that version. If that how it works. As I'm using pyenv I could easily test this on Python 3.5b3. By the way the last official test run on Pyhton 3.5 using Travis was using 3.5a4+: https://travis-ci.org/wikimedia/pywikibot-core/jobs/71443578#L144
ericsnowcurrently commentedon Jul 21, 2015
Ah, sorry. I wasn't thinking past Python 3.5 (which is about to go to beta 4). While 3.6.0a0, doesn't tell us much, d6c91b8242d2 (r96935) does. That revision has all the necessary fixes to OrderedDict. I'll look into this some more. Do you get the failure consistently when running the test suite?
Also, in case you weren't aware: Python 3.5 (hence 3.6) introduces a C implementation of OrderedDict.
xZise commentedon Jul 21, 2015
It is consistent as in it happens on every run of the test suite. But unfortunately I haven't checked if it's always happening at the same place. Luckily we have 4 builds on Travis with 3.6 and in all it happened from the beginning and got > 100 matches for “KeyError:”:
Maybe I can do additional analysis but I'm pretty sure for me locally I didn't get failures so soon.
And no I wasn't aware about OrderedDict implemented in C. Now I haven't done tests in 3.5.0b3 (which seems to be the newest version of 3.5 available via pyenv at the moment) as it's relatively cumbersome and prevents me from doing any development on pywikibot at the same time. Anyway I still might do it and report whether I get the error too (unless OrderedDict is still implemented in Python in that version).
ericsnowcurrently commentedon Jul 21, 2015
Thanks for the extra info. I'm going to see if I can reproduce the issue by running the pywikibot test suite locally. What's the best way to set that up? Are there instructions somewhere?
As to the C implementation, it was first released (as a special exception) in 3.5b2.
xZise commentedon Jul 21, 2015
Yes see the tests/README.rst. And afaik do you only need to have requests and six installed.
xZise commentedon Jul 21, 2015
Okay I did a test run on all three 3.5 betas available to me through pyenv. The beta 3 failed as Python 3.6 does that popitem() raises a KeyError. The beta 2 had the bug that popitem() does not support keyword arguments so I wasn't able to test it there. And the beta 1 works fine so it looks like the C implementation is the cause of this.
ericsnowcurrently commentedon Jul 21, 2015
I've thus far been unsuccessful in running the pywikibot test suite. I'm guessing there are some prerequisites (e.g. an account on some wiki site). Is there a way to run the tests without network access? Also, I ran into some trouble with i18n.__file__, but hacked my way around it.
xZise commentedon Jul 22, 2015
Oh sorry, I basically never need to install pywikibot anew so it's easy to forget but there is a submodule in scripts/i18n which needs to be cloned as well. With the following commands I could reproduce the error (and you don't even need to install requests and six):
With that you only need a network connection (as this error is in urllib3 I doubt it works without network connection) but you don't need any wiki account. If you want to test it with an account, execute “generate_user_files.py” before. Just as a note I've activated write and write failure tests but without an account these tests should not be run anyway.
18 remaining items
ericsnowcurrently commentedon Aug 4, 2015
It turns out the problem was that the odict resize mechanism was not getting triggered in all the cases that it should have been. dict resizes after a certain number of insertions, whether or not previous deletions have cleared out space. odict only resizes its fast lookup table when it needs to do a fast node lookup and only when the current dict keys size does not match the current size of the odict fast lookup table.
This bug is the consequence of a corner case which odict did not handle correctly. When you delete an item and then insert another, the resulting size of the dict keys is the same as the it was before the deletion. However, the insertion may have triggered a resize. This matters because when a resize happens the keys are re-inserted into the hash table. If it so happens that they key you just removed was in a slot that would have otherwise been occupied by another key (i.e. there was an earlier collision) then upon resizing that other key will be moved to its preferred slot.
Here's a patch that changes odict to rely on the pointer value of dict's ma_keys (rather than on ma_keys.dk_size) to indicate the need for a resize. This works because dict creates a new keys struct every time it resizes.
I'll point out that one alternative would be to track a "state" counter on dict that increments each time there's a resize. Then odict could check that rather than the ma_keys pointer value.
xZise commentedon Aug 5, 2015
Thank you for figuring it out and providing a patch after I couldn't really give good information. I haven't been able to test it for myself but your explanation makes sense :)
ericsnowcurrently commentedon Aug 6, 2015
@fabian, hey, thanks for bringing it to our attention!
ericsnowcurrently commentedon Aug 6, 2015
If I don't get any feedback before then, I'll commit the patch on Friday.
xZise commentedon Aug 6, 2015
Okay I think I've run the test suite on 3.6 although it only works with https://gerrit.wikimedia.org/r/#/c/229683/ now as inspect.getargspec has been removed (when I initially posted this, it was just deprecated).
Well writing this I just thought I could've tested that on 3.5… well anyway I don't get any KeyErrors.
ericsnowcurrently commentedon Aug 6, 2015
That's good to know. Thanks.
python-dev commentedon Aug 7, 2015
New changeset adb510322c8f by Eric Snow in branch '3.5':
Issue bpo-24667: Resize odict in all cases that the underlying dict resizes.
https://hg.python.org/cpython/rev/adb510322c8f
New changeset 47287c998bb0 by Eric Snow in branch 'default':
Merge from 3.5 (issue bpo-24667).
https://hg.python.org/cpython/rev/47287c998bb0
ericsnowcurrently commentedon Aug 7, 2015
I've pushed the fix for RC1. Thanks again Fabian for bringing it to our attention.
larryhastings commentedon Aug 8, 2015
FWIW I set up a test harness that runs test_collections and odict_reproduce with monotonically increasing PYTHONHASHSEED values. I let it run overnight; it's now past iteration 158600. Apart from some weirdness in the low 70ks that I can't reproduce it's been rock solid.
So, looks good!
larryhastings commentedon Aug 9, 2015
Yeah, I stopped it at 300k iterations. No problems. Glad this fix got in for rc1.
ericsnowcurrently commentedon Aug 9, 2015
Thanks for the double-check, Larry. I'd done similar testing, though not
as exhaustively. :) Getting a fix in for RC1 was pretty important to me.
It stretched me a bit, but I got it done.