Crow icon indicating copy to clipboard operation
Crow copied to clipboard

Fix websocket crash from connection pointer dangling

Open kang-sw opened this issue 2 years ago • 3 comments

On windows, with error 10053, a websocket instance sometimes deleted before post-ed/dispatch-ed asynchronous I/O events invocation, which makescompletion handler refer to dangled pointer 'this'.

We can detect whether 'this' pointer is deleted already or not, by checking captured weak_ptr is expired. Though this introduces a bit of overhead on each message posting/dispatching, it makes concurrent access to single websocket connection instance safe.

kang-sw avatar Aug 28 '22 06:08 kang-sw

Hi! Thanks for the fix! Sorry for the long wait 😓

Lets ask someone who might have windows on hand @The-EDev if he can reproduce the original bug.

Else I'll check this out in a few days.

dranikpg avatar Sep 10 '22 14:09 dranikpg

@dranikpg I haven't used windows in ages. Though this might be related to a problem I ran into in an early implementation of #269. I kept attempting to access Websocket connections after they've been deleted and couldn't figure out how or why.

The-EDev avatar Sep 10 '22 22:09 The-EDev

  1. Resolved merge conflict with master branch
  2. Resolved compilation warning due to c++14 features

kang-sw avatar Sep 24 '22 07:09 kang-sw

@dranikpg @kang-sw hey guys, is anything missing with this PR to move forward?

dangerden avatar Oct 09 '22 05:10 dangerden

oh, thank you for the speedy response! @dranikpg

dangerden avatar Oct 09 '22 18:10 dangerden

I would suggest explaining a thread-safe or at least a "recommended" way of using websocket::Connection outside of the asio handler flow. In a way it is related to #258 but taking into account the nature of websocket async communication might be even more urgent. In one way or another a server which needs to send a message to a connection has to capture a reference to the connection received much earlier in accept or previous messages. @dranikpg @kang-sw

Besides, I'm looking forward to #529 and refactor of websocket core parts. Crow is awesome :-) Thank you!

dangerden avatar Oct 09 '22 19:10 dangerden

You're right, it's not documented so far. Websockets are increasingly popular in crow, but unfortunately pretty rough so far.

I wouldn't say its related to the async handlers issue because lifetime management is to be managed differently.

Thanks for using Crow in spite of its small flaws :)

dranikpg avatar Oct 10 '22 19:10 dranikpg