Thread overview
Vibe.d WebSockets crashing the thread
Mar 22, 2019
a
Mar 22, 2019
Sebastiaan Koppe
Mar 22, 2019
a
Mar 22, 2019
Sebastiaan Koppe
Mar 22, 2019
a
Mar 24, 2019
Benjamin Schaaf
Apr 20, 2019
arakan arkino
Apr 20, 2019
Benjamin Schaaf
Mar 22, 2019
a
March 22, 2019
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/vibe/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!

March 22, 2019
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.

March 22, 2019
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?
March 22, 2019
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.
March 22, 2019
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.
March 22, 2019
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?
March 24, 2019
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.
April 20, 2019
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?

April 20, 2019
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.