Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not unique Package.ID generation. #101

Closed
KotM opened this issue Oct 26, 2018 · 4 comments
Closed

Not unique Package.ID generation. #101

KotM opened this issue Oct 26, 2018 · 4 comments
Assignees
Labels

Comments

@KotM
Copy link

KotM commented Oct 26, 2018

It seems like both IQ and Message classes (inherited from Package) incorrectly generates ID values when interacting with multiple threads. Sometimes these classes may produce the ID was generated before.

Tested on latest Jabber-net version too.

Please find below simply UnitTest with reproduction this issue almost constantly.

        [TestMethod]
        public void MultipleIQCreation()
        {
            ConcurrentBag<string> ids = new ConcurrentBag<string>();

            var doc = new XmlDocument();

            //for (int i = 0; i < 500; i++)
            //{
            //    ids.Add((new IQ(doc)).ID);
            //}

            Parallel.For(0, 500, i => ids.Add((new IQ(doc)).ID));

            Assert.AreEqual(500, ids.Count);
            Assert.AreEqual(500, ids.Distinct().Count());
        }
@KotM
Copy link
Author

KotM commented Oct 26, 2018

Workaround it simply -- I've just assigned my own id to the packet. But it would be great to fix it on Jabber-net side. Suppose, the fix would be easy enough, something like Interlocked.Increment(ref _jabberPacketId).

@KotM KotM changed the title No unique IQ.ID generation. Not unique IQ.ID generation. Oct 26, 2018
@KotM KotM changed the title Not unique IQ.ID generation. Not unique Package.ID generation. Oct 26, 2018
@ForNeVeR
Copy link
Member

Ah my gosh, this library is full of threading problems. Thanks for your wonderful unit test, I'll take a look.

@ForNeVeR ForNeVeR self-assigned this Oct 26, 2018
@pomoika
Copy link

pomoika commented Oct 31, 2018

The bug is in JabberNet/jabber/protocol/Element.cs, method NextID. It should be changed as follows:

public static string NextID()
{
    return "JN_" + System.Threading.Interlocked.Increment(ref s_counter).ToString();
}

@ForNeVeR
Copy link
Member

Thanks for your help, it definitely was an issue highlighted by @pomoika. It's fixed now and I've published a new preview release 3.0.0-preview3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants