Skip to content

Triage for dotnet/runtime#116190 by jonmdev - HttpListener cannot be safely stopped and started #1099

Description

@MihuBot

Triage for dotnet/runtime#116190.
Last updated: 2025-06-01_11-59-06

HttpListener cannot be safely stopped and started by jonmdev
[Tool] Searching for HttpListener ObjectDisposedException, HttpListener cannot be restarted, HttpListener stop start exception, System.Net.HttpRequestQueueV2Handle ObjectDisposedException, HttpListener disposed object (IncludeOpen=True, IncludeClosed=True, IncludeIssues=True, IncludePullRequests=False, CreatedAfter=N/A)
[Tool] Found 37 issues, 20 comments, 37 returned results (11798 ms)
[Tool] Obtained 5 comments for issue #96705: C# HttpListener will show ObjectDisposedException if you stop listen after start listen immediately
[Tool] Obtained 6 comments for issue #30284: [HttpListener] HttpListener Start()/Stop()/Start()/BeginGetContext() causes ObjectDisposedException on Windows

Here are related issues and discussions for issue #116190 ("HttpListener cannot be safely stopped and started"):


1. Issue #96705 (January 2024) - "C# HttpListener will show ObjectDisposedException if you stop listen after start listen immediately"

  • Summary: Reports that starting and immediately stopping an HttpListener throws an ObjectDisposedException. The exception is seen in debug logs but is caught internally and does not affect the actual call to Stop().
  • Key Comments:
    • The exception is thrown and caught internally in the implementation; it does not propagate to user code.
    • The .NET team triaged this as not a bug since the exception is handled internally and only visible in debug logs.
    • The team notes that HttpListener is discouraged for new development and is unlikely to receive fixes for such issues.
    • The issue was closed with the explanation that unless there is a practical impact beyond debug logging, it is not actionable.

2. Issue #30284 (July 2019) - "[HttpListener] HttpListener Start()/Stop()/Start()/BeginGetContext() causes ObjectDisposedException on Windows"

  • Summary: Demonstrates that calling Stop() and then Start() on an HttpListener, followed by BeginGetContext(), results in an ObjectDisposedException due to a disposed ThreadPoolBoundHandle. The problem is specific to .NET Core (not .NET Framework).
  • Key Comments:
    • The root cause is that Stop() disposes an internal handle, but Start() does not recreate it, so subsequent operations fail.
    • The documentation states that Stop() followed by Start() should be valid, but this is not the case in .NET Core.
    • There was an attempt to fix this, but the fix was reverted due to flaws.
    • The issue was considered for a fix in .NET 5.0, but there is no indication it was resolved.
    • The problem is a known difference from .NET Framework and is a breaking change for some ported applications.

3. Issue #71364 (June 2022) - "HttpListenerResponse: Unhandled exception. System.ObjectDisposedException"

  • Summary: Reports ObjectDisposedException when using HttpListener.GetContext() on Linux with large requests. The exception does not occur on Windows.
  • Key Comments:
    • The .NET team notes that HttpListener is not in active development and would only accept community contributions for fixes.
    • This is considered a corner case scenario.

4. Issue #25947 (April 2018) - "Invalid HttpListener.Stop() behaviour"

  • Summary: Describes unexpected behavior after calling HttpListener.Stop(), including errors on client connections and exceptions when closing ClientWebSocket.
  • Key Comments:
    • The issue highlights that stopping the listener can leave client connections in a problematic state.
    • The behavior is consistent across .NET Core 2.0 and 2.1.

5. Issue #35526 (April 2020) - "[HttpListener] No way to stop HttpListener from GetContext"

  • Summary: Reports that after calling GetContext() from another thread, there is no way to stop the listener cleanly, leading to blocked threads.
  • Key Comments:
    • This is another example of problematic lifecycle management in HttpListener.

6. Issue #37242 (June 2020) - "[HttpListener] Failure in HttpListener.Start causes HttpListener get disposed automatically"

  • Summary: If HttpListener.Start() throws, the instance is disposed and cannot be reused, which is not documented and can surprise users.
  • Key Comments:
    • The behavior is by design (matches .NET Framework), but is not documented and can be confusing.

Summary of Findings

  • There are multiple longstanding issues with HttpListener lifecycle management, especially around Stop() and Start() sequences.
  • The specific problem in #116190 (ObjectDisposedException on stop/start) is a known issue, with similar reports in #30284 and #96705.
  • The .NET team has repeatedly stated that HttpListener is not in active development, is discouraged for new projects, and fixes are unlikely unless contributed by the community.
  • The exceptions are often caught internally, but can surface in debug logs or in certain usage patterns (especially when porting from .NET Framework).
  • Workarounds are limited, and the recommended approach for new development is to use alternative server technologies (e.g., Kestrel).

References:

If you need more details on any of these issues or further related discussions, let me know!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions