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

WebSocketApp connection not closing as expected #962

Open
Juliet-Kahofer opened this issue Dec 20, 2023 · 1 comment
Open

WebSocketApp connection not closing as expected #962

Juliet-Kahofer opened this issue Dec 20, 2023 · 1 comment

Comments

@Juliet-Kahofer
Copy link

I have inherited a program that makes a websocket connection to a server, reads messages from a queue then closes:

    print("Starting the retreiving process")
    ws = websocket.WebSocketApp(ws_url,
                                header={'authorization':'Basic ' + auth},
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
                                #if error exit
    print("Connected to websocket")
    ws.on_open = on_open
    wst=threading.Thread(target=ws.run_forever)
    print("Starting the retreiving process")
    wst.start()
    time.sleep(float(active_time))
    ws.keep_running=False
    wst.join()

This was working fine for us. However, I've had to amend the Thread line as follows, to stop the connection carrying out SSL certificate checks:
wst=threading.Thread(target=ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}))

The program now successfully makes a connection to the server but never closes.
I've tried explicitly closing the connection with ws.close(), but no luck.

Two questions:

  • is this expected behaviour - i.e. does bypassing ssl certificate checks mean that the connection will not close?
  • how can I force the connection to close (after active_time seconds)?
@engn33r
Copy link
Collaborator

engn33r commented Dec 24, 2023

  1. No, this is not expected. The only logic that should be associated with this cert_reqs value are in _wrap_sni_socket in _http.py
  2. You can use a ping_timeout to close the connection if there is no response from the server after a certain time. If you want to close the connection even though the server is responding, you should handle it in on_message or a similar callback.

Please make sure you are using the latest version of the library and consider checking issues #612 and #592 related to closing a connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants