NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

Uniqueness of Guid.NewGuid ?

Open murshex opened this issue 3 years ago • 10 comments

I noticed that Guid.NewGuid is used for each peer, I know that most likely, it will be a unique Guid, but is it guaranteed?

murshex avatar Aug 07 '22 10:08 murshex

Uniqueness is guaranteed for a host which is important to keep server sessions in dictionary by guid Id. For different peers probability is dramatically low and is not so important. For example we need client Id only for logging.

chronoxor avatar Aug 07 '22 11:08 chronoxor

I think it’s important to guarantee uniqueness because this is just a potential source of problems

murshex avatar Aug 07 '22 11:08 murshex

In TcpServer.cs, RegisterSession uses Sessions.TryAdd but doesn’t handle a false return value (Aka duplicate guid)

murshex avatar Aug 07 '22 11:08 murshex

https://stackoverflow.com/questions/8642858/what-are-the-chances-to-get-a-guid-newguid-duplicate

chronoxor avatar Aug 07 '22 11:08 chronoxor

In next version I'll add virtual Guid CreateGuid() method, which you can override in your code if you need more paranoid UUID generation. For example create a cryptographically secure random GUID in .NET

chronoxor avatar Aug 07 '22 11:08 chronoxor

I’ve seen that stackoverflow post before, but I don’t see any hard evidence. After researching:

From dotnet runtime: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Guid.Windows.cs

Function being used: https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cocreateguid

To a very high degree of certainty, this function returns a unique value

I stand corrected! Sorry!

murshex avatar Aug 07 '22 12:08 murshex

https://stackoverflow.com/questions/2621563/how-random-is-system-guid-newguid-take-two

After reading this I feel less confident…

murshex avatar Aug 07 '22 12:08 murshex

Instead of using a Guid identifier, what about using Interlocked.Increment ?

murshex avatar Aug 08 '22 01:08 murshex

Instead of using a Guid identifier, what about using Interlocked.Increment ?

i like this idea

M0n7y5 avatar Sep 26 '22 06:09 M0n7y5

https://stackoverflow.com/questions/2621563/how-random-is-system-guid-newguid-take-two

After reading this I feel less confident…

You're mistaking randomness and uniqueness. GUIDs do not guarantee any useful pseudorandomness, they are not built for that purpose. GUIDs are however perfect for uniqueness, as a relevant comment from one of your linked stackoverflow posts demonstrates:

Statistically speaking, you would be more likely to win the lottery 30,382 times, consecutively, than to run into a single pair of GUIDs within a sample of 1,000,000,000,000 GUIDs.

If you get a collision... go buy a ticket!

Instead of using a Guid identifier, what about using Interlocked.Increment ?

This only makes things worse in terms of uniqueness, considering that this library is built for networked applications, and Interlocked only guarantees uniqueness within a single process.

MellowArpeggiation avatar Nov 15 '22 22:11 MellowArpeggiation