Created
March 16, 2017 08:58
-
-
Save tmds/493a1c56cb00e453e131a6c3134b76a3 to your computer and use it in GitHub Desktop.
Socket no CLOEXEC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
new Program().Run(); | |
} | |
Thread _serverThread; | |
Thread _clientThread; | |
Thread _sleepProcessThread; | |
// used to synchronize between the threads | |
TaskCompletionSource<IPEndPoint> _serverListening = new TaskCompletionSource<IPEndPoint>(); | |
TaskCompletionSource<object> _clientConnected = new TaskCompletionSource<object>(); | |
TaskCompletionSource<object> _sleepProcessRunning = new TaskCompletionSource<object>(); | |
public void Run() | |
{ | |
_serverThread = new Thread(ServerThread); | |
_clientThread = new Thread(ClientThread); | |
_sleepProcessThread = new Thread(SleepProcessThread); | |
var threads = new [] { _serverThread, _clientThread, _sleepProcessThread }; | |
foreach (var thread in threads) | |
{ | |
thread.Start(); | |
} | |
foreach (var thread in threads) | |
{ | |
thread.Join(); | |
} | |
} | |
public void ServerThread(object state) => ServerThreadAsync().Wait(); | |
public void ClientThread(object state) => ClientThreadAsync().Wait(); | |
public void SleepProcessThread(object state) => SleepProcessThreadAsync().Wait(); | |
public async Task ServerThreadAsync() | |
{ | |
var listener = new TcpListener(IPAddress.Loopback, 0); | |
listener.Start(); | |
// clients can connect | |
_serverListening.SetResult(listener.LocalEndpoint as IPEndPoint); | |
var socket = await listener.AcceptSocketAsync(); | |
System.Console.WriteLine($"{DateTime.Now}: Server: accept client"); | |
// sleepProcess can start when we accepted our client | |
_clientConnected.SetResult(null); | |
// ensure the sleepProcess is running | |
await _sleepProcessRunning.Task; | |
System.Console.WriteLine($"{DateTime.Now}: Server: sleep process started"); | |
// dispose the socket | |
socket.Dispose(); | |
System.Console.WriteLine($"{DateTime.Now}: Server: dispose client"); | |
} | |
public async Task ClientThreadAsync() | |
{ | |
// wait for the server to start | |
var endpoint = await _serverListening.Task; | |
// connect the client | |
var client = new TcpClient(); | |
await client.ConnectAsync(endpoint.Address, endpoint.Port); | |
var stream = client.GetStream(); | |
var buffer = new byte[1]; | |
stream.Read(buffer, 0, buffer.Length); | |
System.Console.WriteLine($"{DateTime.Now}: Client: received close"); | |
client.Dispose(); | |
} | |
public async Task SleepProcessThreadAsync() | |
{ | |
// wait for the client to connect | |
await _clientConnected.Task; | |
Process.Start("sleep", "10s"); | |
// process running, client can connect | |
_sleepProcessRunning.SetResult(null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since the CLOEXEC flag is not set on the socket, the child process (sleep) will keep the socket open even though the server has disposed it: