braintree / braintree_python

Braintree Python library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
242 stars 115 forks source link

strptime not thread safe #55

Closed andymckay closed 9 years ago

andymckay commented 9 years ago

I've been getting intermittent errors in parsing webhooks from braintree:

    return self.__node_content(root, child.nodeValue)
  File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/braintree/util/parser.py", line 87, in __node_content
    return self.__convert_to_datetime(content)
  File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/braintree/util/parser.py", line 42, in __convert_to_datetime
    return datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
AttributeError: _strptime

I think the cause is this:

https://bugs.python.org/issue7980

In my case I'm using braintree_python in Django and runserver. It sounds like this would occur in our prod environment using uwsgi too. Since the webhooks can be retried, this isn't a terrible problem, but it could be an issue in areas other than webhook.

Since the dates are consistent and well formatted from Braintree, perhaps just working around strptime will be easier.

agfor commented 9 years ago

@andymckay Thanks for the report.

It seems unlikely this would crop up in production -- you'd have to be formatting dates for the first time at exactly the same time in two different threads. There are two workarounds mentioned in http://stackoverflow.com/questions/2427240/thread-safe-equivalent-to-pythons-time-strptime both of which I tested successfully. You can either use strptime before creating your threads, or you can import _strptime either inside or outside your threads before you use strptime.

We'll consider making changes to the library to stop this from being a problem. Since there are simple workarounds, and we've never had a report of this in production, I'm going to close this issue.