From 63b93561e30c5e83ad61403d6948ffb45f9fddad Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Mon, 11 Mar 2019 08:33:16 +0000 Subject: [PATCH] Added a global mutex (not used yet) and moved around the code for loggin in since I suspect the Get Roles call is using deffered execution on the database causing the lock when attempting to access straight away #2750 --- src/Ombi.Helpers/GlobalMutex.cs | 38 ++++++++++++++++++++++ src/Ombi/Controllers/V1/TokenController.cs | 6 ++-- 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/Ombi.Helpers/GlobalMutex.cs diff --git a/src/Ombi.Helpers/GlobalMutex.cs b/src/Ombi.Helpers/GlobalMutex.cs new file mode 100644 index 000000000..ecf9cdf15 --- /dev/null +++ b/src/Ombi.Helpers/GlobalMutex.cs @@ -0,0 +1,38 @@ +using System; +using System.Threading; + +namespace Ombi.Helpers +{ + public static class GlobalMutex + { + public static T Lock(Func func) + { + const string mutexId = "Global\\OMBI"; + + using (var mutex = new Mutex(false, mutexId, out __)) + { + var hasHandle = false; + try + { + try + { + hasHandle = mutex.WaitOne(5000, false); + if (hasHandle == false) + throw new TimeoutException("Timeout waiting for exclusive access"); + } + catch (AbandonedMutexException) + { + hasHandle = true; + } + + return func(); + } + finally + { + if (hasHandle) + mutex.ReleaseMutex(); + } + } + } + } +} diff --git a/src/Ombi/Controllers/V1/TokenController.cs b/src/Ombi/Controllers/V1/TokenController.cs index 91b2656da..eea38dcd1 100644 --- a/src/Ombi/Controllers/V1/TokenController.cs +++ b/src/Ombi/Controllers/V1/TokenController.cs @@ -121,9 +121,6 @@ namespace Ombi.Controllers.V1 return new UnauthorizedResult(); } - user.LastLoggedIn = DateTime.UtcNow; - await _userManager.UpdateAsync(user); - var claims = new List { new Claim(JwtRegisteredClaimNames.Sub, user.UserName), @@ -150,6 +147,9 @@ namespace Ombi.Controllers.V1 //await _token.CreateToken(new Tokens() {Token = accessToken, User = user}); } + user.LastLoggedIn = DateTime.UtcNow; + await _userManager.UpdateAsync(user); + return new JsonResult(new { access_token = accessToken,