Signalr: Hub OnDisconnected - Context.User.Identity.Name throwing an ObjectDisposedException in IE

Created on 6 Apr 2014  路  21Comments  路  Source: SignalR/SignalR

Hi guys, first of all SignalR is awesome. Also I'm sorry if this has been documented before - couldn't find anything.

Steps to reproduce the issue:

  1. Create an ASP.NET MVC project using Windows Authentication
  2. Create a Hub:

C# public class ChatHub : Hub { public override Task OnDisconnected() { // throws an ObjectDisposedException if you open the app in IE (not in Chrome) string name = Context.User.Identity.Name; return base.OnDisconnected(); } }

I've tested this on 2 different machines (Windows 8 and Windows 7). I should also note that it works fine on an app that uses Forms Authentication. Let me know if I can provide more details.

EDIT

After some more fiddling, it turns out it happens in IE10 and IE9 as well.

Bug

All 21 comments

Why are you calling OnConnected from OnDisconnected?

@davidfowl Sorry that was a typo. I meant OnDisconnected. Edited. (it's been a looong day)

@davidfowl I tried a lot of things and it appears that this fixes the problem:

window.onbeforeunload = function (e) {
   $.connection.hub.stop();
};

@n1ghtmare What version of SignalR are you using?

@davidfowl Sorry I should've mentioned. I'm using 2.0.3 (which I pulled from NuGet). Additionally in regards to my "fix" - it seems to be working like 95% of the time, but occasionally it throws the exception, I'm not sure what is the reason.

I can repro this too.
I see the below error in _user in ServerRequest class in this case.
If we also cache the User.Identity in ServerRequest class and use the cached Identity for user in this case, then we can avoid the exception when use Windows Authentication.

System.ObjectDisposedException occurred
  HResult=-2146232798
  Message=Safe handle has been closed
  Source=mscorlib
  ObjectName=""
  StackTrace:
       at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
       at Microsoft.Win32.Win32Native.GetTokenInformation(SafeTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
       at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeTokenHandle tokenHandle, TokenInformationClass tokenInformationClass)
  InnerException:

@halter73 can you take a look at this please?

@DamianEdwards What should I do with this bug? This is fixed on IIS but not OWIN self host.

The self host issue isn't a SignalR issue, it's Katana. Log the bug with them and point back here for details.

Reopening because I plan to revert #3206 as part of the fix (#3347) for #3342.

The problem is that #3206 only fixes the issue for clean WebSocket disconnects. You will still get an ODE if you try accessing WindowsIdentity.Name in OnDisconnected after an unclean disconnects with any transport.

This also made it harder to close the WebSocket from the server after an exception in the receive loop.

The best solution for this issue might be to copy what we can from the Windows IPrincipal and/or IIdentity before it gets disposed and use that when calling OnDisconnected.

HI, Is this issue fixed. I am still getting the issue in version 2.1.2, Is it in 2.2.0?

Although it's not a problem to catch the exception, I was also wondering if this issue has a chance of being solved.

Maybe you are all tied-up in dotnet core?

On a side note, the README.md points to a none existing ci server:

We have a CI Server (http://ci.signalr.net/)

--> https://ci.asp.net/ no such host

I am still seeing this issue today on SignalR 2.2.1

OnConnect has the full identity information populated. OnDisconnect all the identity information properties throw exceptions.

When you'll fix this issue? Thanks in advance

FYI: With SignalR 2.2.2 the issue is still there.

What are you supposed to do with OnDisconnected with the "Safe Handle" exception being thrown to update that the user is no longer online. How do you track users with this exception. ConnectionIds? That's it? No username?

You have to manage the mapping between usernames and connectionIds yourself, for example updating a database table when the user first makes the connection. In the OnDisconnected event you will get the connectionId, you need to use this to look up which user had that connectionId

That's what I figured, I was following MS documentation : https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/mapping-users-to-connections#inmemory

public override Task OnDisconnected(bool stopCalled)
        {
            var name = Context.User.Identity.Name;
            var table = GetConnectionTable();

            var deleteOperation = TableOperation.Delete(
                new ConnectionEntity(name, Context.ConnectionId) { ETag = "*" });
            table.Execute(deleteOperation);

            return base.OnDisconnected(stopCalled);
        }

This issue has been closed as part of issue clean-up as described in https://blogs.msdn.microsoft.com/webdev/2018/09/17/the-future-of-asp-net-signalr/. If you're still encountering this problem, please feel free to re-open and comment to let us know! We're still interested in hearing from you, the backlog just got a little big and we had to do a bulk clean up to get back on top of things. Thanks for your continued feedback!

I am still facing the issue. I am using WPF client and when the user disconnects, it runs into the same error when I try to access Context.User.Identity.Name;

public override Task OnDisconnected(bool stopCalled) { var userConnection = new UserConnection { UserName = this.Context.User.Identity.Name, ConnectionID = this.Context.ConnectionId }; this.ScenarioHubContainer.OnDisconnected(userConnection); return base.OnDisconnected(stopCalled); }

Above is the code where I am running into the issue.

Please note:

  • SignalR is hosted within WebApi project.

  • Windows Authentication has been enabled for the WebAPI.

  • using Microsoft.AspNet.SignalR version="2.2.2" targetFramework="net472"

Hi, I am facing this issue as well,
I am using WPF Client with MVC 5 which connects to Signalr (version 2.3.0.0) Hub hosted on IIS. I see below error which is sporadic in nature in OnDisconnected

System.ObjectDisposedException: 'Safe handle has been closed' when I call Context.User.Identity.Name

Exception Details :
System.ObjectDisposedException
HResult=0x80131622
Message=Safe handle has been closed
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Microsoft.Win32.Win32Native.GetTokenInformation(SafeAccessTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeAccessTokenHandle tokenHandle, TokenInformationClass tokenInformationClass)
at System.Security.Principal.WindowsIdentity.get_User()
at System.Security.Principal.WindowsIdentity.GetName()
at System.Security.Principal.WindowsIdentity.get_Name()
at Hub.OnDisconnected(Boolean stopCalled) in C:\pathoftheProject\Hub.cs:line 146
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Disconnect(IHub hub, Boolean stopCalled)
at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.<>c__DisplayClass3_0.b__0(IHub hub, Boolean stopCalled)
at Microsoft.AspNet.SignalR.Hubs.HubPipeline.Disconnect(IHub hub, Boolean stopCalled)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.<>c__DisplayClass33_0.b__0(IHub hub)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.<>c__DisplayClass35_0.b__0(IHub instance)
at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ExecuteHubEvent(IRequest request, String connectionId, Func2 action) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnDisconnected(IRequest request, String connectionId, Boolean stopCalled) at Microsoft.AspNet.SignalR.PersistentConnection.<>c__DisplayClass64_2.<ProcessRequestPostGroupRead>b__7() at Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func1 func)

Was this page helpful?
0 / 5 - 0 ratings