diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs
index 25463b660e..c5cb810e53 100644
--- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs
+++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs
@@ -179,6 +179,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer
private void OnWebSocketConnecting(WebSocketConnectingEventArgs args)
{
+ if (_disposed)
+ {
+ return;
+ }
+
if (WebSocketConnecting != null)
{
WebSocketConnecting(this, args);
@@ -187,6 +192,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer
private void OnWebSocketConnected(WebSocketConnectEventArgs args)
{
+ if (_disposed)
+ {
+ return;
+ }
+
if (WebSocketConnected != null)
{
WebSocketConnected(this, args);
@@ -331,6 +341,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer
var httpRes = httpReq.Response;
+ if (_disposed)
+ {
+ httpRes.StatusCode = 503;
+ httpRes.Close();
+ return Task.FromResult(true);
+ }
+
var operationName = httpReq.OperationName;
var localPath = url.LocalPath;
diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs
index 8719f54489..33d1069162 100644
--- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs
+++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs
@@ -71,6 +71,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// The web socket listeners.
private readonly List _webSocketListeners = new List();
+ private bool _disposed;
+
///
/// Initializes a new instance of the class.
///
@@ -143,6 +145,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// The instance containing the event data.
void HttpServer_WebSocketConnected(object sender, WebSocketConnectEventArgs e)
{
+ if (_disposed)
+ {
+ return;
+ }
+
var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, _jsonSerializer, _logger)
{
OnReceive = ProcessWebSocketMessageReceived,
@@ -164,6 +171,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager
/// The result.
private async void ProcessWebSocketMessageReceived(WebSocketMessageInfo result)
{
+ if (_disposed)
+ {
+ return;
+ }
+
//_logger.Debug("Websocket message received: {0}", result.MessageType);
var tasks = _webSocketListeners.Select(i => Task.Run(async () =>
@@ -244,6 +256,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager
throw new ArgumentNullException("dataFunction");
}
+ if (_disposed)
+ {
+ throw new ObjectDisposedException(GetType().Name);
+ }
+
cancellationToken.ThrowIfCancellationRequested();
var connectionsList = connections.Where(s => s.State == WebSocketState.Open).ToList();
@@ -301,6 +318,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager
///
public void Dispose()
{
+ _disposed = true;
+
Dispose(true);
GC.SuppressFinalize(this);
}
diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs
index ac1b7ca996..f83c7d545d 100644
--- a/MediaBrowser.ServerApplication/MainStartup.cs
+++ b/MediaBrowser.ServerApplication/MainStartup.cs
@@ -555,9 +555,10 @@ namespace MediaBrowser.ServerApplication
private static void ShutdownWindowsApplication()
{
- _logger.Info("Calling Application.Exit");
- Application.Exit();
+ //_logger.Info("Calling Application.Exit");
+ //Application.Exit();
+ _logger.Info("Calling Environment.Exit");
Environment.Exit(0);
_logger.Info("Calling ApplicationTaskCompletionSource.SetResult");