Skip to content

Commit fa7d33c

Browse files
committed
Fixing race condition in host restart
1 parent d66fd05 commit fa7d33c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/WebJobs.Script/Host/ScriptHostManager.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,15 @@ protected virtual void Dispose(bool disposing)
407407

408408
public virtual void RestartHost()
409409
{
410-
_restartHostEvent.Set();
411-
412-
// we reset the state immediately here to ensure that
410+
// We reset the state immediately here to ensure that
413411
// any external calls to CanInvoke will return false
414-
// immediately after the restart has been initiated
412+
// immediately after the restart has been initiated.
413+
// Note: this state change must be done BEFORE we set
414+
// the event below to avoid a race condition with the
415+
// main host lifetime loop.
415416
State = ScriptHostState.Default;
417+
418+
_restartHostEvent.Set();
416419
}
417420

418421
public virtual void Shutdown()

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,16 @@ public async Task EmptyHost_StartsSuccessfully()
288288
var eventManagerMock = new Mock<IScriptEventManager>();
289289
ScriptHostManager hostManager = new ScriptHostManager(config, eventManagerMock.Object);
290290

291+
// start the host and wait for it to be running
291292
Task runTask = Task.Run(() => hostManager.RunAndBlock());
293+
await TestHelpers.Await(() => hostManager.State == ScriptHostState.Running, timeout: 10000);
292294

295+
// exercise restart
296+
hostManager.RestartHost();
297+
Assert.Equal(ScriptHostState.Default, hostManager.State);
293298
await TestHelpers.Await(() => hostManager.State == ScriptHostState.Running, timeout: 10000);
294299

300+
// stop the host fully
295301
hostManager.Stop();
296302
Assert.Equal(ScriptHostState.Default, hostManager.State);
297303

0 commit comments

Comments
 (0)