youknowone / itunes-iap

Apple iTunes In-app purchase verification tool
http://itunes-iap.readthedocs.io
Other
136 stars 50 forks source link

IndexError: pop index out of range #22

Closed hanleyhansen closed 7 years ago

hanleyhansen commented 8 years ago

I'm getting the following error when trying to validate in sandbox mode:

Internal Server Error: /api/validate/
Traceback (most recent call last):
  File "/Users/hanleyhansen/.virtualenvs/mkl/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/hanleyhansen/.virtualenvs/mkl/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/hanleyhansen/.virtualenvs/mkl/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/hanleyhansen/Google Drive/Work/Projects/git/mkl-web/mkl/views/api_views.py", line 99, in validate
    response = itunesiap.verify(receipt_data, IAP_SHARED_SECRET)
  File "/Users/hanleyhansen/.virtualenvs/mkl/lib/python2.7/site-packages/itunesiap/environment.py", line 39, in __exit__
    self._stack.pop(self._ctx_id)
IndexError: pop index out of range

Any ideas?

Here is my code:

    try:
        if IAP_SANDBOX:
            with itunesiap.env.sandbox:
                response = itunesiap.verify(receipt_data, IAP_SHARED_SECRET)
        else:
            with itunesiap.env.default:
                response = itunesiap.verify(receipt_data, IAP_SHARED_SECRET)
    except itunesiap.exc.InvalidReceipt as e:
        return HttpResponseForbidden({'message': 'not valid', 'error': e}, content_type='application/json')
youknowone commented 8 years ago

This is not expected behavior.

it seems it is being popped in weird order. in environment code, will you insert logging code in enter and exit to catch what's happened?

beschulz commented 8 years ago

I'm seeing this as well. In my scenario the validation is done inside a multithreaded wsgi server. So this library does not appear to be thread safe.

To be honest, I don't really get why one want to use a global stack. I'd prefer to pass the environment as a variable to verify() or have verify() be a member variable of the environment.

despite that, it's a really useful library – thanks.

youknowone commented 8 years ago

for future fix: https://github.com/beschulz/itunes-iap/commit/2bdff274b70d261790d26dbec02ee69983f79b37

jox commented 7 years ago

When is the future? :) I'm getting these errors as well, running itunes-iap 2.3.0. Thanks for the otherwise very useful library.

youknowone commented 7 years ago

Hello jox, beschulz's fix is great but I need to test it is breaking backward compatibility or not. (Or developing a new backward-compatibility mode)

Yes - it is mostly because I wasn't taking care of this project for a while. But you reminded it. thanks. Let's see I will take a chance to look at this.

jox commented 7 years ago

@youknowone thanks for responding!

I patched my installation and it is not backwards compatible, that I can say. But it fixes the problem.

It would be great if you could take a look.

youknowone commented 7 years ago

I found mmeisinger made a patch of it already. You can check the new api at the README.

itunesiap.verify(receipt, env=itunesiap.env.review)
jox commented 7 years ago

@youknowone Thank you!