python-websockets / websockets

Library for building WebSocket servers and clients in Python

Home Page:https://websockets.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Showing connection failed (200 OK) when executed health check endpoint

srikamalteja opened this issue · comments

Hello Author,

I have created an endpoint to check the health of the Websocket server based on this example https://github.com/python-websockets/websockets/blob/main/example/faq/health_check_server.py

After the endpoint is executed from process_request it is showing below logs

connection failed (200 OK)
connection closed

so my question is why connection failed although it is showing 200 OK? am I doing anything wrong? below is my code.

async def health_check(path, request_headers):
if path == "/health_check":
logging.info(request_headers)
return http.HTTPStatus.OK, [], b"OK"

start_server = await websockets.serve(engine_class.handle_request, host=argument_parser.host, port=argument_parser.port, process_request=health_check )
await start_server.wait_closed()

Executing a curl command from the Dockerfile.

HEALTHCHECK --interval=1m --timeout=3s CMD curl --header "Connection: close" http://localhost:9980/health_check || exit 1

Thank you.

The log message is "technically correct" 😉 in the sense that the client didn't establish a WebSocket connection successfully.

You consider the HTTP connection successful. Unfortunately, websockets can't tell because it doesn't know about HTTP in general 😅

This doesn't feel like a major issue because people usually look at logs when things fail, not when they work, but I recognize that it can be confusing and I'd be open to a SIMPLE change to improve that.

Yes, I suspected that it's the correct scenario! But had a benefit of doubt, so thought to raise.

Thanks for considering, cheers.

I made a simple change :-) I don't claim that it solves the issue entirely but at least it gives you a better chance to understand that the WebSocket connection is rejected (by sending an HTTP response).