Opened 10 years ago
Closed 10 years ago
#7392 closed Bugs (invalid)
boost::asio::ip::tcp::socket::is_open returns true even if socket is unusable
| Reported by: | Owned by: | chris_kohlhoff | |
|---|---|---|---|
| Milestone: | To Be Determined | Component: | asio |
| Version: | Boost 1.49.0 | Severity: | Problem |
| Keywords: | Cc: |
Description
socket.connect() failed with error code 10060:
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond".
Subsequent call to socket::is_open() method returns true (which is bad). Subsequent call to boost::asio::write(socket, ...) returns error code 10057 ("A request to send or receive data was disallowed because the socket is not connected and (w hen sending on a datagram socket using a sendto call) no address was supplied") (which is OK).
Change History (4)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Note: In reactive_socket_service_base.hpp Determine whether the socket is open.
bool is_open(const base_implementation_type& impl) const {
return impl.socket_ != invalid_socket;
}
impl.socket_ is "8" in my case
The issue is also present in 1.50 (the latest one in debian)
comment:3 by , 10 years ago
| Version: | Boost 1.51.0 → Boost 1.49.0 |
|---|
comment:4 by , 10 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
A socket is not automatically closed when the connect fails, hence is_open still returns true. This is the intended (and documented) behaviour. You need to check the error code produced by the operation and act on it accordingly.

I also see that behaviour with boost 1.49.0.1 (stock Debian package), but in my case the error from the call to connect failed with error 111: Connection refused
Subsequent call to is_open returns != 0
1902561 [0x7ffff7fc3760] ERROR inverter.Shared_Comms_Inverter1 null - Error while connecting: (111) Connection refused 1902561 [0x7ffff7fc3760] DEBUG inverter.Shared_Comms_Inverter1 null - new state: CMD_DISCONNECTED 1902561 [0x7ffff4d3b700] DEBUG inverter.Shared_Comms_Inverter1.Comms_SharedConnection..Comms_TCP_ASIO null - Already connected