Skip to content

Commit f84f18d

Browse files
authored
Merge pull request #143 from EvotecIT/feature/worker-startup-cleanup
Fix background thread leaks from rejected worker construction
2 parents bf03621 + 28f23aa commit f84f18d

3 files changed

Lines changed: 63 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 1.1.1
22

3+
- Fixed background thread leaks when worker construction is rejected
34
- Fixed canonical DOM node identity and concrete prototypes through `DomSameObject` getters (#140)
45

56
# 1.1.0

‎src/AngleSharp.Js.Tests/WorkerTests.cs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
namespace AngleSharp.Js.Tests
22
{
3+
using AngleSharp.Dom;
34
using AngleSharp.Io;
45
using AngleSharp.Js.Tests.Mocks;
56
using NUnit.Framework;
67
using System;
8+
using System.Diagnostics;
79
using System.Threading.Tasks;
810

911
[TestFixture]
@@ -90,6 +92,34 @@ public async Task WorkerShouldRequireResourceLoading()
9092
Assert.AreEqual("true", result);
9193
}
9294

95+
[Test]
96+
[NonParallelizable]
97+
public async Task RejectedWorkersShouldNotLeaveRunningThreads()
98+
{
99+
using (var context = BrowsingContext.New(Configuration.Default.WithJs()))
100+
{
101+
var document = await context.OpenAsync(request => request.Content("<!doctype html>")).ConfigureAwait(false);
102+
Assert.Throws<DomException>(() => new AngleSharp.Js.Dom.Worker(document.DefaultView, "/worker.js"));
103+
var before = CountProcessThreads();
104+
105+
for (var i = 0; i < 32; i++)
106+
{
107+
Assert.Throws<DomException>(() => new AngleSharp.Js.Dom.Worker(document.DefaultView, "/worker.js"));
108+
}
109+
110+
// Allow unrelated runtime threads without hiding one surviving loop per failure.
111+
Assert.Less(CountProcessThreads() - before, 16);
112+
}
113+
}
114+
115+
private static Int32 CountProcessThreads()
116+
{
117+
using (var process = Process.GetCurrentProcess())
118+
{
119+
return process.Threads.Count;
120+
}
121+
}
122+
93123
[Test]
94124
public async Task WorkerDirectPostToOwnerShouldRaiseMessage()
95125
{
@@ -133,4 +163,4 @@ private static async Task WaitForResultAsync(AngleSharp.Dom.IDocument document,
133163
}
134164
}
135165
}
136-
}
166+
}

‎src/AngleSharp.Js/Dom/Worker.cs‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,45 @@ public Worker(IWindow window, String source)
6060

6161
var parentContext = _window.Document.Context;
6262
_scripting = parentContext.GetService<JsScriptingService>() ?? throw new DomException(DomError.NotSupported);
63-
_workerLoop = new JsEventLoop();
64-
var workerConfig = Configuration.Default
65-
.With(_scripting)
66-
.WithOnly(_workerLoop);
67-
_workerContext = BrowsingContext.New(workerConfig);
68-
63+
EnsureLoaderAvailable();
64+
var workerUrl = ResolveUrl(source);
6965
_pendingMessages = new Queue<Object>();
7066

71-
EnsureLoaderAvailable();
67+
// Check the parent's capabilities before starting a thread that a failed constructor
68+
// would leave unreachable. Release it if context creation or scheduling fails too.
69+
_workerLoop = new JsEventLoop();
70+
try
71+
{
72+
var workerConfig = Configuration.Default
73+
.With(_scripting)
74+
.WithOnly(_workerLoop);
75+
_workerContext = BrowsingContext.New(workerConfig);
7276

73-
var workerUrl = ResolveUrl(source);
74-
Enqueue(_workerLoop, TaskPriority.Critical, () =>
77+
Enqueue(_workerLoop, TaskPriority.Critical, () =>
78+
{
79+
try
80+
{
81+
InitializeWorker(workerUrl);
82+
}
83+
catch (Exception ex)
84+
{
85+
_startupError = ex;
86+
}
87+
});
88+
}
89+
catch
7590
{
7691
try
7792
{
78-
InitializeWorker(workerUrl);
93+
_workerContext?.Dispose();
7994
}
80-
catch (Exception ex)
95+
finally
8196
{
82-
_startupError = ex;
97+
((IDisposable)_workerLoop).Dispose();
8398
}
84-
});
99+
100+
throw;
101+
}
85102
}
86103

87104
#endregion
@@ -289,4 +306,4 @@ private static void Enqueue(IEventLoop loop, TaskPriority priority, Action actio
289306
#endregion
290307

291308
}
292-
}
309+
}

0 commit comments

Comments
 (0)