You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Asynchronous HTTP Server (0.10.1): multiple connection->write causes exception #271
voidHttpServer::operator() (
server_type::request const& request,
server_type::connection_ptr connection) {
std::vector<server_type::response_header> headers;
connection->set_status(server_type::connection::ok);
connection->set_headers(
boost::make_iterator_range(headers.begin(), headers.end()));
for (int i = 0; i < 100; i++) {
connection->write(data);
}
}
causes:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'what(): Success
Program received signal SIGABRT, Aborted.
Thanks @mb1986 -- I think you're running into something really weird here. Can you try building this in Debug mode and posting a stack-trace of where the exception is being thrown? I suspect it's a race condition of sorts, or even just because your headers are empty. Did you intend to keep your headers empty?
Was this ever resolved? I am running into a similar issue. I'm calling writing a bunch in a row. A stack track is below. This is running on the latest version in the Ubuntu 14.04 repo, 0.11.
After examining the source it looks like an async_connection does not support multiple writes. After the first write completes (in another thread) the error_encountered variable is set. This causes any subsequent call to write to fail with this error. If the variable is only set if the status code is not success then it still fails; after the headers are sent all pending write operations are executed concurrently and subsequent writes eventually fail with Broken Pipe.
It's noted that calls to the 'write' function only schedule writes, rather than actually performing a write. If you want to chain writes together, you're going to have to do that manually with the version of the write function that takes a callback as a second parameter.
Thanks for the clarification. I had been reading that documentation, but I did not find it very clear that I could not make multiple calls to write (without a callback). This is made especially confusing by the fact that the documentation shows an example use case with multiple calls to write. The behavior that I had expected was the calling write would append the new data to the existing list of buffers queued to be written out async.
In my specific use case I did not need to perform a long running action, but was instead trying to write data when I received a callback from another API (always on the same thread). Specifically I was attempting to implements a server that generated an MJPEG stream where I got a callback for each frame of the image.
Hmmm... you're right @mitchellwills -- I think it does queue up the writes, but it doesn't have queue admission control. I don't think it even handles errors gracefully. 😦 Let me look into this again. My memory is a little fuzzy here.
Activity
deanberris commentedon Jul 8, 2013
Thanks @mb1986 -- I think you're running into something really weird here. Can you try building this in Debug mode and posting a stack-trace of where the exception is being thrown? I suspect it's a race condition of sorts, or even just because your headers are empty. Did you intend to keep your headers empty?
mitchellwills commentedon Oct 9, 2014
Was this ever resolved? I am running into a similar issue. I'm calling writing a bunch in a row. A stack track is below. This is running on the latest version in the Ubuntu 14.04 repo, 0.11.
#0 0x00007ffff6293bb9 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1 0x00007ffff6296fc8 in __GI_abort () at abort.c:89
#2 0x00007ffff68996b5 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff6897836 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff6897863 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff6897aa2 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00000000004fd76e in void boost::throw_exceptionboost::system::system_error(boost::system::system_error const&) ()
#7 0x000000000050f515 in void boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler>::write_vec_impl<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer >, boost::function<void (boost::system::error_code)> >(std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > >) ()
#8 0x00000000005298b6 in void boost::mfi::mf4<void, boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler>, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > >::call<boost::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler> >, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const, boost::function<void (boost::system::error_code)> const, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > >(boost::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler> >&, void const, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >&, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > >&) const ()
#9 0x0000000000527ece in void boost::_mfi::mf4<void, boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler>, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > >::operator()<boost::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler> > >(boost::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler> >&, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > >) const ()
#10 0x0000000000525e42 in void boost::_bi::list5<boost::_bi::value<boost::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler> > >, boost::_bi::value<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > >, boost::_bi::value<boost::function<void (boost::system::error_code)> >, boost::_bi::value<boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > > >, boost::_bi::value<boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > > >::operator()<boost::_mfi::mf4<void, boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler>, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > >, boost::_bi::list0>(boost::_bi::type, boost::_mfi::mf4<void, boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler>, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > >&, boost::_bi::list0&, int) ()
#11 0x0000000000522f19 in boost::_bi::bind_t<void, boost::_mfi::mf4<void, boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler>, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > >, boost::_bi::list5<boost::_bi::value<boost::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler> > >, boost::_bi::value<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > >, boost::_bi::value<boost::function<void (boost::system::error_code)> >, boost::_bi::value<boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > > >, boost::_bi::value<boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > > > >::operator()() ()
#12 0x000000000051f1ba in boost::detail::function::void_function_obj_invoker0<boost::_bi::bind_t<void, boost::_mfi::mf4<void, boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler>, std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > const&, boost::function<void (boost::system::error_code)> const&, boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > >, boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > >, boost::_bi::list5<boost::_bi::value<boost::shared_ptr<boost::network::http::async_connection<boost::network::http::tags::http_async_server, mjpeg_server::MJPEGServerRequestHandler> > >, boost::_bi::value<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > >, boost::_bi::value<boost::function<void (boost::system::error_code)> >, boost::bi::value<boost::shared_ptr<std::list<boost::shared_ptr<boost::array<char, 1024ul> >, std::allocator<boost::shared_ptr<boost::array<char, 1024ul> > > > > >, boost::bi::value<boost::shared_ptr<std::vector<boost::asio::const_buffer, std::allocatorboost::asio::const_buffer > > > > >, void>::invoke(boost::detail::function::function_buffer&) ()
#13 0x00000000004fc8ca in boost::function0::operator()() const ()
#14 0x0000000000527832 in void boost::asio::asio_handler_invoke<boost::function<void ()> >(boost::function<void ()>, ...) ()
#15 0x000000000052561f in void boost_asio_handler_invoke_helpers::invoke<boost::function<void ()>, boost::function<void ()> >(boost::function<void ()>&, boost::function<void ()>&) ()
#16 0x00000000005227ab in boost::asio::detail::completion_handler<boost::function<void ()> >::do_complete(boost::asio::detail::task_io_service, boost::asio::detail::task_io_service_operation, boost::system::error_code const&, unsigned long) ()
#17 0x00000000004f4e50 in boost::asio::detail::task_io_service_operation::complete(boost::asio::detail::task_io_service&, boost::system::error_code const&, unsigned long) ()
#18 0x00000000004f75d5 in boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lockboost::asio::detail::posix_mutex&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) ()
#19 0x00000000004f70bd in boost::asio::detail::task_io_service::run(boost::system::error_code&) ()
#20 0x00000000004f799d in boost::asio::io_service::run() ()
#21 0x00000000005335ca in unsigned long boost::mfi::mf0<unsigned long, boost::asio::io_service>::callboost::shared_ptr<boost::asio::io_service >(boost::shared_ptrboost::asio::io_service&, void const) const ()
#22 0x0000000000533365 in unsigned long boost::_mfi::mf0<unsigned long, boost::asio::io_service>::operator()boost::shared_ptr<boost::asio::io_service >(boost::shared_ptrboost::asio::io_service&) const ()
#23 0x0000000000533101 in unsigned long boost::_bi::list1boost::_bi::value<boost::shared_ptr<boost::asio::io_service > >::operator()<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list0>(boost::_bi::type, boost::_mfi::mf0<unsigned long, boost::asio::io_service>&, boost::_bi::list0&, long) ()
#24 0x000000000053249b in boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1boost::_bi::value<boost::shared_ptr<boost::asio::io_service > > >::operator()() ()
#25 0x0000000000531398 in boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1boost::_bi::value<boost::shared_ptr<boost::asio::io_service > > > >::run() ()
#26 0x00007ffff71a9a4a in ?? () from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.54.0
#27 0x00007ffff6f88182 in start_thread (arg=0x7fffe67fc700) at pthread_create.c:312
#28 0x00007ffff6357fbd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111
mitchellwills commentedon Oct 12, 2014
After examining the source it looks like an async_connection does not support multiple writes. After the first write completes (in another thread) the error_encountered variable is set. This causes any subsequent call to write to fail with this error. If the variable is only set if the status code is not success then it still fails; after the headers are sent all pending write operations are executed concurrently and subsequent writes eventually fail with Broken Pipe.
deanberris commentedon Oct 13, 2014
@mitchellwills Yes, it doesn't support multiple writes because that would be a race condition. It's properly documented here:
http://cpp-netlib.org/0.11.0/reference/http_server.html#asynchronous-servers
In particular:
http://cpp-netlib.org/0.11.0/reference/http_server.html#connection-object
It's noted that calls to the 'write' function only schedule writes, rather than actually performing a write. If you want to chain writes together, you're going to have to do that manually with the version of the write function that takes a callback as a second parameter.
mitchellwills commentedon Oct 13, 2014
Thanks for the clarification. I had been reading that documentation, but I did not find it very clear that I could not make multiple calls to write (without a callback). This is made especially confusing by the fact that the documentation shows an example use case with multiple calls to write. The behavior that I had expected was the calling write would append the new data to the existing list of buffers queued to be written out async.
In my specific use case I did not need to perform a long running action, but was instead trying to write data when I received a callback from another API (always on the same thread). Specifically I was attempting to implements a server that generated an MJPEG stream where I got a callback for each frame of the image.
deanberris commentedon Oct 13, 2014
Hmmm... you're right @mitchellwills -- I think it does queue up the writes, but it doesn't have queue admission control. I don't think it even handles errors gracefully. 😦 Let me look into this again. My memory is a little fuzzy here.
Thanks for the explanation.