Cannot Properly Catch DeadlineExceededError

73 views
Skip to first unread message

lenza

unread,
Feb 23, 2009, 12:55:03 AM2/23/09
to Google App Engine
I am attempting to customize my application response to a
DeadlineExceededError. I am able to catch the exception, but my
custom response is not getting through. Instead I get the default
"502 Server Server Error" page even after clearing the response and
writing a custom one. It seems as if the GAE is allowing time for
cleanup, but ignoring anything written to the response object. Is
anyone doing this successfully?

More details...

The GAE documentation states that you can customize your applications
response to a DeadlineExceededError with the following example code
(see http://code.google.com/appengine/docs/python/runtime.html):

class MainPage(webapp.RequestHandler):
def get(self):
try:
# Do stuff...

except DeadlineExceededError:
self.response.clear()
self.response.set_status(500)
self.response.out.write("This operation could not be completed
in time...")

I have the following handler for "/test" on my App:

class TestPage(webapp.RequestHandle):
def get(self):
try:
sleeptime = int(self.request.get('sleep'))
if(sleeptime == 0):
logging.info("Programatically raising
DeadlineExceededError")
raise DeadlineExceededError
else:
time.sleep(sleeptime)

except DeadlineExceededError:
logging.error("Ran out of time.")
self.response.clear()
self.response.set_status(500)
self.response.out.write("This operation could not be
completed in time...")

logging.info("About to write normal result message")
self.response.out.write("This is the normal result message")

When I request "/test?sleep=30" I get the "502 Server Server Error"
page and the in my logs:

(E) 02-22 09:31PM 05.652
Ran out of time.
(I) 02-22 09:31PM 05.653
About to write normal result message

When I request "/test?sleep=0" I get the expected "This operation
could not be completed in time..." message page. Anyone know what's
up with this? Thanks for any help!

-Lenza

Marzia Niccolai

unread,
Feb 23, 2009, 1:53:13 PM2/23/09
to google-a...@googlegroups.com
Hi Lenza,

This works for me.  So I think it might be one of two things.

First, are you importing the correct DeadlineExceededError?  You need to make sure to have this import:
from google.appengine.runtime import DeadlineExceededError

If you don't, sleeping for 30 seconds will raise this error, but it won't be caught.

The other thing is that you need to explicitly return your handler after you print the error message or else the handler will keep executing, and that may be the cause of the error (not returning the message quick enough).  So, modify the exception with:

except DeadlineExceededError:
  logging.error("Ran out of time.")
  self.response.clear()
  self.response.set_status(500)
  self.response.out.write("This operation could not be completed in time...")
  return

-Marzia

lenza

unread,
Feb 24, 2009, 5:07:33 AM2/24/09
to Google App Engine
Thanks for the response Marzia. I tried again with the return
statement and I am still having the same issue. The code I pasted
below is the entirety of my handler, so there is nothing in it that
would cause a timeout after the initial DeadlineExceededError. I also
checked that I am importing the correct DeadlineExceededError, and I
can tell the error is being caught because I get the "Ran out of
time." message in my logs. Any other ideas?

It is also interesting to note that for most exceptions I get a stack
trace of the error. In this case I am getting a Google branded page
titled "502 Server Error" that says:

Google Error

Server Error
The server encountered a temporary error and could not complete
your request.

Please try again in 30 seconds.

What is this page?
> > (seehttp://code.google.com/appengine/docs/python/runtime.html):

Marzia Niccolai

unread,
Feb 24, 2009, 4:20:26 PM2/24/09
to google-a...@googlegroups.com
Hi,

I really am out of ideas since it works perfectly for me (see http://yo.appspot.com/?sleep=2 vs http://yo.appspot.com/?sleep=35) where I used your exact handler information.

The issue is that, for some reason, the handler continues to execute too long after the DeadlineExeededError is thrown.  So when you see the Google Page this is the expected behavior.  But if all you do is render that simple text after catching the error, it's strange that you exceed the second deadline.

Are you sure there are no indenting or other issues that may cause your script to execute something after self.response.out.write("This is the normal result message")?

Also, it's worth mentioning that the dev_appserver does strictly enforce the request limit times as the production system does, so it may not be something you notice on the dev_appserver when trying to test.

-Marzia

lenza

unread,
Feb 24, 2009, 5:03:03 PM2/24/09
to Google App Engine
Thanks so much for testing this out Marzia! It is really awesome how
super helpful you are. You have helped me get one step closer to a
solution...

I have my app setup to use my own domain. When I go directly to my
appspot.com address it works:

http://lenzasapp2.appspot.com/test?sleep=35

but going through my domain fails:

http://apartmenthunter.lenza.org/test?sleep=35

This give you any ideas? Thanks again for your help!

-Lenza

On Feb 24, 1:20 pm, Marzia Niccolai <ma...@google.com> wrote:
> Hi,
>
> I really am out of ideas since it works perfectly for me (seehttp://yo.appspot.com/?sleep=2vshttp://yo.appspot.com/?sleep=35) where I

Marzia Niccolai

unread,
Feb 24, 2009, 5:18:26 PM2/24/09
to google-a...@googlegroups.com
AH! There definitely is a problem here with the custom domain, for some reason it's throwing a 'Bad Gateway' error when I grab the headers. I don't know why yet, but I'll keep you posted

-Marzia

GAEfan

unread,
Mar 9, 2009, 1:53:13 PM3/9/09
to Google App Engine
Any news on this? These timeouts are killing me.

Thanks,

Ken

Gijsbert

unread,
Mar 11, 2009, 12:49:56 AM3/11/09
to Google App Engine
I was curious what my error template does with this
DeadlineExceededError but no cigar.
I use the webapp.RequestHandler.handle_exception() but it does not
seem to get called for DeadlineExceededError. In dev server runtime/
__init__.py DeadlineExceededError is derived from Exception or
BaseException? Is the DeadlineExceededError not derived from
Exception? That would explain why handle_exception is not called.

Cheers,
Gijsbert
Reply all
Reply to author
Forward
0 new messages