websocketpp icon indicating copy to clipboard operation
websocketpp copied to clipboard

tcp::no_delay leads to Bad file descriptor

Open kpeeters opened this issue 5 years ago • 6 comments

The following program attempts to start a server and set the NO_DELAY option on the socket as soon as it has been initialised. This worked with 0.7, but no longer with 0.8.1. Commenting out the s.set_option(option) line makes it work again.

Am I doing this wrong or is this a bug?

#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/common/functional.hpp>

typedef websocketpp::server<websocketpp::config::asio> WebsocketServer;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;

void on_socket_init(websocketpp::connection_hdl, boost::asio::ip::tcp::socket & s)
	{
	boost::asio::ip::tcp::no_delay option(true);
	s.set_option(option);
	}

int main(int, char **)
	{
	WebsocketServer wserver;

	wserver.init_asio();
	wserver.set_reuse_addr(true);
	wserver.set_socket_init_handler(bind(&on_socket_init, ::_1,::_2));
	wserver.listen(0);
	wserver.start_accept();

	websocketpp::lib::asio::error_code ec;
	auto p = wserver.get_local_endpoint(ec);
	std::cout << p.port()  << std::endl;
	}

kpeeters avatar Apr 20 '19 16:04 kpeeters

I am seeing this issue as well. Was working on websocketpp 0.5.1; upgraded to 0.8.1 and it no longer works

jzablot avatar Jun 07 '19 22:06 jzablot

I am also experiencing the same issue. My application works without errors when using v0.7.0. When I try to use v0.8.1 instead, I get the error. Commenting out the tcp::no_delay line in my code makes it work, but I would like to set the option. Attached is the relevant gdb stack trace, if that's any help:

#0  0x00007ffff66eae97 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007ffff66ec801 in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007ffff6d41957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff6d47ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff6d47af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff6d47d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00005555555871e0 in asio::detail::throw_exception<std::system_error> (e=...) at /usr/include/asio/detail/throw_exception.hpp:42
#7  0x000055555556cd06 in asio::detail::do_throw_error (err=..., location=0x55555563dd77 "set_option") at /usr/include/asio/detail/impl/throw_error.ipp:49
#8  0x000055555556cc39 in asio::detail::throw_error (err=..., location=0x55555563dd77 "set_option") at /usr/include/asio/detail/throw_error.hpp:41
#9  0x000055555558ee7a in asio::basic_socket<asio::ip::tcp, asio::stream_socket_service<asio::ip::tcp> >::set_option<asio::detail::socket_option::boolean<6, 1> > (this=0x555555918ce0, option=...) at /usr/include/asio/basic_socket.hpp:819

mrakh avatar Oct 03 '19 07:10 mrakh

Hi Same here. any solution? -Thanks

rambabuwhy avatar Jul 13 '20 11:07 rambabuwhy

Any workaround for this issue?

luoyongh avatar Aug 07 '20 04:08 luoyongh

This regression from 0.8.0 has been fixed on the develop branch. The socket_init event handler is the correct place to set socket options like TCP_NODELAY on the already accepted socket.

zaphoyd avatar Aug 07 '20 20:08 zaphoyd

Hi, why don't you merge the fix into main branch?

sweihub avatar Jan 29 '24 07:01 sweihub