-
Notifications
You must be signed in to change notification settings - Fork 760
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
on_close() doesn't get called after calling close() #612
Comments
Same issue after upgrading to 0.57.0. Went back to 0.40.0 and all worked fine again. |
Yeah, I used 0.57.0. |
The same issue with me, I had to downgrade to 0.54.0 |
Lost few hours on this, using 0.47 now haha |
I downgraded to 0.52.0 and it worked, not working on above versions. |
It seems it has evolved, in 0.58 I have this error on KeyboardInterrupt when assigning any function to on_close:
KeyboardInterrupt
|
@lalondesteve Thanks for posting a nice simple example. I was able to recreate the error you got and the issue is that your on_close() function needs to accept the WebSocketApp as an argument. The following fixes your error: def on_close(ws): # This line has the fix
print("closed")
ws = websocket.WebSocketApp("ws://echo.websocket.org/", on_close=on_close)
ws.run_forever() I think the original issue here is different though. If I understand the original issue correctly, the following code snippet recreates the original issue where the word "closed" is never printed out, even though the connection does get closed: import websocket
websocket.enableTrace(True)
def on_open(wsapp):
wsapp.send("hi")
def on_message(wsapp, message):
print(message)
wsapp.close()
def on_close(wsapp): # new
print("closed")
wsapp = websocket.WebSocketApp("ws://echo.websocket.org", on_open=on_open, on_message=on_message, on_close=on_close)
wsapp.run_forever() |
You're right, I'm an idiot! Thanks for this very nice answer. FYI, in my tests after your fix, the "closed" is printing with >>> import websocket
>>> from threading import Thread
>>>
>>> def on_close(wsapp):
... print("closed for real")
>>>
>>> wsapp = websocket.WebSocketApp("ws://echo.websocket.org", on_close=on_close)
>>> t = Thread(target=wsapp.run_forever)
>>> t.start()
>>> wsapp.close()
closed for real |
A follow-up on the original issue of this thread: it looks like the Edit: plus lalondesteve helpfully confirmed that he called |
@engn33r Thanks firstly, I will try to recreate this error in a few days. |
I looked at the code example from @lalondesteve and discovered that the on_close() function in his comment was only called because of the explicit One weird behavior I encountered with the working threading code example posted on the threading documentation page was that it doesn't usually work for ws://echo.websocket.org. I encountered strange behavior where about 1 in 5 tries connecting to ws://echo.websocket.org did result in calling Since there is new example code on the threading page that should provide a solution to the simplest case of this issue (ignoring the variable behavior that I observed for certain endpoints), I am closing this issue for now. We can reopen discussions if there are still loose ends on this topic or if issues with the variable behavior are encountered and problematic. |
I was able to find out something else about this issue. I must admit that this is the first time I have encountered a problem that can be solved by a print. XD Here are the examples:
Maybe this will help someone who had the same problems as me and didn't put a |
@engn33r I also spend some time debugging this issue and it seems to me that it is a matter of timing. This is what I found:
So whenever you call Whenever you defer close call (and from my tests it seems that the thread needs to be somehow delayed - sleep, print, whatever), the original My current solution is to call close using a timer (it seems to works every repeat):
I guess this might be fully solved by not using |
I will reopen this issue because it may not be solved completely based on these reports. |
I also have issues with this, on_close is never called no matter what. |
FYI for everyone here. I might have fixed this bug while actually hunting another issue, as you can see in the PR above. I would love to hear if this bug is resokved in 1.6.0! |
Thank you very much @QuinnDamerell and @engn33r ! I plan to test your fix in the following weeks and I'll let you know. |
Thank you very much for fixing it @QuinnDamerell ! |
code just like:
After calling
run()
above and connection successful, I callself._ws.close()
somewhere, Buton_close()
func doesn't get called. What's wrong?In _app.py:
As above, After calling
self._ws.close()
,self.keep_running
is set toFalse
, Then in _app.py:the
while
loop inread()
func will be over according toself.keep_running
. Afterdispatcher.read()
, nothing is going to be done inrun_forever()
func.Is there some suggestions? Thks.
The text was updated successfully, but these errors were encountered: