sta / sta/websocket-sharp

HttpListener Stop() does not properly clean up.

Open
#235 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

I am observing the following behavior. First I set up an HttpListener object like this:

            _listener = new HttpListener();
            string prefix = String.Format("http://*:{0}/", port);
            _listener.Prefixes.Add(prefix);
            _listener.AuthenticationSchemeSelector = SelectAuthScheme;
            _listener.Realm = realm;
            _listener.UserCredentialsFinder = GetCredential;

The I start the listener and begin listening asynchronously

            _listener.Start();
            var getContext = Task.Factory.FromAsync<HttpListenerContext>(_listener.BeginGetContext,   _listener.EndGetContext, null);
            getContext.ContinueWith(t => ProcessRequest(t.Result));

I then stop the listener:

            _listener.Stop();

I then start the listener again (either after a set amount of time or immediately, it doesn't matter) and I get back "400 Bad Request" on the next request

The issue is that the HttpListener stop method does not properly clean up. It will stay listening on a stream read here so the next time a connection comes through, the listener won't have the prefix anymore since it was removed in Stop() and BindContext will fail because searchListener will no longer return a valid listener here. I was able to stop this behavior by adding in a cleanup(false) call at the end of Stop(). Is this appropriate behavior? If not, then what can be done?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the HttpConnection.cs stream-read location and EndPointListener.cs BindContext/searchListener location linked in the report, then trace how HttpListener.Stop() removes prefixes and performs cleanup. Reproduce the sequence of Start, BeginGetContext, Stop, and restart, and compare behavior with and without cleanup(false). Done means restarting the listener no longer produces a 400 response from stale connection state.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.