Skip to content

Conversation

@cswiedler
Copy link

Make WebSocketFrame reads fully async. Process close requests via the
message queue, so that anything received prior to the close frame is
handled

Make WebSocketFrame reads fully async. Process close requests via the message queue, so that anything received prior to the close frame is handled
@cswiedler
Copy link
Author

First time using GitHub so the request may be a little wonky.

We've found that at least in Unity (and maybe elsewhere), the way websocket-sharp does reads can cause deadlocks. The problem is that websocket-sharp does an async read to get the header for a frame, and then does synchronous reads to get the rest. If the synchronous reads block, then Bad Things can supposedly happen. Up until now we've put in a hack to use QueueUserWorkItem to do threaded blocking reads under Unity, but I'm concerned that even outside of Unity, the async/sync combination could be a problem, so this is my attempt to fix it.

So far this is only lightly tested.

@sta
Copy link
Owner

sta commented Aug 20, 2015

Hello there,

I plan to merge this manually by inches, adding some changes.

And when i merge your code, i will add a contributor info such as below.

/* * Contributors: * - cswiedler (or another name) <your e-mail address if you have> */ 

Is it okay?

Very thanks!

@cswiedler
Copy link
Author

Sure, thanks. Unfortunately it turns out that it still doesn’t completely solve the hang we see in Unity. If I can’t figure it out, I’ll add an option to enable doing the receives on a thread (which is the workaround you suggested a while back). I’ll send another pull request when I have that in.

chris

From: sta [mailto:notifications@github.com]
Sent: Thursday, August 20, 2015 1:25 AM
To: sta/websocket-sharp
Cc: Chris Swiedler
Subject: Re: [websocket-sharp] Make WebSocketFrame reads fully async (#153)

Hello there,

I plan to merge this manually by inches, adding some changes.

And when i merge your code, i will add a contributor info such as below.

/*

  • Contributors:

    • cswiedler (or another name)

    */

Is it okay?

Very thanks!


Reply to this email directly or view it on GitHubhttps://github.com//pull/153#issuecomment-132934360.

sta added a commit that referenced this pull request Sep 13, 2015
Merge a part of pull request #153 from cswiedler@fd5fc9a.
sta added a commit that referenced this pull request Sep 13, 2015
Merge a part of pull request #153 from cswiedler@fd5fc9a.
sta added a commit that referenced this pull request Sep 13, 2015
Add ReadBytesAsync2 (Stream, long, int, Action<byte[]>, Action<Exception>) method to support pull request #153
sta added a commit that referenced this pull request Sep 28, 2015
Merge a part of pull request #153 from cswiedler@fd5fc9a.
@253153
Copy link

253153 commented Feb 17, 2017

@cswiedler Were you able to get to the bottom of where the problem in Unity was occurring? Or if possible, could you show me how you hacked the QueueUserWorkItem? I am experiencing lag (using websockets-sharp for client only) every couple of seconds, and I believe it is caused by this issue.

@cswiedler
Copy link
Author

We ended up doing all network IO under Unity on a thread using blocking operations. It may be better in more recent versions. As far as I know, the changes I posted here would only be necessary/helpful if you were running out of threadpool threads, since in that case an async callback from one network operation would perform a (potentially) blocking read, preventing that threadpool thread from being released.

@253153
Copy link

253153 commented Feb 17, 2017

@cswiedler I am using a lock in the update function for processing the data, but I'm not sure if it's working that well since I get lag when getting packets every couple seconds in my unity client that's not present in my javascript web app client, is that the same thing you are talking about?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants