From 4f65a94aa598b7db3ad10d9dc2e7b12cd8f739c8 Mon Sep 17 00:00:00 2001 From: neuecc Date: Fri, 28 Jan 2022 10:39:55 +0900 Subject: [PATCH] removing store HashAlgorithm to avoid memory leak --- src/LitJWT/JwtAlgorithm.cs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/LitJWT/JwtAlgorithm.cs b/src/LitJWT/JwtAlgorithm.cs index f72e1d7..3149de6 100644 --- a/src/LitJWT/JwtAlgorithm.cs +++ b/src/LitJWT/JwtAlgorithm.cs @@ -57,7 +57,6 @@ public abstract class SymmetricJwtAlgorithmBase : IJwtAlgorithm, IDisposable int isDisposed; public ReadOnlySpan HeaderBase64Url => header; - ConcurrentBag generatedAlgorithms = new ConcurrentBag(); protected SymmetricJwtAlgorithmBase(byte[] key) { @@ -69,11 +68,9 @@ protected SymmetricJwtAlgorithmBase(byte[] key) Base64.TryToBase64UrlUtf8(alg, buffer, out _); header = buffer.ToArray(); - // Create a local thread version of the hash algorithm instance for thread safety. hash = new ThreadLocal(() => { var newHash = CreateHashAlgorithm(key); - generatedAlgorithms.Add(newHash); return newHash; }, false); } @@ -105,10 +102,6 @@ protected virtual void Dispose(bool disposing) { if (Interlocked.Increment(ref isDisposed) == 1) { - foreach (var item in generatedAlgorithms) - { - item.Dispose(); - } hash.Dispose(); } } @@ -200,7 +193,6 @@ public abstract class RSAJwtAlgorithmBase : IJwtAlgorithm, IDisposable readonly ThreadLocal publicKey; readonly ThreadLocal privateKey; - readonly ConcurrentBag generatedAlgorithms = new ConcurrentBag(); int isDisposed; byte[] header; @@ -220,7 +212,6 @@ public RSAJwtAlgorithmBase(X509Certificate2 cert) publicKey = new ThreadLocal(() => { var key = cert?.GetRSAPublicKey() ?? publicKeyFactory(); - generatedAlgorithms.Add(key); return key; }, false); @@ -228,7 +219,6 @@ public RSAJwtAlgorithmBase(X509Certificate2 cert) privateKey = new ThreadLocal(() => { var key = cert?.GetRSAPrivateKey() ?? privateKeyFactory(); - generatedAlgorithms.Add(key); return key; }, false); } @@ -275,10 +265,6 @@ protected virtual void Dispose(bool disposing) { if (Interlocked.Increment(ref isDisposed) == 1) { - foreach (var algorithmsValue in generatedAlgorithms) - { - algorithmsValue.Dispose(); - } publicKey.Dispose(); privateKey.Dispose(); } @@ -398,7 +384,6 @@ public abstract class ESJwtAlgorithmBase : IJwtAlgorithm, IDisposable readonly ThreadLocal publicKey; readonly ThreadLocal privateKey; - readonly ConcurrentBag generatedAlgorithms = new ConcurrentBag(); int isDisposed; byte[] header; public ReadOnlySpan HeaderBase64Url => header; @@ -417,7 +402,6 @@ public ESJwtAlgorithmBase(X509Certificate2 cert) publicKey = new ThreadLocal(() => { var key = cert.GetECDsaPublicKey(); - generatedAlgorithms.Add(key); return key; }, false); @@ -425,7 +409,6 @@ public ESJwtAlgorithmBase(X509Certificate2 cert) privateKey = new ThreadLocal(() => { var key = cert.GetECDsaPrivateKey(); - generatedAlgorithms.Add(key); return key; }, false); } @@ -464,10 +447,6 @@ protected virtual void Dispose(bool disposing) { if (Interlocked.Increment(ref isDisposed) == 1) { - foreach (var algorithmsValue in generatedAlgorithms) - { - algorithmsValue.Dispose(); - } publicKey.Dispose(); privateKey.Dispose(); }