11// Copyright The OpenTelemetry Authors
22// SPDX-License-Identifier: Apache-2.0
33
4- #include < stdint.h>
54#include < atomic>
65#include < chrono>
76#include < condition_variable>
7+ #include < cstdint>
88#include < map>
99#include < memory> // IWYU pragma: keep
1010#include < mutex>
@@ -100,21 +100,24 @@ class ResponseHandler : public http_client::EventHandler
100100 << log_message);
101101 }
102102
103- // Set the response_received_ flag to true and notify any threads waiting on this result
104- response_received_ = true ;
103+ // Record the outcome and notify any threads waiting on this result
104+ recordCompletionLocked (CompletionState::Success) ;
105105 }
106106 cv_.notify_all ();
107107 }
108108
109109 /* *
110- * A method the user calls to block their thread until the response is received. The longest
111- * duration is the timeout of the request, set by SetTimeoutMs()
110+ * A method the user calls to block their thread until the request has either produced a
111+ * response or failed. The longest duration is the timeout of the request, set by
112+ * SetTimeoutMs(), which arrives here as a TimedOut session event.
112113 */
113114 bool waitForResponse ()
114115 {
115116 std::unique_lock<std::mutex> lk (mutex_);
116- cv_.wait (lk);
117- return response_received_;
117+ // Waiting on a predicate rather than bare: the completion may already have been recorded
118+ // before this thread got here, in which case there is no notification left to receive.
119+ cv_.wait (lk, [this ] { return completion_ != CompletionState::Pending; });
120+ return completion_ == CompletionState::Success;
118121 }
119122
120123 /* *
@@ -135,20 +138,23 @@ class ResponseHandler : public http_client::EventHandler
135138 {
136139 case http_client::SessionState::CreateFailed:
137140 OTEL_INTERNAL_LOG_ERROR (" [ES Log Exporter] Failed to create session" );
138- cv_. notify_all ( );
141+ recordCompletion (CompletionState::Failure );
139142 break ;
140143 case http_client::SessionState::Created:
141144 OTEL_INTERNAL_LOG_DEBUG (" [ES Log Exporter] Session created" );
142145 break ;
143146 case http_client::SessionState::Destroyed:
144147 OTEL_INTERNAL_LOG_DEBUG (" [ES Log Exporter] Session destroyed" );
148+ // Nothing else will arrive after this. If no outcome was recorded, the session ended
149+ // without a response, so release the waiter rather than leaving it blocked forever.
150+ recordCompletion (CompletionState::Failure);
145151 break ;
146152 case http_client::SessionState::Connecting:
147153 OTEL_INTERNAL_LOG_DEBUG (" [ES Log Exporter] Connecting to peer" );
148154 break ;
149155 case http_client::SessionState::ConnectFailed:
150156 OTEL_INTERNAL_LOG_ERROR (" [ES Log Exporter] Failed to connect to peer" );
151- cv_. notify_all ( );
157+ recordCompletion (CompletionState::Failure );
152158 break ;
153159 case http_client::SessionState::Connected:
154160 OTEL_INTERNAL_LOG_DEBUG (" [ES Log Exporter] Connected to peer" );
@@ -158,22 +164,22 @@ class ResponseHandler : public http_client::EventHandler
158164 break ;
159165 case http_client::SessionState::SendFailed:
160166 OTEL_INTERNAL_LOG_ERROR (" [ES Log Exporter] Failed to send request" );
161- cv_. notify_all ( );
167+ recordCompletion (CompletionState::Failure );
162168 break ;
163169 case http_client::SessionState::Response:
164170 OTEL_INTERNAL_LOG_DEBUG (" [ES Log Exporter] Received response" );
165171 break ;
166172 case http_client::SessionState::SSLHandshakeFailed:
167173 OTEL_INTERNAL_LOG_ERROR (" [ES Log Exporter] Failed SSL Handshake" );
168- cv_. notify_all ( );
174+ recordCompletion (CompletionState::Failure );
169175 break ;
170176 case http_client::SessionState::TimedOut:
171177 OTEL_INTERNAL_LOG_ERROR (" [ES Log Exporter] Request timed out" );
172- cv_. notify_all ( );
178+ recordCompletion (CompletionState::Failure );
173179 break ;
174180 case http_client::SessionState::NetworkError:
175181 OTEL_INTERNAL_LOG_ERROR (" [ES Log Exporter] Network error" );
176- cv_. notify_all ( );
182+ recordCompletion (CompletionState::Failure );
177183 break ;
178184 case http_client::SessionState::ReadError:
179185 OTEL_INTERNAL_LOG_DEBUG (" [ES Log Exporter] Read error" );
@@ -183,18 +189,47 @@ class ResponseHandler : public http_client::EventHandler
183189 break ;
184190 case http_client::SessionState::Cancelled:
185191 OTEL_INTERNAL_LOG_ERROR (" [ES Log Exporter] (manually) cancelled" );
186- cv_. notify_all ( );
192+ recordCompletion (CompletionState::Failure );
187193 break ;
188194 }
189195 }
190196
191197private:
198+ enum class CompletionState : std::uint8_t
199+ {
200+ Pending,
201+ Success,
202+ Failure
203+ };
204+
205+ /* *
206+ * Record the outcome of the request, first writer wins, then release any waiter. Keeping the
207+ * first outcome means a session destroyed after a successful response does not overwrite it.
208+ */
209+ void recordCompletion (CompletionState state)
210+ {
211+ {
212+ std::unique_lock<std::mutex> lk (mutex_);
213+ recordCompletionLocked (state);
214+ }
215+ cv_.notify_all ();
216+ }
217+
218+ // / As recordCompletion(), for callers that already hold mutex_ and notify themselves.
219+ void recordCompletionLocked (CompletionState state)
220+ {
221+ if (completion_ == CompletionState::Pending)
222+ {
223+ completion_ = state;
224+ }
225+ }
226+
192227 // Define a condition variable and mutex
193228 std::condition_variable cv_;
194229 std::mutex mutex_;
195230
196- // Whether the response from Elasticsearch has been received
197- bool response_received_ = false ;
231+ // Whether the request has completed, and how
232+ CompletionState completion_ = CompletionState::Pending ;
198233
199234 // A string to store the response body
200235 std::string body_ = " " ;
0 commit comments