Griffin.Framework icon indicating copy to clipboard operation
Griffin.Framework copied to clipboard

thread safety problem?

Open gcbork opened this issue 7 years ago • 3 comments

Hello,

I have the web server sample code running. (Program2 from http://blog.gauffin.org/2014/05/griffin-framework-performant-networking-in-net/#more-4822) I modified it to do SSL and print out the http method and the IP address. Here is the complete code:

    class MainClass
    {
        public static void Main (string[] args)
        {
            var certificate = new X509Certificate2("MyCert.p12", "password");
            var listener = new Griffin.Net.Protocols.Http.HttpListener();
            listener.ChannelFactory = new SecureTcpChannelFactory(new ServerSideSslStreamBuilder(certificate));
            listener.MessageReceived = OnMessage;
            //listener.BodyDecoder = new CompositeBodyDecoder();
            listener.Start(IPAddress.Any, 8443);
            Console.WriteLine ("Server running...");
            Console.ReadLine();
        }

        private static void OnMessage(ITcpChannel channel, object message)
        {
            var request = (HttpRequest)message;
            String FromIPAddress = ((IPEndPoint)request.RemoteEndPoint).Address.ToString ();
            Console.WriteLine ("Got " + request.HttpMethod + " request from " + FromIPAddress);

            var response = request.CreateResponse();
            if (request.Uri.AbsolutePath == "/favicon.ico")
            {
                Console.WriteLine ("Responding 404");
                response.StatusCode = 404;
                channel.Send(response);
                return;
            }

            var msg = Encoding.UTF8.GetBytes("Hello world");
            response.Body = new MemoryStream(msg);
            response.ContentType = "text/plain";
            channel.Send(response);
            if (request.HttpVersion == "HTTP/1.0")
                channel.Close();
        }
    }

I have two additional computers. On one (with IP .147) I am running a client that sends a POST message every 2 seconds. On the other one (with IP .55) I am running Apache Bench with: ab -n 500 -r -f SSL3 https://185.185.185.136:8443/

The server output is this:

Got GET request from 185.185.185.55 Got POST request from 185.185.185.147 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got POST request from 185.185.185.147 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got POST request from 185.185.185.147 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got POST request from 185.185.185.147 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55 Got POST request from 185.185.185.55 Got POST request from 185.185.185.55 Got GET request from 185.185.185.55 Got GET request from 185.185.185.55

The two lines in bold font make me really worried. How can that be? ApacheBench that runs on computer .55 does not send POST messages. On other tries I got the reverse, GET messages from .147. Could it be that something gets mixed up between threads?

Regards, Gerhard.

gcbork avatar Sep 07 '16 18:09 gcbork

I've taken note and will look it as soon as I can. AFAIK there should not be any static or threadstatic members that could cause this, but I need to take a careful look.

jgauffin avatar Sep 27 '16 05:09 jgauffin

Hello Jonas,

Any update on this one?

Regards, Gerhard

gcbork avatar Mar 04 '17 00:03 gcbork

Will try it with the new networking code. Sorry for the delay ;)

jgauffin avatar Feb 04 '20 21:02 jgauffin