meltwater / served

A C++11 RESTful web server library
MIT License
710 stars 174 forks source link

random chrashes/keep alive #14

Closed martinlierschof closed 6 years ago

martinlierschof commented 8 years ago

Using msvc 14 and win10 i get some random crashes/heap corruptions. boost version is 1.6.0. To get it to work in msvc i had to rewrite url::decode and fix some char[dynamicalloc] things in multiplexer. Those rewrites are probably not causing the issue. If you are interested i can send you the code. i have done quite the same implementation as you did (boost asio example) in another project with keep alive and dropping the connection when currently active caused kind of the same corruptions (i cannot access this code anymore due to i left this company).

the exception appears here:

server.exe!boost::asio::detail::buffer_cast_helper(const boost::asio::const_buffer & b) Line 276 C++ server.exe!boost::asio::buffer_cast<void const __ptr64>(const boost::asio::const_buffer & b) Line 435 C++ server.exe!boost::asio::detail::buffer_sequence_adapterboost::asio::const_buffer,boost::asio::const_buffers_1::validate(const boost::asio::const_buffers_1 & buffer_sequence) Line 255 C++ server.exe!boost::asio::detail::win_iocp_socket_send_opboost::asio::const_buffers_1,boost::asio::detail::write_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service,boost::asio::const_buffers_1,boost::asio::detail::transfer_all_t,void (boost::system::error_code, unsigned __int64) > >::do_complete(boost::asio::detail::win_iocp_io_service * owner, boost::asio::detail::win_iocp_operation \ base, const boost::system::error_code & result_ec, unsigned int64 bytes_transferred) Line 74 C++ server.exe!boost::asio::detail::win_iocp_operation::complete(boost::asio::detail::win_iocp_io_service & owner, const boost::system::error_code & ec, unsigned __int64 bytes_transferred) Line 47 C++ server.exe!boost::asio::detail::win_iocp_io_service::do_one(bool block, boost::system::error_code & ec) Line 406 C++ server.exe!boost::asio::detail::win_iocp_io_service::run(boost::system::error_code & ec) Line 164 C++ server.exe!boost::asio::io_service::run() Line 59 C++ server.exe!served::net::server::run::l7::() Line 86 C++

In addition to that i dont see any implementation for http keep alive. Please correct me if iam wrong.

martinlierschof commented 8 years ago

After a few debug sessions i found the crash. I cannot explain it though but it probably has something to do with the buffer initialisation in when writing to the socket in the following part of the code.

void
connection::do_write()
{
    auto self(shared_from_this());

    boost::asio::async_write(_socket, boost::asio::buffer(_response.to_buffer()),
        [this, self](boost::system::error_code ec, std::size_t) {

As a temporary fix i did the following (MSVC and Lambdas have a lot of bugs so i do not trust lambdas, but this is probably not related). More its the usage of the buffer which i new before the async and only delete when handle_write has been called. Also i tried to do this with a shared_ptr but the same crash.

void
connection::do_write()
{
    std::string* resp = new std::string(_response.to_buffer());
    boost::asio::async_write(_socket, boost::asio::buffer(*resp),
        boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, resp));
}

void
connection::handle_write(const boost::system::error_code& ec, std::size_t bytes_transferred, std::string* resp)
{
    delete resp;
}

does someone care to explain?

Jeffail commented 8 years ago

Hey @martinlierschof, sorry for the late response. I'll take a look at this soon.

Jeffail commented 6 years ago

Hey @martinlierschof, any chance this issue is still reproducible for you? I failed to reproduce on a few OSes but don't have a windows machine to test with.