Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'ThreadedDict' object has no attribute 'app_stack' #268

Open
singpenguin opened this issue Nov 27, 2013 · 20 comments
Open
Assignees
Labels

Comments

@singpenguin
Copy link

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 237, in process
return p(lambda: process(processors))
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 591, in processor
h()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 98, in _unload
web.ctx.app_stack = web.ctx.app_stack[:-1]
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 237, in process
return p(lambda: process(processors))
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 566, in processor
return handler()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 237, in
return p(lambda: process(processors))
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 246, in process
raise self.internalerror()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 473, in internalerror
parent = self.get_parent_app()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 458, in get_parent_app
if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 508, in handle_one_response
self.run_application()
File "/usr/local/lib/python2.7/dist-packages/gevent-1.0-py2.7-linux-x86_64.egg/gevent/pywsgi.py", line 494, in run_application
self.result = self.application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 279, in wsgi
result = self.handle_with_processors()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 249, in handle_with_processors
return process(self.processors)
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 246, in process
raise self.internalerror()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 473, in internalerror
parent = self.get_parent_app()
File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 458, in get_parent_app
if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'
{'CONTENT_LENGTH': '0',
'CONTENT_TYPE': 'application/x-www-form-urlencoded',
'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_CACHE_CONTROL': 'no-cache',
'HTTP_CONNECTION': 'close',
'HTTP_COOKIE': '',
'HTTP_HOST': 'test.com',
'HTTP_USER_AGENT': 'Internet',
'HTTP_X_FORWARDED_FOR': '...',
'HTTP_X_REAL_IP': '...',
'PATH_INFO': '/test',
'QUERY_STRING': '',
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '58279',
'REQUEST_METHOD': 'POST',
'SCRIPT_NAME': '',
'SERVER_NAME': 'localhost',
'SERVER_PORT': '8058',
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
'wsgi.errors': <open file '', mode 'w' at 0x7ff36ea49270>,
'wsgi.input': <gevent.pywsgi.Input object at 0x2b83250>,
'wsgi.multiprocess': False,
'wsgi.multithread': False,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)} failed with AttributeError

web server is gevent(1.0), The "/test" interface has been reported that the call was wrong

@IllegalWalker
Copy link

I had the same problem
I found the reason for my problem is that : I created multiple webpy application instance for each worker server process, when I change the code to create one application for all server worker process, this bug gone

@Ravil-Safin
Copy link

I have the same issue with web.py 0.37 version. I've got my code from an example:

import web
urls = ('/hello', 'hello')
app = web.application(urls, globals())

class hello:
    def GET(self):
        web.header('Content-Type', 'text/plain')
        return "Hello world!"

response = app.request("/hello")
print(response.data)

And getting error:

C:\Python27\python.exe D:/tmp/test_web/server.py
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\web\application.py", line 239, in process
    return self.handle()
  File "C:\Python27\lib\site-packages\web\application.py", line 229, in handle
    fn, args = self._match(self.mapping, web.ctx.path)
AttributeError: 'ThreadedDict' object has no attribute 'path'

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\web\application.py", line 237, in process
    return p(lambda: process(processors))
  File "C:\Python27\lib\site-packages\web\application.py", line 566, in processor
    return handler()
  File "C:\Python27\lib\site-packages\web\application.py", line 237, in <lambda>
    return p(lambda: process(processors))
  File "C:\Python27\lib\site-packages\web\application.py", line 246, in process
    raise self.internalerror()
  File "C:\Python27\lib\site-packages\web\application.py", line 473, in internalerror
    parent = self.get_parent_app()
  File "C:\Python27\lib\site-packages\web\application.py", line 458, in get_parent_app
    if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\web\application.py", line 237, in process
    return p(lambda: process(processors))
  File "C:\Python27\lib\site-packages\web\application.py", line 566, in processor
    return handler()
  File "C:\Python27\lib\site-packages\web\application.py", line 237, in <lambda>
    return p(lambda: process(processors))
  File "C:\Python27\lib\site-packages\web\application.py", line 246, in process
    raise self.internalerror()
  File "C:\Python27\lib\site-packages\web\application.py", line 473, in internalerror
    parent = self.get_parent_app()
  File "C:\Python27\lib\site-packages\web\application.py", line 458, in get_parent_app
    if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\web\application.py", line 237, in process
    return p(lambda: process(processors))
  File "C:\Python27\lib\site-packages\web\application.py", line 585, in processor
    h()
  File "C:\Python27\lib\site-packages\web\application.py", line 98, in _unload
    web.ctx.app_stack = web.ctx.app_stack[:-1]
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\web\application.py", line 237, in process
    return p(lambda: process(processors))
  File "C:\Python27\lib\site-packages\web\application.py", line 566, in processor
    return handler()
  File "C:\Python27\lib\site-packages\web\application.py", line 237, in <lambda>
    return p(lambda: process(processors))
  File "C:\Python27\lib\site-packages\web\application.py", line 246, in process
    raise self.internalerror()
  File "C:\Python27\lib\site-packages\web\application.py", line 473, in internalerror
    parent = self.get_parent_app()
  File "C:\Python27\lib\site-packages\web\application.py", line 458, in get_parent_app
    if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'

Traceback (most recent call last):
  File "D:/tmp/test_web/server.py", line 11, in <module>
    response = app.request("/hello")
  File "C:\Python27\lib\site-packages\web\application.py", line 221, in request
    response.data = "".join(self.wsgifunc()(env, start_response))
  File "C:\Python27\lib\site-packages\web\application.py", line 279, in wsgi
    result = self.handle_with_processors()
  File "C:\Python27\lib\site-packages\web\application.py", line 249, in handle_with_processors
    return process(self.processors)
  File "C:\Python27\lib\site-packages\web\application.py", line 246, in process
    raise self.internalerror()
  File "C:\Python27\lib\site-packages\web\application.py", line 473, in internalerror
Hello world!
    parent = self.get_parent_app()
  File "C:\Python27\lib\site-packages\web\application.py", line 458, in get_parent_app
    if self in web.ctx.app_stack:
AttributeError: 'ThreadedDict' object has no attribute 'app_stack'

Process finished with exit code 1

@stanislavkozlovski
Copy link

Anybody found a solution?

@singpenguin
Copy link
Author

This question is hard. I also met when 2013 year.
My project was tight, so I replaced web.py with tornado.
But i still like web.py very much.

@dinodouglasr
Copy link

dinodouglasr commented Nov 11, 2016

Use requests library instead app.request.

import requests
req = requests.get("http´://127.0.0.1:8058/test")
req.text

@hatcherfang
Copy link

@dinodouglasr we can't limit the client request method.
Is there anyone found the solution?

@hatcherfang
Copy link

hatcherfang commented Jan 4, 2018

@Ravil-Safin Did you find the solution finally?

@hatcherfang
Copy link

I found a solution (https://www.itcao.com/post_1182.html) mark it

@magicknight
Copy link

I encountered this problem. anyone has any suggestions?

@magicknight
Copy link

magicknight commented Jun 7, 2018

well, seems to be a gevent bug.

put these line before everything

from gevent import monkey
monkey.patch_all()

works for me

@iredmail
Copy link
Contributor

@cclauss could you help check this issue? I can reproduce it with py2 and py3 with the sample code given by @Ravil-Safin #268 (comment)

@cclauss
Copy link
Contributor

cclauss commented Sep 14, 2019

Can you please create a PR that contains the failing test so that we all have a common playground for trying out solutions?

@iredmail
Copy link
Contributor

@cclauss Use this code snippet:

import web
urls = ('/hello', 'hello')
app = web.application(urls, globals())

class hello:
    def GET(self):
        web.header('Content-Type', 'text/plain')
        return "Hello world!"

response = app.request("/hello")
print(response.data)

@cclauss
Copy link
Contributor

cclauss commented Sep 14, 2019

We should try out this style of solution...
aaronsw@348f1f4#diff-52ed61391b0139abeacbed53a6bd0eb0R50
Looks for 'app_stack' and if it is not found then do nothing.

@iredmail
Copy link
Contributor

  • Did you reply to wrong issue or it's relevant?
  • The solution in the link is much clearer than existing implementation. I agree to go with this change.

@cclauss
Copy link
Contributor

cclauss commented Sep 14, 2019

Replying to the correct issue with a solution discussed in another issue.

@iredmail
Copy link
Contributor

Could you help create a PR for this change?

@iredmail
Copy link
Contributor

@cclauss if you're working on this issue, don't forget to check (and maybe fix) #246 also, it's relevant.

@iredmail iredmail added this to the 0.40 + Python-3 milestone Sep 16, 2019
@iredmail iredmail added the Bug label Sep 16, 2019
@iredmail iredmail removed this from the 0.40 + Python-3 milestone Sep 16, 2019
@AndreLouisCaron
Copy link

@iredmail In that example you posted above, isn't the issue created because the sample code uses import-time side-effect to invoke the app (which triggers a nested request)?

If you change the code to this (to avoid web.py importing 'index' from globals()), it works:

import web

class hello:
    def GET(self):
        web.header('Content-Type', 'text/plain')
        return "Hello world!"

urls = ('/hello', hello)
app = web.application(urls, {})

response = app.request("/hello")

@AndreLouisCaron
Copy link

AndreLouisCaron commented Jul 3, 2020

I'm confident this issue arises in presence of concurrency. The web.application._cleanup() function calls web.ThreadedDict.clear_all(), which clears web.ctx in all threads. This is called at the end of .wsgifunc(). If any 2 requests execute concurrently, the first to finish will clear web.ctx for the other. (EDIT: nevermind, this is not related.)

There is a related issue with web.ctx, such as incorrect handling of nested context. If you have multiple "micro services" running in the same process (e.g. for simplicity of development), you will also run into the AttributeError: 'ThreadedDict' object has no attribute 'app_stack' error.

# -*- coding: utf-8 -*-


from __future__ import print_function

import web


class inner:
    def GET(self):
        web.header("Content-Type", "text/plain")
        return "This is the `inner` app!"


inner_app = web.application(("/", inner), {})


class outer:
    def GET(self):
        web.header("Content-Type", "text/plain")
        inner_app.request("/")
        return "This is the `outer` app!"


outer_app = web.application(("/", outer), {})


print("RESPONSE:", outer_app.request("/").data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants