www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Vibe.d WebSockets crashing the thread

reply a <email example.edu> writes:
I am working on a project (multiplayer browser-based game) that 
uses websockets. I structured the project so that there are 2 
threads: one of them is the vibe.d web server, and the other is 
the actual server for the game itself.

Because of this structure, I am passing WebSockets from the 
webserver to the game server using std.concurrency.send. This 
seems to barely work though, and it is really unreliable (crashes 
after a few seconds). Sometimes it gives and error about 
core.exception.AssertError ../../.dub/packages/vibe-core-1.6.1/vibe-core/source/v
be/core/net.d(593): Assertion failure, but other times it just simply stops
receiving messages from the websocket (I think this is because the game-server
thread may be crashing, but I'm not sure).

Here is some minimal code that reproduces the issue:
source/app.d:
http://dpaste.com/2QCSXC1

and public/index.html:
http://dpaste.com/02QANJY

I would really appreciate if anyone could help me fix this.
Thanks!
Mar 22 2019
parent reply Sebastiaan Koppe <mail skoppe.eu> writes:
On Friday, 22 March 2019 at 20:23:05 UTC, a wrote:
 Here is some minimal code that reproduces the issue:
 source/app.d:
 http://dpaste.com/2QCSXC1

 and public/index.html:
 http://dpaste.com/02QANJY

 I would really appreciate if anyone could help me fix this.
 Thanks!
I don't think it is safe to pass a vibe-d Websocket to another thread. It might very well insist on staying on the thread it is created from. Instead you could do all reading in the handler function and then only pass the (immutable) data to another thread.
Mar 22 2019
next sibling parent reply a <email example.edu> writes:
On Friday, 22 March 2019 at 20:49:52 UTC, Sebastiaan Koppe wrote:
 On Friday, 22 March 2019 at 20:23:05 UTC, a wrote:
 Here is some minimal code that reproduces the issue:
 source/app.d:
 http://dpaste.com/2QCSXC1

 and public/index.html:
 http://dpaste.com/02QANJY

 I would really appreciate if anyone could help me fix this.
 Thanks!
I don't think it is safe to pass a vibe-d Websocket to another thread. It might very well insist on staying on the thread it is created from. Instead you could do all reading in the handler function and then only pass the (immutable) data to another thread.
Hm, I might have to do that. But would that explain why is works fine for a little while before crashing?
Mar 22 2019
parent reply Sebastiaan Koppe <mail skoppe.eu> writes:
On Friday, 22 March 2019 at 20:59:33 UTC, a wrote:
 On Friday, 22 March 2019 at 20:49:52 UTC, Sebastiaan Koppe 
 wrote:
 Hm, I might have to do that. But would that explain why is 
 works fine for a little while before crashing?
What I forgot to mention is that you are calling methods on the socket from two threads, and I dont think those methods are thread safe. Which would explain the random crashes.
Mar 22 2019
parent reply a <email example.edu> writes:
On Friday, 22 March 2019 at 22:51:11 UTC, Sebastiaan Koppe wrote:
 On Friday, 22 March 2019 at 20:59:33 UTC, a wrote:
 On Friday, 22 March 2019 at 20:49:52 UTC, Sebastiaan Koppe 
 wrote:
 Hm, I might have to do that. But would that explain why is 
 works fine for a little while before crashing?
What I forgot to mention is that you are calling methods on the socket from two threads, and I dont think those methods are thread safe. Which would explain the random crashes.
I tried getting rid of the function calls in the vibe.d thread, but the crashes keep happening. Any other ideas?
Mar 22 2019
parent reply Benjamin Schaaf <ben.schaaf gmail.com> writes:
On Friday, 22 March 2019 at 23:44:58 UTC, a wrote:
 On Friday, 22 March 2019 at 22:51:11 UTC, Sebastiaan Koppe 
 wrote:
 On Friday, 22 March 2019 at 20:59:33 UTC, a wrote:
 On Friday, 22 March 2019 at 20:49:52 UTC, Sebastiaan Koppe 
 wrote:
 Hm, I might have to do that. But would that explain why is 
 works fine for a little while before crashing?
What I forgot to mention is that you are calling methods on the socket from two threads, and I dont think those methods are thread safe. Which would explain the random crashes.
I tried getting rid of the function calls in the vibe.d thread, but the crashes keep happening. Any other ideas?
Not sure about WebSockets, but vibe.d often has the requirement that things are accessed from the same thread that created them. I'd try using fibers instead of threads and seeing how that goes.
Mar 24 2019
parent reply arakan arkino <marcosdonalonso gmail.com> writes:
On Sunday, 24 March 2019 at 09:56:21 UTC, Benjamin Schaaf wrote:
 On Friday, 22 March 2019 at 23:44:58 UTC, a wrote:
 On Friday, 22 March 2019 at 22:51:11 UTC, Sebastiaan Koppe 
 wrote:
 On Friday, 22 March 2019 at 20:59:33 UTC, a wrote:
 On Friday, 22 March 2019 at 20:49:52 UTC, Sebastiaan Koppe 
 wrote:
 Hm, I might have to do that. But would that explain why is 
 works fine for a little while before crashing?
What I forgot to mention is that you are calling methods on the socket from two threads, and I dont think those methods are thread safe. Which would explain the random crashes.
I tried getting rid of the function calls in the vibe.d thread, but the crashes keep happening. Any other ideas?
Not sure about WebSockets, but vibe.d often has the requirement that things are accessed from the same thread that created them. I'd try using fibers instead of threads and seeing how that goes.
I tried getting rid of the function calls in the vibe.d thread, but the crashes keep happening. Any other ideas?
Apr 20 2019
parent Benjamin Schaaf <ben.schaaf gmail.com> writes:
On Saturday, 20 April 2019 at 19:24:28 UTC, arakan arkino wrote:
 I tried getting rid of the function calls in the vibe.d thread, 
 but the crashes keep happening. Any other ideas?
Could you post your new code, that will probably narrow it down further.
Apr 20 2019
prev sibling parent a <email example.edu> writes:
On Friday, 22 March 2019 at 20:49:52 UTC, Sebastiaan Koppe wrote:
 On Friday, 22 March 2019 at 20:23:05 UTC, a wrote:
 Here is some minimal code that reproduces the issue:
 source/app.d:
 http://dpaste.com/2QCSXC1

 and public/index.html:
 http://dpaste.com/02QANJY

 I would really appreciate if anyone could help me fix this.
 Thanks!
I don't think it is safe to pass a vibe-d Websocket to another thread. It might very well insist on staying on the thread it is created from. Instead you could do all reading in the handler function and then only pass the (immutable) data to another thread.
Also, sending data _to_ the websocket from the 2nd thread works fine.
Mar 22 2019