Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpListenerResponse: Unhandled exception. System.ObjectDisposedException #71364

Open
caoyudeng opened this issue Jun 28, 2022 · 10 comments
Open

Comments

@caoyudeng
Copy link

caoyudeng commented Jun 28, 2022

Description

Linux localhost.localdomain 4.18.0-193.el8.x86_64 #1 SMP Fri May 8 10:59:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
.Net 5.0/6.0
HttpListener.GetContext() Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.

Reproduction Steps

  1. demo
using System;
using System.Net;

namespace TestHttp
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpListener oListener = new HttpListener();
            oListener.Prefixes.Add("http://*:9527/");
            oListener.Start();
            Console.WriteLine("Listening...");

            while (true)
            {
                HttpListenerContext oContext = default;
                try
                {
                    oContext = oListener.GetContext();
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{e.Message}");
                }

                var oRequest = oContext.Request;
                var oResponse = oContext.Response;

                string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                oResponse.ContentLength64 = buffer.Length;
                System.IO.Stream output = oResponse.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();
            }

            oListener.Stop();
        }
    }
}
  1. Postman: Send extremely long Get requests(300+kb)

Expected behavior

no exception.

Actual behavior

  • linux .NET 5
Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.HttpListenerResponse'.
   at System.Net.HttpListenerResponse.CheckDisposed()
   at System.Net.HttpListenerResponse.<>c.<Close>b__74_0(IAsyncResult iar)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.ContextAwareResult.CompleteCallback()
   at System.Net.ContextAwareResult.<>c.<Complete>b__15_0(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.ContextAwareResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionCallback(Int32 numBytes, SocketError errorCode)
   at System.Net.Sockets.OverlappedAsyncResult.CompletionCallback(Int32 numBytes, Byte[] socketAddress, Int32 socketAddressSize, SocketFlags receivedFlags, SocketError errorCode)
   at System.Net.Sockets.SocketAsyncContext.BufferMemorySendOperation.InvokeCallback(Boolean allowPooling)
   at System.Net.Sockets.SocketAsyncContext.AsyncOperation.<>c.<TryCancel>b__18_0(Object o)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  • linux .NET 6
Object name: 'System.Net.HttpListenerResponse'.
   at System.Net.HttpListenerResponse.CheckDisposed()
   at System.Net.HttpListenerResponse.<>c.<Close>b__74_0(IAsyncResult iar)
   at System.Threading.Tasks.TaskToApm.TaskAsyncResult.InvokeCallback()
   at System.Threading.Tasks.AwaitTaskContinuation.<>c.<.cctor>b__17_0(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_1(Object state)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback()
Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.HttpListenerResponse'.
   at System.Net.HttpListenerResponse.CheckDisposed()
   at System.Net.HttpListenerResponse.<>c.<Close>b__74_0(IAsyncResult iar)
   at System.Threading.Tasks.TaskToApm.TaskAsyncResult.InvokeCallback()
   at System.Threading.Tasks.AwaitTaskContinuation.<>c.<.cctor>b__17_0(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__127_1(Object state)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback() 

====================================================================

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 28, 2022
@ghost
Copy link

ghost commented Jun 28, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Linux localhost.localdomain 4.18.0-193.el8.x86_64 #1 SMP Fri May 8 10:59:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
HttpListener.GetContext() Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.

Reproduction Steps

Send extremely long Get requests

Expected behavior

==============================================================

Actual behavior

Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.HttpListenerResponse'.
at System.Net.HttpListenerResponse.CheckDisposed()
at System.Net.HttpListenerResponse.<>c.b__74_0(IAsyncResult iar)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.ContextAwareResult.CompleteCallback()
at System.Net.ContextAwareResult.<>c.b__15_0(Object s)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.ContextAwareResult.Complete(IntPtr userToken)
at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionCallback(Int32 numBytes, SocketError errorCode)
at System.Net.Sockets.OverlappedAsyncResult.CompletionCallback(Int32 numBytes, Byte[] socketAddress, Int32 socketAddressSize, SocketFlags receivedFlags, SocketError errorCode)
at System.Net.Sockets.SocketAsyncContext.BufferMemorySendOperation.InvokeCallback(Boolean allowPooling)
at System.Net.Sockets.SocketAsyncContext.AsyncOperation.<>c.b__18_0(Object o)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

====================================================================

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: caoyudeng
Assignees: -
Labels:

area-System.Net

Milestone: -

@davidfowl
Copy link
Member

How long was the get request ?

@caoyudeng
Copy link
Author

300+kb

@danmoseley
Copy link
Member

Looks like this is .NET 5? That is out of support and not getting security fixes. Could you upgrade to .NET 6 and try again?

@caoyudeng
Copy link
Author

linux .net 5: Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
linux .net 6: Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
win .net 5: no exception.

@caoyudeng
Copy link
Author

update comment

@danmoseley
Copy link
Member

danmoseley commented Jun 29, 2022

Just curious as I'm not a networking person, what is a huge GET request -- a lot of headers? an enormously long URL? huge cookies? Wondering whether I could do it with curl.

@caoyudeng
Copy link
Author

curl --location --request GET 'http://127.0.0.1:9527?content=<long url 300+kb>'

@danmoseley
Copy link
Member

Yeah, that gives argument too long so I guess curl doesn't work for vast URL's.

@karelz
Copy link
Member

karelz commented Jun 30, 2022

Triage: HttpListener is not in active development. We would likely take a contribution for a fix though.
This looks like a corner case scenario.

@karelz karelz added this to the Future milestone Jun 30, 2022
@karelz karelz removed the untriaged New issue has not been triaged by the area owner label Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants