Skip to content

WebSocket cleanup on stopping crow::App #529

@kang-sw

Description

@kang-sw

Firsly, I really appreciate your amazing work. It literally changed my life :D.

And apologies for my short English skills as I don't natively use it.


The text below is logging from my application which is under development currently.

---> Normal cycle: accept - open - close
[2022-08-28 08:45:44.399] [__PERFKIT:BACKEND] [info] [web_terminal.cpp:185] * Accepting terminal WebSocket from: 127.0.0.1
[2022-08-28 08:45:44.399] [__PERFKIT:BACKEND] [info] [web_terminal.cpp:192] * Opening WebSocket: 127.0.0.1
[2022-08-28 08:45:47.139] [__PERFKIT:BACKEND] [info] [web_terminal.cpp:163] * (127.0.0.1) Closing websocket: ��
[2022-08-28 08:45:47.139] [__PERFKIT:BACKEND] [info] [web_terminal.cpp:180] * WebSocket closed. (0.000 seconds)
---> 

---> Stopping application while there are any active websocket connection:
---> Cleanup logic is not called!
[2022-08-28 08:45:47.562] [__PERFKIT:BACKEND] [info] [web_terminal.cpp:185] * Accepting terminal WebSocket from: 127.0.0.1
[2022-08-28 08:45:47.562] [__PERFKIT:BACKEND] [info] [web_terminal.cpp:192] * Opening WebSocket: 127.0.0.1
[2022-08-28 08:45:51.855] [info] CMD: quit
[2022-08-28 08:45:51.855] [__PERFKIT] [trace] invoking as tokens
[2022-08-28 08:45:51.855] [info] Disposing application
[2022-08-28 08:45:51.855] [info] Destroying application instance
[2022-08-28 08:45:51.855] [info] Disposing terminal.
(2022-08-27 23:45:51) [INFO    ] Closing IO service 0000018D4EEB7190
(2022-08-27 23:45:51) [INFO    ] Closing main IO service (0000018D4EF028A0)
(2022-08-27 23:45:51) [INFO    ] Exiting.

It seems active websockets does not receive onclose() callback call on application disposal.

It causes problem as I usually dynamically allocate userdata object on onaccept() routine, to implement notification without explicit client request. (Holding connection references outside of on*() callback routines will make calling send_text|send_binary() at random timing available)

Currently I'm releasing allocated memory from onclose() callback routine. However, if it's not properly called on crow::App's stop sequence, it results in memory leak.


I just found your TODO message inside of stop() method after writing this ... I believe you have a plan to resolve this. Please just take below content as an idea ...

It seems there's already logic to handle it, however, it seems there are a few problems with it.

  1. Newly connected websocket instances are not registered within Crow::::websockets_ field automatically. Thus, user have to make call to add_websocket() and remove_websocket() on their own websocket handler routine.
  2. However, any access to websocket_ field is not properly protected with any mutex or critical section. Link to add_websocket, remove_websocket
  3. And, even if websocket_ access routines are made atomic, as they are invoked prior to actual http_server::stop() call, new websocket connection can be made during existing websocket cleanup routines, which will make add_websocket call, after copying websocket_to_close content.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfeatureCode based project improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions