mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-18 19:40:57 +00:00
* Let ClientImpl::stop() end a connect or a TLS handshake stop() took socket_mutex_, which send_() and open_stream() hold while they open the connection, so a stop() made during a connect, a TLS handshake or the CONNECT of a proxy tunnel waited for the connection to open or to time out, 300 seconds by default, for a server that never answers. stop() no longer waits for the mutex while a connection opens: it cancels the connection phase by a compare-exchange from Connecting, and the end of the phase reads the cancel in the same exchange that takes the state back to Idle, so a cancel stop() accepts is never lost. stop() also sets stop_requested_: the wait of the connect and the waits of the handshake, in every TLS backend, poll it in slices of 100 ms, as do the reads of the CONNECT of a proxy tunnel. A request or a stream canceled there reports Error::Canceled, before it sends anything or takes the socket. A request opened after stop() clears the flag as before, and a stream now does too. A negative connection timeout still waits without end in the interruptible connect wait. * Add ClientImpl::set_interrupt(), a stop the caller holds stop() ends what is in flight, and a request or a stream opened after it clears the flag, so a stop() made just before a request goes out is lost. set_interrupt() gives the client a predicate its caller owns, polled wherever stop_requested_ is: the connect, the TLS handshake, the CONNECT of a proxy tunnel and the reads of a request, and the head of a stream too. The client of a cross-host redirect keeps it. A request or a stream refuses to go out once it holds, so a stop the caller holds is never lost, whenever it comes. A call blocked in the system, a send() of a large body or a TLS read inside a record, still ends through stop() alone, which shuts the socket down. A timeout past what the clock reaches now waits without end instead of expiring at once, so such a wait still ends on the interrupt. * test: create the backlog sockets with SOCK_NONBLOCK detail::set_nonblocking lives in httplib.cc in the split build, so test_split did not compile. The test only runs on Linux, where SOCK_NONBLOCK makes the socket nonblocking at creation. * Open the connection without socket_mutex_ instead Rework the change so that send_() and open_stream_() open the connection without holding socket_mutex_, as requests are already serialized by request_mutex_. stop() then never waits for a connection being opened: it always sets stop_requested_, which the connect wait, the TLS handshake, the CONNECT and the wait for the head of a stream poll, and the call ends with Error::Canceled before it sends anything. This replaces the connection state machine and the try_lock loop in stop(). The flag is cleared once, as a send() or an open_stream() starts, so a retry inside the call no longer drops a stop() it already got. Drop set_interrupt() for now: a predicate held per client cannot be shared, e.g. by an SSEClient that borrows the caller's Client. Also drop StopEndsATlsReadInsideARecord, which checked existing behavior that does not hold on Windows, and share the listener setup of the tests. Keep the deadline_after() fix: a timeout past what the clock reaches waits forever instead of overflowing into an immediate timeout. --------- Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>