Skip to content

Commit 5e6bc69

Browse files
ooplesclaude
andcommitted
fix: add configureawait false to reduce deadlock risk in websearchtool
Added ConfigureAwait(false) to all await calls in SearchBingAsync and SearchSerpAPIAsync methods to reduce deadlock risk when these async methods are called synchronously via GetAwaiter().GetResult() in the Execute method. This follows async best practices for library code and mitigates issues with blocking async continuations in synchronization contexts. Fixes PR #423 comment #6. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 68ee97f commit 5e6bc69

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/Tools/WebSearchTool.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ private async Task<string> SearchBingAsync(string query)
158158
var request = new HttpRequestMessage(HttpMethod.Get, url);
159159
request.Headers.Add("Ocp-Apim-Subscription-Key", _apiKey);
160160

161-
var response = await _httpClient.SendAsync(request);
161+
var response = await _httpClient.SendAsync(request).ConfigureAwait(false);
162162

163163
if (!response.IsSuccessStatusCode)
164164
{
165-
var errorContent = await response.Content.ReadAsStringAsync();
165+
var errorContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
166166
throw new HttpRequestException($"Bing Search API error ({response.StatusCode}): {errorContent}");
167167
}
168168

169-
var content = await response.Content.ReadAsStringAsync();
169+
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
170170
var searchResponse = JsonConvert.DeserializeObject<BingSearchResponse>(content);
171171

172172
if (searchResponse?.WebPages?.Value == null || searchResponse.WebPages.Value.Length == 0)
@@ -184,15 +184,15 @@ private async Task<string> SearchSerpAPIAsync(string query)
184184
{
185185
var url = $"https://serpapi.com/search.json?q={Uri.EscapeDataString(query)}&num={_defaultResultCount}&api_key={_apiKey}&engine=google";
186186

187-
var response = await _httpClient.GetAsync(url);
187+
var response = await _httpClient.GetAsync(url).ConfigureAwait(false);
188188

189189
if (!response.IsSuccessStatusCode)
190190
{
191-
var errorContent = await response.Content.ReadAsStringAsync();
191+
var errorContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
192192
throw new HttpRequestException($"SerpAPI error ({response.StatusCode}): {errorContent}");
193193
}
194194

195-
var content = await response.Content.ReadAsStringAsync();
195+
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
196196
var searchResponse = JsonConvert.DeserializeObject<SerpAPIResponse>(content);
197197

198198
if (searchResponse?.OrganicResults == null || searchResponse.OrganicResults.Length == 0)

0 commit comments

Comments
 (0)