Skip to content

Commit

Permalink
Fixing Issue NLog#1058
Browse files Browse the repository at this point in the history
Deadlock in NetworkTarget Issue  NLog#1058  Fix.
  • Loading branch information
kt1996 committed Dec 25, 2015
1 parent fd705e3 commit f564030
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/NLog/Targets/NetworkTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,12 @@ protected override void Write(AsyncLogEventInfo logEvent)
else
{

NetworkSender sender;
LinkedListNode<NetworkSender> linkedListNode;

lock (this.openNetworkSenders)
{


//handle to many connections
//handle too many connections
var tooManyConnections = this.openNetworkSenders.Count >= MaxConnections;

if (tooManyConnections && MaxConnections > 0)
Expand Down Expand Up @@ -304,33 +305,34 @@ protected override void Write(AsyncLogEventInfo logEvent)
}
}

var sender = this.SenderFactory.Create(address, MaxQueueSize);
sender = this.SenderFactory.Create(address, MaxQueueSize);
sender.Initialize();

var linkedListNode = this.openNetworkSenders.AddLast(sender);
this.ChunkedSend(
sender,
bytes,
ex =>
linkedListNode = this.openNetworkSenders.AddLast(sender);
}
this.ChunkedSend(
sender,
bytes,
ex =>
{
lock (this.openNetworkSenders)
{
lock (this.openNetworkSenders)
TryRemove(this.openNetworkSenders, linkedListNode);
if (this.OnConnectionOverflow == NetworkTargetConnectionsOverflowAction.Block)
{
TryRemove(this.openNetworkSenders, linkedListNode);
if (this.OnConnectionOverflow == NetworkTargetConnectionsOverflowAction.Block)
{
System.Threading.Monitor.PulseAll(this.openNetworkSenders);
}
System.Threading.Monitor.PulseAll(this.openNetworkSenders);
}
}

if (ex != null)
{
InternalLogger.Error("Error when sending {0}", ex);
}
if (ex != null)
{
InternalLogger.Error("Error when sending {0}", ex);
}

sender.Close(ex2 => { });
logEvent.Continuation(ex);
});
}
sender.Close(ex2 => { });
logEvent.Continuation(ex);
});

}
}

Expand Down

0 comments on commit f564030

Please sign in to comment.