Javascript.NodeJS icon indicating copy to clipboard operation
Javascript.NodeJS copied to clipboard

SocketException - An existing connection was forcibly closed by the remote host

Open kelmelzer opened this issue 6 months ago • 1 comments

Here is my exception plus innerException in my code that we're only seeing on one Windows server:

2023-12-18 12:00:25.5390|0|ERROR|XXXXXXXX|System.Net.Http.HttpRequestException: An error occurred while sending the request. System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
 ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
   --- 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.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.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.<SendAsync>g__Core|5_0(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendAsync>g__Core|5_0(HttpRequestMessage request, 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 Jering.Javascript.NodeJS.HttpNodeJSService.TryInvokeAsync[T](InvocationRequest invocationRequest, CancellationToken cancellationToken)
   at Jering.Javascript.NodeJS.OutOfProcessNodeJSService.TryInvokeCoreAsync[T](InvocationRequest invocationRequest, CancellationToken cancellationToken)
   at Jering.Javascript.NodeJS.OutOfProcessNodeJSService.InvokeFromStringAsync[T](Func`1 moduleFactory, String cacheIdentifier, String exportName, Object[] args, CancellationToken cancellationToken)

Details:

  • Node v20.8 is installed on the server (Server 2016) and works fine running the same code on other servers as well as my local machine.
  • Using .NET 8.0 but I also tried .NET 7.0 again just to be certain but received same exceptions.
  • The nodejs module I am running is the Newman package (for Postman API test collections) and has never had this issue
  • I have tried this with Concurrency.MultiProcess (my normal config) as well as Concurrency.None (default setting)

Code snippet of my call:

static string moduleFactory() => @"
module.exports = async (collectPath, reportPath, htmlReportPath, reportName) => {
    const newman = require('newman');

    return new Promise((resolve, reject) => {
        newman.run({
            collection: require(collectPath),
            reporters: ['junit','htmlextra'],
            reporter: { 
                junit: {
                    export: reportPath
                },
                htmlextra: {
                    export: htmlReportPath,
                    browserTitle: reportName,
                    title: reportName,
                    showGlobalData: true,
                    showMarkdownLinks: true
                }
            },
            color: 'off',
            insecure: true
        }, function() {
            console.log('in callback');
        }).on('start', function (err, args) {
            if (err) { console.log(err); }
        }).on('beforeDone', function (err, data) {
            if (err) { console.log(err); }
        }).on('done', function (err, summary) {
            if (err) { reject(err); } else { resolve(summary); }
        })     
    })
};";

var newmanSummaryStr = await StaticNodeJSService.InvokeFromStringAsync<string>(moduleFactory, "newmanModule", args: new object[] { tempPostmanCollectionPath.Replace('\\', '/'), tempReportPath.Replace('\\', '/'), tempHtmlReportPath.Replace('\\', '/'), $"{runId} - {scenarioName} - {componentName} - {stepName}".Replace('\\', '/') });

kelmelzer avatar Dec 18 '23 17:12 kelmelzer

I fixed this issue. It was an odd issue with some npm dependencies missing that presented itself with this error. I wasn't able to discover that until switching to use the INodeJSService DI implementation instead of the static one since the DI one uses ILogger. Can we add logging to the static methods?

kelmelzer avatar Dec 26 '23 14:12 kelmelzer