Add error handling and enable graceful shutdown#2131
Add error handling and enable graceful shutdown#2131IronJam11 wants to merge 4 commits intodjango:mainfrom
Conversation
channels/generic/http.py
Outdated
| if not logger.hasHandlers(): | ||
| handler = logging.StreamHandler() | ||
| formatter = logging.Formatter( | ||
| "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | ||
| ) | ||
| handler.setFormatter(formatter) | ||
| logger.addHandler(handler) | ||
| logger.setLevel(logging.DEBUG) | ||
|
|
There was a problem hiding this comment.
Adding the logging call is fine I guess, but I don't think we should be adding handlers for users. That's up to them.
|
|
||
| Note that the ASGI spec requires that the protocol server only starts | ||
| sending the response to the client after ``self.send_body`` has been | ||
| called the first time. |
There was a problem hiding this comment.
Can you undo all of these unrelated changes to comments please.
There was a problem hiding this comment.
I am so sorry about these
| await self.handle(b"".join(self.body)) | ||
| except Exception: | ||
| logger.error(f"Error in handle(): {traceback.format_exc()}") | ||
| await self.send_response(500, b"Internal Server Error") |
There was a problem hiding this comment.
So this is the essence of the proposal.
channels/generic/http.py
Outdated
| try: | ||
| await self.disconnect() | ||
| await aclose_old_connections() | ||
| except Exception as e: | ||
| logger.error(f"Error during disconnect: {str(e)}") | ||
| finally: | ||
| raise StopConsumer() |
|
Made the changes as requested |
carltongibson
left a comment
There was a problem hiding this comment.
Thanks @IronJam11. I need to test this against an actual application, so give me a cycle.
channels/generic/http.py
Outdated
| try: | ||
| await self.handle(b"".join(self.body)) | ||
| except Exception: | ||
| logger.error(f"Error in handle(): {traceback.format_exc()}") |
There was a problem hiding this comment.
What do you think about using logger.exception() ?
|
I was a bit caught up with a hackathon. apologies for the wait. |
|
No problem. No rush. I need to find a cycle to sit down with this properly. 👀 |
|
Any update on this PR? |
PR Description:
Closes #1350
Deliverables:
handle()function.Changes:
Error Handling in
handle():handle()function execution.logger.error().500 Internal Server Errorresponse usingself.send_response()when an error occurs.Graceful Shutdown:
self.send_response().Improved Error Handling in
http_disconnect():