Skip to content

Commit 7c2371b

Browse files
committed
Making debug_sentinel touch more resilient (#1212)
1 parent 708ed02 commit 7c2371b

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,28 @@ public void NotifyDebug()
174174
// file. However, we leave this here for assurances.
175175
LastDebugNotify = DateTime.UtcNow;
176176

177-
// create or update the debug sentinel file to trigger a
178-
// debug timeout update across all instances
179-
string debugSentinelFileName = Path.Combine(ScriptConfig.RootLogPath, "Host", ScriptConstants.DebugSentinelFileName);
180-
if (!File.Exists(debugSentinelFileName))
177+
try
181178
{
182-
File.WriteAllText(debugSentinelFileName, "This is a system managed marker file used to control runtime debug mode behavior.");
179+
// create or update the debug sentinel file to trigger a
180+
// debug timeout update across all instances
181+
string debugSentinelFileName = Path.Combine(ScriptConfig.RootLogPath, "Host", ScriptConstants.DebugSentinelFileName);
182+
if (!File.Exists(debugSentinelFileName))
183+
{
184+
File.WriteAllText(debugSentinelFileName, "This is a system managed marker file used to control runtime debug mode behavior.");
185+
}
186+
else
187+
{
188+
File.SetLastWriteTimeUtc(debugSentinelFileName, DateTime.UtcNow);
189+
}
183190
}
184-
else
191+
catch (Exception ex)
185192
{
186-
File.SetLastWriteTimeUtc(debugSentinelFileName, DateTime.UtcNow);
193+
// best effort
194+
TraceWriter.Error("Unable to update the debug sentinel file.", ex);
195+
if (ex.IsFatal())
196+
{
197+
throw;
198+
}
187199
}
188200
}
189201

test/WebJobs.Script.Tests/ScriptHostTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,31 @@ public void NotifyDebug_UpdatesDebugMarkerFileAndTimestamp()
149149
Assert.True(host.LastDebugNotify > lastDebugNotify);
150150
}
151151

152+
[Fact]
153+
public void NotifyDebug_HandlesExceptions()
154+
{
155+
ScriptHost host = _fixture.Host;
156+
string debugSentinelFileName = Path.Combine(host.ScriptConfig.RootLogPath, "Host", ScriptConstants.DebugSentinelFileName);
157+
158+
try
159+
{
160+
host.NotifyDebug();
161+
Assert.True(host.InDebugMode);
162+
163+
var attributes = File.GetAttributes(debugSentinelFileName);
164+
attributes |= FileAttributes.ReadOnly;
165+
File.SetAttributes(debugSentinelFileName, attributes);
166+
Assert.True(host.InDebugMode);
167+
168+
host.NotifyDebug();
169+
}
170+
finally
171+
{
172+
File.SetAttributes(debugSentinelFileName, FileAttributes.Normal);
173+
File.Delete(debugSentinelFileName);
174+
}
175+
}
176+
152177
[Fact]
153178
public void Version_ReturnsAssemblyVersion()
154179
{

0 commit comments

Comments
 (0)