orf / django-debug-toolbar-template-timings

A django-debug-toolbar panel that displays template rendering times for your Django application
https://pypi.python.org/pypi/django-debug-toolbar-template-timings
MIT License
297 stars 28 forks source link

Avoid KeyErrors in timing results #11

Closed originell closed 11 years ago

originell commented 11 years ago

For some weird reason I get KeyErrors in this part…

I'm not exactly sure why they happen, because as far as I understand it they should not.
Sadly I'm under a time constraint right now, so I had to "workaround" this, rather than truly fix it. Below is the workaround, which uses the wonderful setdefault

orf commented 11 years ago

Weird! Thanks for this

originell commented 11 years ago

Totally.. but as I said I consider this more of a workaround than a true fix :)

trwalzer commented 11 years ago

I am still getting KeyErrors even on the newest version.

They are infrequent, but they always happen on the first block of the template. The issue is on line 77 of TemplateTimings.py, key='blocks'

if name_self not in results.timings[key] and should_add(name_self):
originell commented 11 years ago

awww man…

does that happen for you on every page or just a specific one?

orf commented 11 years ago

@trwalzer could you please post the template that is causing you issues? I will add some failsafes to it and make it print out some useful info instead of KeyErroring

trwalzer commented 11 years ago

It happens inconsistently, but I've seen it on multiple pages. When it does happen it ALWAYS happens on the first block of the template. I've seen it on both the admin site and the regular site. They use different base templates, so its not just one template either. Here is the top of the template, it fails on the {% block doctype %} tag.
{% load i18n satchmo_category satchmo_google satchmo_util satchmo_currency satchmo_discounts app_plugins normalize_decimal %} {% load url from future %} {% block doctype %} <!DOCTYPE html> {% endblock %}

I will keep an eye on it and when it happens again I'll grab more info from the error page.

originell commented 11 years ago

for me it always happened on the admin page (django 1.5)… this might sound crazy, but could that be a thread-related issue?

orf commented 11 years ago

It might be, what version of Python are you guys using? The lookup is on a defaultdict() which in theory should never KeyError because any missing keys are automatically inserted and the default value returned.

Perhaps the defaultdict is turning into a normal dictionary somehow? :/

Anyhoo I've added a hack that might fix it (update is on PyPi), let me know how it goes.

originell commented 11 years ago

I'm on Python 2.7.2, what about you?

trwalzer commented 11 years ago

2.7.3 over here. I'll pull the latest and keep an eye on things.

originell commented 11 years ago

defaultdict is in python's collections since 2.5.. something stinks here

orf commented 11 years ago

Well I have seen this issue before, but not since the original pull request. I will revert it on my machine and do some digging tomorrow

trwalzer commented 11 years ago

I think the issue is related to running New Relic and Template Timings at the same time. I realized that when I was running both, the KeyError issue would sometimes be raised by New Relic, other times it would be raised by Template Timings. So I'm not sure who is to blame, but it seems that they don't play well together.

originell commented 11 years ago

I'm not using New Relic, so that's at least not related to my error back then.

orf commented 11 years ago

Does this still happen, even with the latest version? If this occurs again could I please have the key it is trying to find and the value of results.timings, otherwise I haven't got a chance of fixing this.