Skip to content

Commit 9fd02c3

Browse files
committed
Fixing disposal issue + test timing issue
1 parent fbbb47a commit 9fd02c3

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/WebJobs.Script/Host/ScriptHostManager.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public bool CanInvoke()
150150
_consecutiveErrorCount = 0;
151151
do
152152
{
153+
ScriptHost newInstance = null;
153154
try
154155
{
155156
// if we were in an error state retain that,
@@ -166,22 +167,23 @@ public bool CanInvoke()
166167
};
167168
OnInitializeConfig(_config);
168169

169-
_currentInstance = _scriptHostFactory.Create(_environment, EventManager, _settingsManager, _config);
170+
newInstance = _scriptHostFactory.Create(_environment, EventManager, _settingsManager, _config);
171+
_currentInstance = newInstance;
170172
lock (_liveInstances)
171173
{
172-
_liveInstances.Add(_currentInstance);
174+
_liveInstances.Add(newInstance);
173175
_hostStartCount++;
174176
}
175177

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();
180182

181-
_currentInstance.StartAsync(cancellationToken).GetAwaiter().GetResult();
183+
newInstance.StartAsync(cancellationToken).GetAwaiter().GetResult();
182184

183185
// log any function initialization errors
184-
LogErrors(_currentInstance);
186+
LogErrors(newInstance);
185187

186188
LastError = null;
187189
_consecutiveErrorCount = 0;
@@ -204,7 +206,7 @@ public bool CanInvoke()
204206
// Orphan the current host instance. We're stopping it, so it won't listen for any new functions
205207
// it will finish any currently executing functions and then clean itself up.
206208
// Spin around and create a new host instance.
207-
Task.Run(() => Orphan(_currentInstance)
209+
Task.Run(() => Orphan(newInstance)
208210
.ContinueWith(t =>
209211
{
210212
if (t.IsFaulted)
@@ -240,9 +242,9 @@ public bool CanInvoke()
240242

241243
// If a ScriptHost instance was created before the exception was thrown
242244
// Orphan and cleanup that instance.
243-
if (_currentInstance != null)
245+
if (newInstance != null)
244246
{
245-
Task.Run(() => Orphan(_currentInstance, forceStop: true)
247+
Task.Run(() => Orphan(newInstance, forceStop: true)
246248
.ContinueWith(t =>
247249
{
248250
if (t.IsFaulted)

test/WebJobs.Script.Tests.Integration/Host/StandbyManagerTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,17 @@ public async Task StandbyMode_EndToEnd()
122122
var hostConfig = WebHostResolver.CreateScriptHostConfiguration(webHostSettings, true);
123123
var expectedHostId = hostConfig.HostConfig.HostId;
124124

125-
string[] logLines = traceWriter.Traces.Select(p => p.Message).ToArray();
125+
string[] logLines = null;
126+
await TestHelpers.Await(() =>
127+
{
128+
// wait for the trace indicating that the host has been specialized
129+
logLines = traceWriter.Traces.Select(p => p.Message).ToArray();
130+
return logLines.Count(p => p.Contains($"Starting Host (HostId={expectedHostId}")) == 1;
131+
});
132+
133+
// verify the rest of the expected logs
126134
string text = string.Join(Environment.NewLine, logLines);
127-
Assert.True(logLines.Count(p => p.Contains("Stopping Host")) > 1);
135+
Assert.True(logLines.Count(p => p.Contains("Stopping Host")) >= 1);
128136
Assert.Equal(1, logLines.Count(p => p.Contains("Creating StandbyMode placeholder function directory")));
129137
Assert.Equal(1, logLines.Count(p => p.Contains("StandbyMode placeholder function directory created")));
130138
Assert.Equal(2, logLines.Count(p => p.Contains("Starting Host (HostId=placeholder-host")));

0 commit comments

Comments
 (0)