@@ -150,6 +150,7 @@ public bool CanInvoke()
150
150
_consecutiveErrorCount = 0 ;
151
151
do
152
152
{
153
+ ScriptHost newInstance = null ;
153
154
try
154
155
{
155
156
// if we were in an error state retain that,
@@ -166,22 +167,23 @@ public bool CanInvoke()
166
167
} ;
167
168
OnInitializeConfig ( _config ) ;
168
169
169
- _currentInstance = _scriptHostFactory . Create ( _environment , EventManager , _settingsManager , _config ) ;
170
+ newInstance = _scriptHostFactory . Create ( _environment , EventManager , _settingsManager , _config ) ;
171
+ _currentInstance = newInstance ;
170
172
lock ( _liveInstances )
171
173
{
172
- _liveInstances . Add ( _currentInstance ) ;
174
+ _liveInstances . Add ( newInstance ) ;
173
175
_hostStartCount ++ ;
174
176
}
175
177
176
- _currentInstance . HostInitializing += OnHostInitializing ;
177
- _currentInstance . HostInitialized += OnHostInitialized ;
178
- _currentInstance . HostStarted += OnHostStarted ;
179
- _currentInstance . Initialize ( ) ;
178
+ newInstance . HostInitializing += OnHostInitializing ;
179
+ newInstance . HostInitialized += OnHostInitialized ;
180
+ newInstance . HostStarted += OnHostStarted ;
181
+ newInstance . Initialize ( ) ;
180
182
181
- _currentInstance . StartAsync ( cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
183
+ newInstance . StartAsync ( cancellationToken ) . GetAwaiter ( ) . GetResult ( ) ;
182
184
183
185
// log any function initialization errors
184
- LogErrors ( _currentInstance ) ;
186
+ LogErrors ( newInstance ) ;
185
187
186
188
LastError = null ;
187
189
_consecutiveErrorCount = 0 ;
@@ -204,7 +206,7 @@ public bool CanInvoke()
204
206
// Orphan the current host instance. We're stopping it, so it won't listen for any new functions
205
207
// it will finish any currently executing functions and then clean itself up.
206
208
// Spin around and create a new host instance.
207
- Task . Run ( ( ) => Orphan ( _currentInstance )
209
+ Task . Run ( ( ) => Orphan ( newInstance )
208
210
. ContinueWith ( t =>
209
211
{
210
212
if ( t . IsFaulted )
@@ -240,9 +242,9 @@ public bool CanInvoke()
240
242
241
243
// If a ScriptHost instance was created before the exception was thrown
242
244
// Orphan and cleanup that instance.
243
- if ( _currentInstance != null )
245
+ if ( newInstance != null )
244
246
{
245
- Task . Run ( ( ) => Orphan ( _currentInstance , forceStop : true )
247
+ Task . Run ( ( ) => Orphan ( newInstance , forceStop : true )
246
248
. ContinueWith ( t =>
247
249
{
248
250
if ( t . IsFaulted )
0 commit comments