breese / trial.datagram

UDP socket with Boost.Asio
2 stars 0 forks source link

assert error in async_echo_server #1

Closed gegles closed 5 years ago

gegles commented 5 years ago

Hello,

(Potentially an awesome library, thanks!). I was just trying to run the example and got the following error when running async_echo_server:

❯ ./async_echo_server 54321                                                        
Assertion failed: (acceptor_queue.empty()), function ~multiplexer, file /Users/gegles/Workspaces/playground/trial.datagram/include/trial/datagram/detail/multiplexer.ipp, line 44.
[1]    37702 abort      ./async_echo_server 54321

I am running this on macOS (Catalina). Any clue, am I missing something?

breese commented 5 years ago

I can confirm this problem on OSX. The problem does not occur on Linux.

Looks like the io_context returns immediately without waiting for pending operations, so the multiplexer destructor complains about those.

breese commented 5 years ago

Further investigation shows that the async_receive_from() with message_peek in multiplexer::do_start_receive() triggers the callback immediately, rather than when an incoming datagram has arrived.

gegles commented 5 years ago

Ah ah, yes, that's it, I remember facing that very same issue in a little POC I did. Somehow macOS does not like it if you pass it an empty buffer for the peek. I had to create a one byte peek_ buffer just for that:

        socket_.async_receive_from(net::buffer(peek_), remote_endpoint_, net::socket_base::message_peek, [this](lib::error_code ec, std::size_t) {
            if (ec) {
                std::cerr << "UDP Accept error: " << ec.message() << "\n";
                return;
            }

            std::cout << "Accepted! local=" << socket_.local_endpoint() << " remote=" << remote_endpoint_ << "\n";
            socket_.connect(remote_endpoint_);
            std::make_shared<Session>(std::move(socket_))->Start();
            DoAccept();
        });
    }

    std::uint16_t       port_;
    udp::socket         socket_;
    udp::endpoint       remote_endpoint_;
    std::array<char, 1> peek_;

You may know of a better way to solve this..

Unless you can fix it fast, I'll try to submit a PR when I get to it. thanks!

breese commented 5 years ago

Thanks, that works.

Committed in 24f3a908f1634f7924501d8846a5ee7568966d85

gegles commented 5 years ago

You da man! Thanks! it works.