Skip to content

Instantly share code, notes, and snippets.

@antonfirsov
Created December 18, 2019 19:09
Show Gist options
  • Save antonfirsov/26ba5b8de7a88f1818e0adace9dfc9f7 to your computer and use it in GitHub Desktop.
Save antonfirsov/26ba5b8de7a88f1818e0adace9dfc9f7 to your computer and use it in GitHub Desktop.
_rightEndpoint Race Repro
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UdpMultipleTest
{
static class Program
{
static void Main(string[] args)
{
IPEndPoint goodEndpoint = new IPEndPoint(IPAddress.Parse("10.20.30.40"), 12345);
IPEndPoint badEndpoint = new IPEndPoint(IPAddress.Broadcast, 12345);
int counter = 0;
void Print(object msg) => Console.WriteLine($"[{counter}] {msg}");
byte[] buffer1 = new byte[32];
byte[] buffer2 = new byte[32];
void RunExperiment()
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
void Good()
{
socket.BeginSendTo(buffer1, 0, buffer1.Length, SocketFlags.None, goodEndpoint, null, null);
if (socket.LocalEndPoint == null) Print("!!!RACE!!!");
}
void Bad()
{
try
{
socket.BeginSendTo(buffer2, 0, buffer1.Length, SocketFlags.None, badEndpoint, null, null);
}
catch { }
}
Thread good = new Thread(Good);
Thread bad = new Thread(Bad);
bad.Start();
good.Start();
bad.Join();
good.Join();
}
counter++;
}
for (int i = 0; i < 2000; i++)
{
RunExperiment();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment