@@ -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