Skip to content

Commit 23f241b

Browse files
committed
Fixing race condition in host restart
1 parent b79e872 commit 23f241b

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/WebJobs.Script.WebHost/App_Start/WebApiConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public static void Register(HttpConfiguration config, ScriptSettingsManager sett
8282

8383
private static void AddMessageHandlers(HttpConfiguration config)
8484
{
85-
config.MessageHandlers.Add(new WebScriptHostHandler(config));
8685
config.MessageHandlers.Add(new SystemTraceHandler(config));
86+
config.MessageHandlers.Add(new WebScriptHostHandler(config));
8787
}
8888
}
8989
}

src/WebJobs.Script/Host/ScriptHostManager.cs

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

400400
public virtual void RestartHost()
401401
{
402-
_restartHostEvent.Set();
403-
404-
// we reset the state immediately here to ensure that
402+
// We reset the state immediately here to ensure that
405403
// any external calls to CanInvoke will return false
406-
// immediately after the restart has been initiated
404+
// immediately after the restart has been initiated.
405+
// Note: this state change must be done BEFORE we set
406+
// the event below to avoid a race condition with the
407+
// main host lifetime loop.
407408
State = ScriptHostState.Default;
409+
410+
_restartHostEvent.Set();
408411
}
409412

410413
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
@@ -287,10 +287,16 @@ public async Task EmptyHost_StartsSuccessfully()
287287
var eventManagerMock = new Mock<IScriptEventManager>();
288288
ScriptHostManager hostManager = new ScriptHostManager(config, eventManagerMock.Object);
289289

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

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

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

0 commit comments

Comments
 (0)