Skip to content

Sometimes it doesn't trigger the react event. #54

@demanuel

Description

@demanuel

I'm trying to use this library to connect to a server.

Everytime a connection is established a welcome message is sent from the server to the client. So I have the script with the following function:

sub create_sockets(Str $server,
                   Int $port,
                   Str $username,
                   Str $password,
                   Int $connections,
                   Bool $TLS,
                   Bool $insecure
                   --> List
                   ){

    my @socket_list;
    my Lock $lock = Lock.new;
    my @promises := do for 1..$connections -> $i {
        start {
            try {
                my $conn;
                if $TLS {
                    $conn = await IO::Socket::Async::SSL.connect($server, $port, :$insecure);
                } else {
                    $conn = await IO::Socket::Async.connect($server, $port);
                }

                react whenever $conn.Supply -> $line {
                    print $line;
                    if $line ~~ /200/ {
                        await $conn.print("authenticate user $username\r\n");
                    } elsif $line ~~ /381/ {
                        await $conn.print("authenticate pass $password\r\n");
                    } elsif $line ~~ /281/ {
                        done;
                    } elsif $line ~~ /481/ {
                        $*ERR.print($line);
                        $conn.close;
                        exit(1);
                    }
                }
                $lock.protect: {
                    @socket_list.push($conn);
                }
                CATCH  {
                    default {
                        $*ERR.say: "Got exception: { .Str.say; }\n{ .message }";
                        $*ERR.say: .backtrace.full;
                        .die;
                    }
                }
            }
        }
    }
    my $all_done = Promise.allof(@promises);
    await $all_done;
    return @socket_list;
}

And sometimes it hangs.

Because i have a print $line in the react i can see that when it hangs, is because 1 socket that doesn't enter the react block. This never happens with "IO::Socket::Async" (The problem is also not on the server).

I'm not sure how can i debug this. This was the best i could come up with. Is there anything wrong with that function? How can i debug this?

Thanks in advance,
David Santiago

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions