Memory leak in HttpConnection
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 6.1k
- Forks
- 1.7k
- PR merge metrics
- No merged PRs in 30d
Description
Hi there,
Just writing to let you know of a memory leak with the HttpConnection class, and a resolution.
Backstory: I have been running some stress tests against your WebServer, and noticed a sharp memory spike when running the server on .NET, with roughly 180mb being allocated per 10,000 requests. Tests were run using Apache benchmark.
Note: This issue appears to be .net specific. with no problems running on the Mono platform.
Steps to reproduce
var s = new HttpServer(5000);
s.OnGet += (object sender, HttpRequestEventArgs e) =>
{
byte[] data = System.Text.Encoding.UTF8.GetBytes("hello world");
e.Response.OutputStream.Write(data, 0, data.Length);
};
s.Start();
# apache benchmark test
ab -c 100 -n 10000 http://[ip]:5000/
The problem appears to be due to a timer inside the HttpConnection not being disposed. Additionally, its also not being disposed in the latest Mono repo also, so possibly a bug? Anyway, by dropping in a timer.Dispose() at the end of the HttpConnection.Close() method should resolve the issue.
// /websocket-sharp/Net/HttpConnection.cs line 387
internal void Close (bool force)
{
if (_socket != null) {
if (_outputStream != null) {
_outputStream.Close ();
_outputStream = null;
}
var req = _context.Request;
var res = _context.Response;
force |= !req.KeepAlive;
if (!force)
force = res.Headers ["Connection"] == "close";
if (!force &&
req.FlushInput () &&
(!_chunked || (_chunked && !res.ForceCloseChunked))) {
// Don't close. Keep working.
_reuses++;
unbind ();
init ();
BeginReadRequest ();
return;
}
var socket = _socket;
_socket = null;
try {
socket.Shutdown (SocketShutdown.Both);
}
catch {
}
finally {
if (socket != null)
socket.Close ();
}
unbind ();
removeConnection ();
//--------------------------
// dispose of timer
//-------------------------
try {
_timer.Dispose()
}
catch {
}
return;
}
}
Cheers
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in /websocket-sharp/Net/HttpConnection.cs around line 387, where Close(bool force) handles connection cleanup. Reproduce the behavior with the supplied HttpServer example and Apache benchmark command, then inspect the timer lifecycle. Done means the timer is disposed during connection closure and the stress test no longer shows the reported allocation spike.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend, networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100