Listener.d: crash on dirty shutdown

For the small chat server that I am doing, I decided to use D; To find myself with a very neat example from listener.d, to get a hit, I decided to pretty much consider the example! However, I am stuck in a mistake, I can not really wrap my finger. This is most likely my fault, and I am doing something wrong, but given that I took the code largely from the example, I am more likely to believe that the example is broken.

I will explain what will happen:

  • List item
  • I start my server (it's okay, it works as it should and does not listen)
  • I am using telnet. My server accepts the connection.
  • I use telnet to send some information. The server processes the information correctly, again, no problem.
  • I leave telnet using ^] and then write quit. Disruption of the connection is rather unceremonious.
  • The server correctly recognizes that this is not a clean shutdown and that the code to delete the socket is executed.
  • Then I get a range violation.

This is the main process and cycle: https://github.com/JGBrands/BlaatServer/blob/master/source/bserver.d

This is a server class, the code in which it removes the socket is at the bottom in the function void destroySocket (int index);

https://github.com/JGBrands/BlaatServer/blob/master/source/server.d

Actually let me copy the paste. :-)

void destroySocket(int index) {
    this.reads[index].close(); /* release resources. */

    /* Remove the socket now. We don't want this around! It'll crash us! */
    if (index != this.reads.length -1)
        this.reads[index] = this.reads[this.reads.length -1];

    this.reads = this.reads[0 .. this.reads.length -1];
    writeln("Total connections: " ~ to!string(this.reads.length));
}

The code is mainly taken from the listener.d example, as I said, the error I get is:

core.exception.RangeError@server(61): Range violation 
---------------- 
----------------

I was lucky that the function deletes what it should not, for those interested, this is line 61 in server.d:

if (this.sset.isSet(this.reads[i])) { 

, , , , - ?

+3
1

:

if (index != this.reads.length -1)

, . , . , .

void destroySocket(int index)
in {
    assert(index > -1);
    assert(index < this.reads.length);
} body {
{
0

All Articles