samedi 2 février 2019

Nesting asio call ends in __throw_bad_function_call()

I wrote a wrapper around Boostless asio to handle all network communication. Most exchanges involve several packets being sent back and forth between the client and the server. I now have a problem where if I nest calls to asio methods I end up in the method __throw_bad_function_call();

Here's my listener:

.hpp

/// \brief Socket object used to manage connections to the remote server.
    struct listener : public std::enable_shared_from_this<listener> {
      /// \brief Creates an instance of the object and opens a connection to the remote host.
      /// \param[in] params Connection parameters.
      /// \param[out] ec Errors returned by the OS.
      explicit listener(const parameters &params, std::error_code &ec) noexcept;

      /// \brief Executes all queued handlers and resets the io_context for another run.
      auto execute_handlers() noexcept -> void;

      /// \brief Writes data to the socket.
      /// \details Writes data to the socket and passes any errors to the callback.
      /// \param[in] barray Byte array to send to the server.
      /// \param[in] callback Method called when data has been written to the server.
      auto write(packet barray, std::function<void(std::error_code ec, std::size_t length)> callback) noexcept -> void;

      /// \brief Reads data from the socket.
      /// \details Reads that present in the socket and passes the data as a single byte array to the callback.
      /// \param[in] callback Method called when data has been written to the server.
      auto read(std::function<void(std::error_code ec, packet barray)> callback) noexcept -> void;

     private:
      /// \brief Underlying OS socket.
      std::unique_ptr<asio::generic::stream_protocol::socket> m_socket;

      asio::io_context m_context;
      asio::local::stream_protocol::endpoint m_endpoint;

      /// \brief Connection parameters
      parameters m_params;

      /// \brief DNS resolver
      asio::ip::tcp::resolver m_resolver;

      /// \brief Buffer that holds data to be received from the socket. Reset and resized before each call to read().
      packet m_buffer;
    };

.cpp

    listener::listener(const parameters &params, std::error_code &ec) noexcept
        : m_endpoint{params.domain_socket}, m_params{params}, m_resolver{m_context} {
#if !defined(_WIN32) && !defined(_WIN64)
      if (!m_params.domain_socket.empty()) {
        m_socket.reset(new asio::generic::stream_protocol::socket(m_context));
        m_socket->connect(m_endpoint, ec);
        if (ec) {
          return;
        }
        m_socket->non_blocking(true);
        return;
      }
#endif

      m_resolver.async_resolve(m_params.host, std::to_string(m_params.port),
                               [&](const std::error_code &error, asio::ip::tcp::resolver::results_type results) {
                                 if (error) {
                                   ec = error;
                                   return;
                                 }
                                 //! \todo Add timeout to async_connect
                                 for (const auto &endpoint : results) {
                                   m_socket.reset(new asio::generic::stream_protocol::socket(m_context));
                                   m_socket->async_connect(endpoint.endpoint(), [&](const std::error_code &err_c) {
                                     if (err_c) {
                                       ec = err_c;
                                       return;
                                     }
                                   });
                                 }
                               });
    }

    auto listener::execute_handlers() noexcept -> void {
      const auto handlers = m_context.run();
      m_context.restart();
    }

    auto listener::write(packet barray, std::function<void(std::error_code ec, std::size_t length)> callback) noexcept
        -> void {
      asio::async_write(*m_socket, asio::buffer(barray), callback);
    }

    auto listener::read(std::function<void(std::error_code ec, packet barray)> callback) noexcept -> void {
      packet tmp;
      tmp.resize(1);
      asio::async_read(*m_socket, asio::buffer(tmp), asio::transfer_exactly(1),
                       [&](std::error_code ec, std::size_t size) {
                         if (!ec) {
                           const auto available = m_socket->available();
                           m_buffer.resize(available);
                           m_buffer.shrink_to_fit();
                           asio::async_read(*m_socket, asio::buffer(m_buffer), asio::transfer_all());
                         }
                         callback(ec, std::move(m_buffer));
                       });
    }

I then start an exchange with the remote server but lldb tells me we fail:

auto connection::do_connect(std::function<void(std::error_code ec, sql_state state)> callback) noexcept -> void {
m_listener = std::make_shared<listener>(m_params, ec);
      m_listener->execute_handlers();
      if (ec) {
        callback(ec, state);
      }
        sql_state state;
        const auto startup = write::startup(m_params);
        m_listener->write(startup, [&](std::error_code ec, std::size_t length) {
          if (ec) {
            callback(ec, state);
          }
          m_listener->read([&](std::error_code ec, packet packet){});
        });
        m_listener->execute_handlers();
      }

Running this in lldb fails with the following trace:

Process 82736 launched: '/Users/ruihpacheco/Desktop/databaseclient/build_ninja/tests/integration/integration' (x86_64)
Process 82736 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
    frame #0: 0x0000000100178118 integration`std::__1::function<void (std::__1::error_code, std::__1::vector<nonstd::byte, std::__1::allocator<nonstd::byte> >)>::operator(this=0x00007ffeefbfd780, __arg=(__val_ = 0, __cat_ = 0x00007fffa6cd6cd8), __arg=size=329)(std::__1::error_code, std::__1::vector<nonstd::byte, std::__1::allocator<nonstd::byte> >) const at functional:1913
   1910 {
   1911     if (__f_ == 0)
   1912         __throw_bad_function_call();
-> 1913     return (*__f_)(_VSTD::forward<_ArgTypes>(__arg)...);
   1914 }
   1915 
   1916 #ifndef _LIBCPP_NO_RTTI
Target 0: (integration) stopped.

Aucun commentaire:

Enregistrer un commentaire