honeybadger-io / honeybadger-python

Send Python and Django errors to Honeybadger.
https://www.honeybadger.io/
MIT License
15 stars 26 forks source link

Local thread loses context in Flask app #20

Closed LanceOlsen closed 6 years ago

LanceOlsen commented 7 years ago

I am running a Flask app, and using notify in an errorhandler decorator:

@app.errorhandler(Exception)
def handle_error(e):
    honeybadger.notify(e, context={})
    # other error related functionality

In the __init__.py file for this app I have:

from honeybadger import honeybadger
honeybadger.configure(api_key="blablah")

I'm getting the following error:

Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/root/miniconda3/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/root/miniconda3/lib/python3.6/site-packages/flask/app.py", line 1518, in handle_user_exception
    return handler(e)
  File "/root/my_app/api.py", line 116, in handle_error
    honeybadger.notify(e, context={})
  File "/root/miniconda3/lib/python3.6/site-packages/honeybadger/core.py", line 43, in notify
    merged_context = self.thread_local.context
AttributeError: '_thread._local' object has no attribute 'context'
joshuap commented 7 years ago

Thanks for the report @LanceOlsen. We'll work on getting this fixed.

demsullivan commented 7 years ago

Hey @LanceOlsen, I'm looking into this. I'm not able to reproduce it on my end, so I'd like to get some more information from you to see if we can track this down. Can you please provide the answers to the following:

Thanks!

joshuap commented 7 years ago

Closing this out due to lack of response. Feel free to reopen if you're still having this issue!

ifoukarakis commented 6 years ago

I also encountered the same problem. The problem is that if you try to call honeybadger.notify from a different thread than the one the global honeybadger object was created, the thread local variables disappear. Here's a script to reproduce it:

import threading
from honeybadger import honeybadger

def notifier():
    try:
        raise ValueError('Failure')
    except ValueError as e:
        honeybadger.notify(e)

honeybadger.configure(api_key='test')

notify_thread = threading.Thread(target=notifier)
notify_thread.start()
demsullivan commented 6 years ago

@ifoukarakis thanks for the sample! I'll look into getting this fixed up as soon as I can.