# .Net client socket error after many inserts

**URL:** <https://discuss.elastic.co/t/net-client-socket-error-after-many-inserts/304088>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [May 6, 2022, 6:01am UTC](https://discuss.elastic.co/t/net-client-socket-error-after-many-inserts/304088 "2022-05-06T06:01:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![r03](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r03/32/105312_2.png) [@r03](https://discuss.elastic.co/u/r03)\
**Post date:** [May 6, 2022, 6:01am UTC](https://discuss.elastic.co/t/net-client-socket-error-after-many-inserts/304088/1 "2022-05-06T06:01:11Z")

</div>

I'm trying to create documents in Elastic with the .Net client. (7.17.1 on server 8.1.3)  
This works, but on the first sync I have many documents and after a lot of successul inserts the elastic client stops working.

I create the client like this only once:

```auto
var node = new Uri(config.ElasticHost);
var pool = new SingleNodeConnectionPool(node);
var elasticSettings = new ConnectionSettings(pool)
    .CertificateFingerprint(config.ElasticFingerprint)
    .BasicAuthentication(config.ElasticUser, config.ElasticPassword)
    .DefaultMappingFor...
    .EnableDebugMode()
    .PrettyJson();
_elasticClient = new ElasticClient(elasticSettings);

```

Then for each new document I use this:

```auto
private async Task<bool> UpdateOrInsert<T>(T item) where T : ElasticDocument
{
    
    var response = await _elasticClient.IndexDocumentAsync<T>(item);
    if (!response.IsValid)
    {
        _logger.LogError(response.OriginalException.Message);
        _logger.LogError(response.DebugInformation);
        return false;
    }
    return true;
}

```

After a while I get the error below.  
What could cause this? The server is not broken after this. I can just connect again.  
Seems like the client stops working after a while?

```auto
Invalid NEST response built from a unsuccessful () low level call on PUT: /productlink-dev/_doc/LC-7-1085-DUMMY_1201G6302000_ST30?pretty=true&error_trace=true
# Audit trail of this API call:
 - [1] BadRequest: Node: https://xx.xx.xx.xx:9200/ Took: 00:00:19.2788193
# OriginalException: System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: Unable to read data from the transport connection: 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..
 ---> System.Net.Sockets.SocketException (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.
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource<System.Int32>.GetResult(Int16 token)
   at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](TIOAdapter adapter)
   at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory`1 buffer)
   at System.Net.Http.HttpConnection.InitialFillAsync(Boolean async)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
# Request:
{stripped}
# Response:

# TCP states:
  Established: 323
  TimeWait: 7
  CloseWait: 1

# ThreadPool statistics:
  Worker:
    Busy: 1
    Free: 32766
    Min: 16
    Max: 32767
  IOCP:
    Busy: 1
    Free: 999
    Min: 16
    Max: 1000

```

**Update:**  
I tried with BulkAll but now I get Invalid documents without an error.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/c/dca70997277cdc5ac2c8e35bb045a57cca9b22a5.png)

---

<div class="post-metadata">

**Author:** ![r03](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r03/32/105312_2.png) [@r03](https://discuss.elastic.co/u/r03)\
**Post date:** [May 10, 2022, 8:47am UTC](https://discuss.elastic.co/t/net-client-socket-error-after-many-inserts/304088/2 "2022-05-10T08:47:54Z")

</div>

The BulkAll method works after adding `EnableApiVersioningHeader`.

But after a while I get this error:

```auto
 BulkAll halted after PipelineFailure.BadRequest from _bulk and exhausting retries (2)
Elastic.Transport.TransportException: BulkAll halted after PipelineFailure.BadRequest from _bulk and exhausting retries (2)
   at Nest.BlockingSubscribeExtensions.WaitOnObservable[TObservable,TObserve,TObserver](TObservable observable, TimeSpan maximumRunTime, Func`3 factory)
   at Nest.BlockingSubscribeExtensions.Wait[T](BulkAllObservable`1 observable, TimeSpan maximumRunTime, Action`1 onNext)

```

I tried recreating the elastic client on each run but that makes it worse. It seems this client is not yet ready for Elastic 8. I tried the alpha version but get the same errors.

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [May 10, 2022, 2:24pm UTC](https://discuss.elastic.co/t/net-client-socket-error-after-many-inserts/304088/3 "2022-05-10T14:24:23Z")

</div>

Hi @r03. Based on the exceptions which are down in the .NET networking layer, this appears to be a networking issue preventing some of the requests succeeding. How many documents succeed before failure? I've been testing with millions of documents, against an Elastic Cloud 8.x deployment with no issues from 7.17 client or 8.0 alpha. How are you running ES? You may need to enable some logging on the server to verify if the requests are received or any failures are logged there.

---

<div class="post-metadata">

**Author:** ![r03](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/r03/32/105312_2.png) [@r03](https://discuss.elastic.co/u/r03)\
**Post date:** [May 10, 2022, 3:47pm UTC](https://discuss.elastic.co/t/net-client-socket-error-after-many-inserts/304088/4 "2022-05-10T15:47:35Z")

</div>

Yes, it seems to work fine when running the same elastic server version locally.  
I guess there must be an error on the https level somewhere.

![image](https://us1.discourse-cdn.com/elastic/original/3X/9/9/99381c14ccb58b8f94719ce6389c92a69bced83c.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 7, 2022, 3:47pm UTC](https://discuss.elastic.co/t/net-client-socket-error-after-many-inserts/304088/5 "2022-06-07T15:47:52Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
