From 2729418006ad93185a7c7d177228c991af859b58 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Thu, 15 Jun 2017 17:35:20 -0700 Subject: [PATCH] Make published paths thread safe --- Ooui/UI.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Ooui/UI.cs b/Ooui/UI.cs index 32b7cf2..312ff48 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -56,7 +56,7 @@ namespace Ooui public static void Publish (string path, Func elementCtor) { Console.WriteLine ($"PUBLISH {path}"); - publishedPaths[path] = elementCtor; + lock (publishedPaths) publishedPaths[path] = elementCtor; Start (); } @@ -148,12 +148,16 @@ namespace Ooui s.Write (clientJsBytes, 0, clientJsBytes.Length); } } - else if (publishedPaths.TryGetValue (path, out ctor)) { - WriteElementHtml (path, response); - } else { - response.StatusCode = 404; - response.Close (); + var found = false; + lock (publishedPaths) found = publishedPaths.TryGetValue (path, out ctor); + if (found) { + WriteElementHtml (path, response); + } + else { + response.StatusCode = 404; + response.Close (); + } } } @@ -184,7 +188,9 @@ namespace Ooui var path = url.LocalPath; Func ctor; - if (!publishedPaths.TryGetValue (path, out ctor)) { + var found = false; + lock (publishedPaths) found = publishedPaths.TryGetValue (path, out ctor); + if (!found) { listenerContext.Response.StatusCode = 404; listenerContext.Response.Close (); return;