You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ob here is (can be?) an application object that was initialized with the request object
So request is referencing an application object which is referencing the request, which creates a reference cycle.
Those are not inherently bad but this makes it so the request will not be cleaned up at the end of its life cycle, and some related resources will also not be freed up until then
A practical example is a large JSON body sent to the webserver: webob will create a temporary file, that will only be deleted on the next garbage collection (that will raise a ResourceWarning, but only during tests)
Here is a minimal example exposing the problem, disabling the gc will lead to a ever-increasing number of requests
importgcfromwsgiref.simple_serverimportmake_serverfromcornice.resourceimportresource, viewfrompyramid.configimportConfiguratorfrompyramid.requestimportRequest@resource(collection_path="/submit", path="/submit/{id}")classHelloWorldResource:
def__init__(self, request, context=None):
self.request=requestself.context=context@view()defcollection_post(self):
# find number of in-memory pyramid requestscount=sum(1forxingc.get_objects() ifisinstance(x, Request))
return {"message": f"I have {count} request(s) in memory."}
defmain():
withConfigurator() asconfig:
config.include("cornice")
config.scan()
app=config.make_wsgi_app()
returnappif__name__=="__main__":
app=main()
server=make_server("0.0.0.0", 6543, app)
print("Server running on http://localhost:6543")
gc.collect()
# disable gc for demonstrationgc.disable()
server.serve_forever()
The issue could be fixed by using params=dict(request=weakref.proxy(request)) in the original source, but it may have side effect I'm not foreseeing (this does not break the tests however)
The text was updated successfully, but these errors were encountered:
When debugging an issue with our application, we found out that cornice creates a reference cycle with the
request
objectIn
cornice/service.py
:ob
here is (can be?) an application object that was initialized with therequest
objectSo request is referencing an application object which is referencing the request, which creates a reference cycle.
Those are not inherently bad but this makes it so the request will not be cleaned up at the end of its life cycle, and some related resources will also not be freed up until then
A practical example is a large JSON body sent to the webserver: webob will create a temporary file, that will only be deleted on the next garbage collection (that will raise a
ResourceWarning
, but only during tests)Here is a minimal example exposing the problem, disabling the gc will lead to a ever-increasing number of requests
In our case we'll be fixing the issue by using
The issue could be fixed by using
params=dict(request=weakref.proxy(request))
in the original source, but it may have side effect I'm not foreseeing (this does not break the tests however)The text was updated successfully, but these errors were encountered: