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

Request.finish called on a request after its connection was lost #11

Merged
merged 11 commits into from
Sep 7, 2013

Conversation

dreid
Copy link
Contributor

@dreid dreid commented Mar 12, 2013

Hi,

Thanks for Klein, it's really nice! I'm having an issue with requests failing because connections are closed before the response is finished. That leads to the following exception:

exceptions.RuntimeError: Request.finish called on a request after its
connection was lost; use Request.notifyFinish to keep track of this.

Normally I would handle that exception by doing

request.notifyFinish().addErrback(lambda f: d.cancel())

But this doesn't seem to work with Klein, since Klein adds both a callback and an errback that try to write useful information to the request. Of course I could just ignore it, but the error pollutes my logs and steal my attention from real errors. Have you encountered this problem, and if so, how do you handle it?

…request.finish.

This is caused by either the client terminating the connection or a bug in the server.

The default behavior is to allow request.write to be called when a handler completes
successfully, and let twisted.web raise it's RuntimeError.

However, the processingFailed code bath has been changed to guard against
error handling being called when a request has already completed.

This also allows this pattern:

@route("/")
def handler(request):
    d = someOperation()
    request.notifyFinish().addCallback(lambda _: d.cancel())

    return d

Which will effectively cancel someOperation when the client the disconnects,
while the handler is waiting for the result of someOperation.
Conflicts:
	klein/test_resource.py
…deferred returned to klein by the handler if the connection is lost.
Conflicts:
	klein/test_resource.py
request_finished[0] = True

request.notifyFinish().addBoth(_finish)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too bad Twisted's Request doesn't have a finished attribute.

@iffy
Copy link
Contributor

iffy commented May 9, 2013

The tests are good, and the code does what the tests expect. I've made a few inline comments. I think this is a good change.

dreid added a commit that referenced this pull request Sep 7, 2013
Request.finish called on a request after its connection was lost
@dreid dreid merged commit c3627a2 into master Sep 7, 2013
@dreid dreid deleted the finished-requests branch September 7, 2013 02:42
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 this pull request may close these issues.

None yet

2 participants