From a44d3f26557ed5c4c5277c4906835da07aa5165e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 25 Nov 2020 23:23:58 +0100 Subject: [PATCH] Avoid socket leaks in examples. --- examples/bench-dummy-http-server/source/app.d | 3 ++- examples/echo-server/source/app.d | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/bench-dummy-http-server/source/app.d b/examples/bench-dummy-http-server/source/app.d index edbd34c..a5fb025 100644 --- a/examples/bench-dummy-http-server/source/app.d +++ b/examples/bench-dummy-http-server/source/app.d @@ -19,7 +19,7 @@ void main() conn.readLine(r); if (!r.count) break; } - conn.write(cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\n\r\nHello, World!"); + conn.write(cast(const(ubyte)[])"HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nKeep-Alive: timeout=10\r\n\r\nHello, World!"); conn.flush(); } } catch (Exception e) { @@ -30,6 +30,7 @@ void main() auto listener = listenTCP(8080, &staticAnswer, "127.0.0.1"); logInfo("Listening to HTTP requests on http://127.0.0.1:8080/"); + scope (exit) listener.stopListening(); runApplication(); } diff --git a/examples/echo-server/source/app.d b/examples/echo-server/source/app.d index 76a9c74..bb7c64c 100644 --- a/examples/echo-server/source/app.d +++ b/examples/echo-server/source/app.d @@ -5,10 +5,16 @@ import vibe.core.stream : pipe; void main() { - listenTCP(7000, (conn) @safe nothrow { + auto listeners = listenTCP(7000, (conn) @safe nothrow { try pipe(conn, conn); catch (Exception e) logError("Error: %s", e.msg); }); + + // closes the listening sockets + scope (exit) + foreach (l; listeners) + l.stopListening(); + runApplication(); }