Skip to content

Commit 4247a65

Browse files
committed
fix #1589 premature disposal of request state when using async which caused the end result not being traced
1 parent da5a009 commit 4247a65

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

Diff for: src/Elasticsearch.Net/Connection/RequestState/TransportRequestState.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,15 @@ public void SetResult(ElasticsearchResponse<T> result)
205205
this._result = result;
206206

207207
}
208-
208+
209+
public bool IsDisposed { get; private set; }
209210
public void Dispose()
210211
{
211-
if (!_traceEnabled || this._result == null)
212+
if (IsDisposed || !_traceEnabled || this._result == null)
212213
return;
213214

215+
this.IsDisposed = true;
216+
214217
string traceMessage = string.Format(
215218
"NEST end:{0} {1} {2} ({3}):\r\n{4}"
216219
, this._requestId

Diff for: src/Elasticsearch.Net/Connection/Transport.cs

+10-12
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,18 @@ public ElasticsearchResponse<T> DoRequest<T>(string method, string path, object
346346

347347
public Task<ElasticsearchResponse<T>> DoRequestAsync<T>(string method, string path, object data = null, IRequestParameters requestParameters = null)
348348
{
349-
using (var requestState = new TransportRequestState<T>(this.Settings, requestParameters, method, path))
350-
{
351-
return this._requestHandlerAsync.RequestAsync(requestState, data)
352-
.ContinueWith<ElasticsearchResponse<T>>(t =>
349+
var requestState = new TransportRequestState<T>(this.Settings, requestParameters, method, path);
350+
return this._requestHandlerAsync.RequestAsync(requestState, data)
351+
.ContinueWith<ElasticsearchResponse<T>>(t =>
352+
{
353+
requestState.Dispose();
354+
if (t.IsFaulted && t.Exception != null)
353355
{
354-
if (t.IsFaulted && t.Exception != null)
355-
{
356-
t.Exception.Flatten().InnerException.RethrowKeepingStackTrace();
357-
return null; //won't be hit
356+
t.Exception.Flatten().InnerException.RethrowKeepingStackTrace();
357+
return null; //won't be hit
358358
}
359-
360-
return t.Result;
361-
});
362-
}
359+
return t.Result;
360+
});
363361
}
364362
}
365363
}

Diff for: src/Tests/Nest.Tests.Unit/Settings/UseTraceWriterTests.cs

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Text;
4+
using System.Threading.Tasks;
45
using Elasticsearch.Net;
56
using Elasticsearch.Net.Connection;
67
using FluentAssertions;
@@ -29,5 +30,24 @@ public void EnableTraceShouldWriteToPassedTextWriter()
2930
lines[0].Should().StartWith("NEST start:");
3031
lines[1].Should().StartWith("NEST end:");
3132
}
33+
34+
[Test]
35+
public async Task EnableTraceShouldWriteToPassedTextWriterAsync()
36+
{
37+
var stringBuilder = new StringBuilder();
38+
using (var writer = new StringWriter(stringBuilder))
39+
{
40+
var settings = new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex)
41+
.EnableTrace(true, writer);
42+
var connection = new InMemoryConnection(settings);
43+
var client = new ElasticClient(settings, connection);
44+
45+
var r = await client.ClusterHealthAsync(h => h.Level(Level.Cluster));
46+
}
47+
48+
var lines = stringBuilder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
49+
lines[0].Should().StartWith("NEST start:");
50+
lines[1].Should().StartWith("NEST end:");
51+
}
3252
}
3353
}

0 commit comments

Comments
 (0)