Boost(Beast)Websocket:同步写入挂起

时间:2018-09-23 22:35:08

标签: c++ boost boost-asio boost-beast

我遇到了增强野兽websocket流的问题。当我尝试写入远程端点已停止响应的流时,会间歇性地出现此问题(特别是由于远程物理断开了与网络的连接)。

发生此问题时,同步stream.write()调用最终会挂起很长时间(几分钟),直到套接字最终关闭。我知道这种现象很可能是因为我的程序继续写入流中而没有来自远程的确认,直到发送缓冲区已满。我想知道是否有一种方法可以将超时应用于写调用,或者是否有一个接口可以像stream.try_write()那样将错误处理提升到用户级别。

我确实意识到一种选择是使用async_write接口。但是,我担心这会延迟套接字写操作直到io_context的下一次迭代,从而对我的发送性能产生负面影响。

以下是调用挂起时线程的堆栈跟踪。

#0  0x00007f468cf33624 in poll () from /lib64/libc.so.6
#1  0x000000000043e5a7 in boost::asio::detail::socket_ops::poll_write (ec=..., msec=-1, state=0 '\000', s=16)
    at /usr/include/boost/asio/detail/impl/socket_ops.ipp:1898
#2  boost::asio::detail::socket_ops::sync_send (ec=..., all_empty=<optimized out>, flags=0, count=<optimized out>, bufs=0x7fff43c17e20, 
    state=<optimized out>, s=<optimized out>) at /usr/include/boost/asio/detail/impl/socket_ops.ipp:1224
#3  boost::asio::detail::reactive_socket_service_base::send<boost::asio::detail::prepared_buffers<boost::asio::const_buffer, 64ul> > (impl=..., 
    buffers=..., ec=..., this=<optimized out>, flags=0) at /usr/include/boost/asio/detail/reactive_socket_service_base.hpp:245
#4  0x0000000000481c71 in boost::asio::basic_stream_socket<boost::asio::ip::tcp>::write_some<boost::asio::detail::prepared_buffers<boost::asio::const_buffer, 64ul> > (ec=..., buffers=..., this=0x108ad50) at /usr/include/boost/asio/buffer.hpp:941
#5  boost::asio::detail::write_buffer_sequence<boost::asio::basic_stream_socket<boost::asio::ip::tcp>, boost::beast::buffers_cat_view<boost::asio::mutable_buffer, boost::beast::buffers_prefix_view<boost::beast::buffers_suffix<boost::beast::basic_multi_buffer<std::allocator<char> >::const_buffers_type> > >, boost::beast::buffers_cat_view<boost::asio::mutable_buffer, boost::beast::buffers_prefix_view<boost::beast::buffers_suffix<boost::beast::basic_multi_buffer<std::allocator<char> >::const_buffers_type> > >::const_iterator, boost::asio::detail::transfer_all_t> (completion_condition=..., ec=..., buffers=..., s=...)
    at /usr/include/boost/asio/impl/write.hpp:53
#6  boost::asio::write<boost::asio::basic_stream_socket<boost::asio::ip::tcp>, boost::beast::buffers_cat_view<boost::asio::mutable_buffer, boost::beast::buffers_prefix_view<boost::beast::buffers_suffix<boost::beast::basic_multi_buffer<std::allocator<char> >::const_buffers_type> > >, boost::asio::detail::transfer_all_t> (ec=..., buffers=..., s=..., completion_condition=...) at /usr/include/boost/asio/impl/write.hpp:69
#7  boost::asio::write<boost::asio::basic_stream_socket<boost::asio::ip::tcp>, boost::beast::buffers_cat_view<boost::asio::mutable_buffer, boost::beast::buffers_prefix_view<boost::beast::buffers_suffix<boost::beast::basic_multi_buffer<std::allocator<char> >::const_buffers_type> > > > (ec=..., buffers=..., s=...)
    at /usr/include/boost/asio/impl/write.hpp:92
#8  boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >::write_some<boost::beast::basic_multi_buffer<std::allocator<char> >::const_buffers_type> (this=this@entry=0x108ad50, fin=fin@entry=true, buffers=..., ec=...) at /usr/include/boost/beast/websocket/impl/write.ipp:625
#9  0x000000000042c5e1 in boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >::write<boost::beast::basic_multi_buffer<std::allocator<char> >::const_buffers_type> (ec=..., buffers=..., this=0x108ad50)

2 个答案:

答案 0 :(得分:0)

Beast websockets不支持非阻止模式。如果在与Websocket流一起使用的套接字上设置此模式,则在某些情况下websocket::stream的实现将产生未定义的行为。对于同步代码,缺少超时是一个普遍的问题。除了使用异步操作外,您别无选择。您说过要直接从堆栈发送缓冲区,这可以通过使用协程在异步上下文中轻松完成(请参见boost::asio::spawnboost::asio::yield_context)。

有很多技术可以使异步I / O执行与同步I / O相同或更好的性能,包括在所需延迟很短的情况下。

这是Boost.Asio的作者直接提出的关于实现超低延迟的建议:https://groups.google.com/a/isocpp.org/d/msg/sg14/FoLFHXqZSck/i4rdO-O3BQAJ

答案 1 :(得分:-1)

我能够通过将基础流套接字置于非阻塞模式来解决此问题:

socket.non_blocking(true);

在这种模式下,一旦发送缓冲区变满,write()调用将立即返回boost::system::error_code::try_again(又称posix EAGAIN)。