Skip to content

Commit 494f676

Browse files
committed
Ensuring scope resolver uses a thread safe collection for child scopes
1 parent 70c62d7 commit 494f676

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/WebJobs.Script.WebHost/DependencyInjection/ScopedResolver.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
5+
using System.Collections.Concurrent;
66
using System.Linq;
77
using System.Threading.Tasks;
88
using DryIoc;
@@ -16,18 +16,18 @@ public ScopedResolver(IContainer resolver, bool isRootResolver = false)
1616
{
1717
Container = resolver ?? throw new ArgumentNullException(nameof(resolver));
1818
IsRootResolver = isRootResolver;
19-
ChildScopes = new HashSet<ServiceScope>();
19+
ChildScopes = new ConcurrentDictionary<ServiceScope, object>();
2020
}
2121

2222
public IContainer Container { get; }
2323

24-
public HashSet<ServiceScope> ChildScopes { get; }
24+
public ConcurrentDictionary<ServiceScope, object> ChildScopes { get; }
2525

2626
public bool IsRootResolver { get; }
2727

2828
public void Dispose()
2929
{
30-
Task childScopeTasks = Task.WhenAll(ChildScopes.Select(s => s.DisposalTask));
30+
Task childScopeTasks = Task.WhenAll(ChildScopes.Keys.Select(s => s.DisposalTask));
3131
Task.WhenAny(childScopeTasks, Task.Delay(5000))
3232
.ContinueWith(t =>
3333
{
@@ -52,9 +52,9 @@ internal ServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
5252
}));
5353

5454
var scope = new ServiceScope(resolver, scopedRoot);
55-
ChildScopes.Add(scope);
55+
ChildScopes.TryAdd(scope, null);
5656

57-
scope.DisposalTask.ContinueWith(t => ChildScopes.Remove(scope));
57+
scope.DisposalTask.ContinueWith(t => ChildScopes.TryRemove(scope, out object _));
5858

5959
return scope;
6060
}

0 commit comments

Comments
 (0)