JsonWebSocket failure under stress test

Sep 5, 2016 at 5:48 PM
Edited Sep 5, 2016 at 9:09 PM
Hi,

I started look at WebSocket4Net, an interesting project, I appreciate the effort you've spent on it. I noticed one problem though.

In the file "WebSocket4Net\JsonWebSocket.cs", the "token" is not unique enough and that causes a failure under stress test.

For instance, if I do a stress test like:
    var clientJsonWebSocket = new JsonWebSocket("ws://somewhere");
    for (var i = 0; i < 10000; ++i)
    {
        clientJsonWebSocket.Query<string>("Name", "content", s => { });
    }
...adding to the dictionary in RegisterExecutor below will fail with "An item with the same key has already been added."
        void RegisterExecutor<T>(string name, string token, IJsonExecutor executor)
        {
            lock (m_ExecutorDict)
            {
                if (string.IsNullOrEmpty(token))
                    m_ExecutorDict.Add(name, executor);
                else
                    m_ExecutorDict.Add(string.Format(m_QueryKeyTokenTemplate, name, token), executor); // <<<<<<<<< Gives ArgumentException under stress test
            }
        }
I did a quick fix with using a Guid instead (see below), seems to solve my problem, but I don't know if there may be other side effects:
        string Query<T>(string name, object content, IJsonExecutor executor)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

          //var token = m_Random.Next(1000, 9999).ToString();
            var token = Guid.NewGuid().ToString();

            RegisterExecutor<T>(name, token, executor);
Any chance you can look into this and do a fix?

(I had a simple server responding to the request when I tested, let me know if you need more info to reproduce)


Thanks in advance,
Mattias