Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Ci fixes #344

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Microsoft.AspNet.Server.Kestrel/Http/ListenerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ public class ListenerContext : ServiceContext
{
public ListenerContext()
{
Memory2 = new MemoryPool2();
}

public ListenerContext(ServiceContext serviceContext)
: base(serviceContext)
{
Memory2 = new MemoryPool2();
}

public ListenerContext(ListenerContext listenerContext)
Expand All @@ -25,7 +23,6 @@ public ListenerContext(ListenerContext listenerContext)
ServerAddress = listenerContext.ServerAddress;
Thread = listenerContext.Thread;
Application = listenerContext.Application;
Memory2 = listenerContext.Memory2;
Log = listenerContext.Log;
}

Expand All @@ -35,6 +32,6 @@ public ListenerContext(ListenerContext listenerContext)

public RequestDelegate Application { get; set; }

public MemoryPool2 Memory2 { get; set; }
public MemoryPool2 Memory2 => Thread.Memory2;
}
}
24 changes: 23 additions & 1 deletion src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ public void End(ProduceEndType endType)

private void ScheduleWrite()
{
_thread.Post(_this => _this.WriteAllPending(), this);
// Don't post write to closed socket
if (!_socket.IsClosed)
{
_thread.Post(_this => _this.WriteAllPending(), this);
}
else
{
CompleteAllCallbacksWithError(_lastWriteError ?? new InvalidOperationException("Socket is closed"));
}
}

// This is called on the libuv event loop
Expand Down Expand Up @@ -173,6 +181,20 @@ private void WriteAllPending()
throw;
}
}

private void CompleteAllCallbacksWithError(Exception error)
{
lock (_lockObj)
{
while (_callbacksPending.Count > 0)
{
var callbackContext = _callbacksPending.Dequeue();

// callback(error, state, calledInline)
callbackContext.Callback(error, callbackContext.State, false);
}
}
}

// This is called on the libuv event loop
private void OnWriteCompleted(Queue<ArraySegment<byte>> writtenBuffers, int status, Exception error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class KestrelThread
public KestrelThread(KestrelEngine engine)
{
_engine = engine;
Memory2 = new MemoryPool2();
_appLifetime = engine.AppLifetime;
_log = engine.Log;
_loop = new UvLoopHandle(_log);
Expand All @@ -46,6 +47,9 @@ public KestrelThread(KestrelEngine engine)
}

public UvLoopHandle Loop { get { return _loop; } }

public MemoryPool2 Memory2 { get; }

public ExceptionDispatchInfo FatalError { get { return _closeError; } }

public Action<Action<IntPtr>, IntPtr> QueueCloseHandle { get; internal set; }
Expand All @@ -61,6 +65,7 @@ public void Stop(TimeSpan timeout)
{
if (!_initCompleted)
{
Memory2.Dispose();
return;
}

Expand All @@ -79,6 +84,9 @@ public void Stop(TimeSpan timeout)
}
}
}

Memory2.Dispose();

if (_closeError != null)
{
_closeError.Throw();
Expand Down
46 changes: 19 additions & 27 deletions src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPool2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public class MemoryPool2 : IDisposable
/// </summary>
private readonly ConcurrentStack<MemoryPoolSlab2> _slabs = new ConcurrentStack<MemoryPoolSlab2>();

/// <summary>
/// This is part of implementing the IDisposable pattern.
/// </summary>
private bool _disposedValue = false; // To detect redundant calls

/// <summary>
/// Called to take a block from the pool.
/// </summary>
Expand Down Expand Up @@ -137,39 +132,36 @@ public void Return(MemoryPoolBlock2 block)

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
MemoryPoolSlab2 slab;
while (_slabs.TryPop(out slab))
{
if (disposing)
{
MemoryPoolSlab2 slab;
while (_slabs.TryPop(out slab))
{
// dispose managed state (managed objects).
slab.Dispose();
}
}

// N/A: free unmanaged resources (unmanaged objects) and override a finalizer below.

// N/A: set large fields to null.
// Free pinned objects
slab.Dispose();
}

_disposedValue = true;
MemoryPoolBlock2 block;
while (_blocks.TryPop(out block))
{
// Deactivate finalizers
block.Dispose();
}

}

// N/A: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~MemoryPool2() {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// Disposing slabs unpin memory so finalizer is needed.
~MemoryPool2()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}

// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// N/A: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);

GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,10 @@ public MemoryPoolIterator2 GetIterator()
{
return new MemoryPoolIterator2(this);
}

internal void Dispose()
{
GC.SuppressFinalize(this);
}
}
}
4 changes: 4 additions & 0 deletions src/Microsoft.AspNet.Server.Kestrel/Networking/Libuv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Microsoft.AspNet.Server.Kestrel.Networking
Expand Down Expand Up @@ -199,6 +200,9 @@ public void async_init(UvLoopHandle loop, UvAsyncHandle handle, uv_async_cb cb)
protected Func<UvAsyncHandle, int> _uv_async_send;
public void async_send(UvAsyncHandle handle)
{
// Can't Assert with .Validate as that checks threadId
// and this function is to post to correct thread.
Debug.Assert(!handle.IsInvalid, "Handle is invalid");
Check(_uv_async_send(handle));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ public async Task GetDateHeaderValue_ReturnsUpdatedValueAfterIdle()
{
result1 = dateHeaderValueManager.GetDateHeaderValue();
systemClock.UtcNow = future;
// Wait for twice the idle timeout to ensure the timer is stopped
await Task.Delay(timeWithoutRequestsUntilIdle.Add(timeWithoutRequestsUntilIdle));
// Wait for 3 times the idle timeout to ensure the timer is stopped
await Task.Delay(
timeWithoutRequestsUntilIdle
.Add(timeWithoutRequestsUntilIdle)
.Add(timeWithoutRequestsUntilIdle)
);
result2 = dateHeaderValueManager.GetDateHeaderValue();
}
finally
Expand Down
27 changes: 15 additions & 12 deletions test/Microsoft.AspNet.Server.KestrelTests/FrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,25 @@ public void ChunkedPrefixMustBeHexCrLfWithoutLeadingZeros(int dataCount, string
[InlineData("Connection:\r\n \r\nCookie \r\n", 1)]
public void EmptyHeaderValuesCanBeParsed(string rawHeaders, int numHeaders)
{
var socketInput = new SocketInput(new MemoryPool2());
var headerCollection = new FrameRequestHeaders();
using (var memory = new MemoryPool2())
{
var socketInput = new SocketInput(memory);
var headerCollection = new FrameRequestHeaders();

var headerArray = Encoding.ASCII.GetBytes(rawHeaders);
var inputBuffer = socketInput.IncomingStart(headerArray.Length);
Buffer.BlockCopy(headerArray, 0, inputBuffer.Data.Array, inputBuffer.Data.Offset, headerArray.Length);
socketInput.IncomingComplete(headerArray.Length, null);
var headerArray = Encoding.ASCII.GetBytes(rawHeaders);
var inputBuffer = socketInput.IncomingStart(headerArray.Length);
Buffer.BlockCopy(headerArray, 0, inputBuffer.Data.Array, inputBuffer.Data.Offset, headerArray.Length);
socketInput.IncomingComplete(headerArray.Length, null);

var success = Frame.TakeMessageHeaders(socketInput, headerCollection);
var success = Frame.TakeMessageHeaders(socketInput, headerCollection);

Assert.True(success);
Assert.Equal(numHeaders, headerCollection.Count());
Assert.True(success);
Assert.Equal(numHeaders, headerCollection.Count());

// Assert TakeMessageHeaders consumed all the input
var scan = socketInput.ConsumingStart();
Assert.True(scan.IsEnd);
// Assert TakeMessageHeaders consumed all the input
var scan = socketInput.ConsumingStart();
Assert.True(scan.IsEnd);
}
}
}
}
13 changes: 10 additions & 3 deletions test/Microsoft.AspNet.Server.KestrelTests/TestInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@

namespace Microsoft.AspNet.Server.KestrelTests
{
class TestInput : IConnectionControl, IFrameControl
class TestInput : IConnectionControl, IFrameControl, IDisposable
{
private readonly MemoryPool2 _memory2;

public TestInput()
{
var memory = new MemoryPool();
var memory2 = new MemoryPool2();
_memory2 = new MemoryPool2();
FrameContext = new FrameContext
{
SocketInput = new SocketInput(memory2),
SocketInput = new SocketInput(_memory2),
Memory = memory,
ConnectionControl = this,
FrameControl = this
};
}

public void Dispose()
{
_memory2.Dispose();
}

public FrameContext FrameContext { get; set; }

public void Add(string text, bool fin = false)
Expand Down