Skip to content

Commit 42a4111

Browse files
committed
Disposal improvements for ScriptHost
1 parent 31ead6b commit 42a4111

File tree

12 files changed

+120
-86
lines changed

12 files changed

+120
-86
lines changed

src/WebJobs.Script.WebHost/Diagnostics/FunctionInstanceLogger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ internal FunctionInstanceLogger(
7979
// This exception will cause the function to not get executed.
8080
throw new InvalidOperationException($"Missing function.json for '{shortName}'.");
8181
}
82-
Description.FunctionLogger logInfo = descr.Invoker.LogInfo;
83-
state = new FunctionInstanceMonitor(descr.Metadata, _metrics, item.FunctionInstanceId, logInfo);
82+
state = new FunctionInstanceMonitor(descr.Metadata, _metrics, item.FunctionInstanceId, descr.Invoker.FunctionLogger);
8483

8584
item.Properties[Key] = state;
8685

src/WebJobs.Script/Binding/FunctionTraceBinderProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public static void Create(ExtensionConfigContext context)
2525

2626
Func<string, FunctionDescriptor> funcLookup = context.Config.GetService<Func<string, FunctionDescriptor>>();
2727

28-
// Func<string, FunctionDescriptor> funcLookup = null;
29-
3028
if (funcLookup != null && inner != null)
3129
{
3230
IBindingProvider wrapper = new Wrapper(inner, funcLookup);
@@ -86,10 +84,10 @@ private async Task<IValueProvider> WrapAsync(IValueProvider result, ValueBinding
8684
{
8785
var shortName = context.FunctionContext.MethodName;
8886
FunctionDescriptor descr = _parent._funcLookup(shortName);
89-
var logInfo = descr.Invoker.LogInfo;
87+
var functionLogger = descr.Invoker.FunctionLogger;
9088

9189
// This is the critical call
92-
trace = logInfo.CreateUserTraceWriter(trace);
90+
trace = functionLogger.CreateUserTraceWriter(trace);
9391

9492
return new SimpleValueProvider(typeof(TraceWriter), trace, result.ToInvokeString());
9593
}

src/WebJobs.Script/Description/FunctionInvokerBase.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ public abstract class FunctionInvokerBase : IFunctionInvoker, IDisposable
2323
private bool _disposed = false;
2424
private IDisposable _fileChangeSubscription;
2525

26-
internal FunctionInvokerBase(ScriptHost host, FunctionMetadata functionMetadata)
27-
: this(host, functionMetadata, new FunctionLogger(host, functionMetadata.Name))
28-
{
29-
}
30-
31-
internal FunctionInvokerBase(ScriptHost host, FunctionMetadata functionMetadata, FunctionLogger logInfo)
26+
internal FunctionInvokerBase(ScriptHost host, FunctionMetadata functionMetadata, string logDirName = null)
3227
{
3328
Host = host;
3429
Metadata = functionMetadata;
35-
LogInfo = logInfo;
30+
FunctionLogger = new FunctionLogger(host, logDirName ?? functionMetadata.Name);
3631
}
3732

3833
protected static IDictionary<string, object> PrimaryHostTraceProperties { get; }
@@ -46,15 +41,15 @@ internal FunctionInvokerBase(ScriptHost host, FunctionMetadata functionMetadata,
4641

4742
public ScriptHost Host { get; }
4843

49-
public FunctionLogger LogInfo { get; }
44+
public FunctionLogger FunctionLogger { get; }
5045

5146
public FunctionMetadata Metadata { get; }
5247

53-
protected TraceWriter TraceWriter => LogInfo.TraceWriter;
48+
protected TraceWriter TraceWriter => FunctionLogger.TraceWriter;
5449

55-
protected ILogger Logger => LogInfo.Logger;
50+
protected ILogger Logger => FunctionLogger.Logger;
5651

57-
public TraceWriter FileTraceWriter => LogInfo.FileTraceWriter;
52+
public TraceWriter FileTraceWriter => FunctionLogger.FileTraceWriter;
5853

5954
/// <summary>
6055
/// All unhandled invocation exceptions will flow through this method.
@@ -70,7 +65,7 @@ public virtual void OnError(Exception ex)
7065

7166
protected virtual void TraceError(string errorMessage)
7267
{
73-
LogInfo.TraceError(errorMessage);
68+
FunctionLogger.TraceError(errorMessage);
7469
}
7570

7671
protected bool InitializeFileWatcherIfEnabled()
@@ -155,7 +150,7 @@ internal void TraceCompilationDiagnostics(ImmutableArray<Diagnostic> diagnostics
155150
return;
156151
}
157152

158-
TraceWriter traceWriter = LogInfo.TraceWriter;
153+
TraceWriter traceWriter = FunctionLogger.TraceWriter;
159154
IDictionary<string, object> properties = PrimaryHostTraceProperties;
160155

161156
if (!logTarget.HasFlag(LogTargets.User))
@@ -208,7 +203,7 @@ protected virtual void Dispose(bool disposing)
208203
{
209204
_fileChangeSubscription?.Dispose();
210205

211-
(LogInfo.TraceWriter as IDisposable)?.Dispose();
206+
FunctionLogger.Dispose();
212207
}
213208

214209
_disposed = true;

src/WebJobs.Script/Description/FunctionLogger.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Diagnostics;
67
using Microsoft.Azure.WebJobs.Host;
@@ -11,8 +12,10 @@ namespace Microsoft.Azure.WebJobs.Script.Description
1112
{
1213
// Each function can get its own log stream.
1314
// Static per-function logging information.
14-
public class FunctionLogger
15+
public class FunctionLogger : IDisposable
1516
{
17+
private bool _disposed = false;
18+
1619
public FunctionLogger(ScriptHost host, string functionName, string logDirName = null)
1720
{
1821
// Function file logging is only done conditionally
@@ -82,5 +85,25 @@ public void LogFunctionResult(bool success, string invocationId, long elapsedMs)
8285
TraceWriter.Trace(message, traceWriterLevel, null);
8386
Logger?.Log(logLevel, new EventId(0), message, null, (s, e) => s);
8487
}
88+
89+
protected virtual void Dispose(bool disposing)
90+
{
91+
if (!_disposed)
92+
{
93+
if (disposing)
94+
{
95+
(FileTraceWriter as IDisposable)?.Dispose();
96+
(TraceWriter as IDisposable)?.Dispose();
97+
}
98+
99+
_disposed = true;
100+
}
101+
}
102+
103+
public void Dispose()
104+
{
105+
Dispose(true);
106+
GC.SuppressFinalize(this);
107+
}
85108
}
86109
}

src/WebJobs.Script/Description/IFunctionInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IFunctionInvoker
1111
/// <summary>
1212
/// Gets logging information for this function.
1313
/// </summary>
14-
FunctionLogger LogInfo { get; }
14+
FunctionLogger FunctionLogger { get; }
1515

1616
/// <summary>
1717
/// Invoke the function using the specified parameters.

src/WebJobs.Script/Description/Proxies/ProxyFunctionInvoker.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
7-
using System.Net.Http;
8-
using System.Text;
96
using System.Threading.Tasks;
10-
using Microsoft.Azure.AppService.Proxy.Client.Contract;
117
using Microsoft.AspNetCore.Http;
128

139
namespace Microsoft.Azure.WebJobs.Script.Description
@@ -17,7 +13,7 @@ internal class ProxyFunctionInvoker : FunctionInvokerBase
1713
private ProxyClientExecutor _proxyClient;
1814

1915
public ProxyFunctionInvoker(ScriptHost host, FunctionMetadata functionMetadata, ProxyClientExecutor proxyClient)
20-
: base(host, functionMetadata, new FunctionLogger(host, functionMetadata.Name, logDirName: "Proxy"))
16+
: base(host, functionMetadata, logDirName: "Proxy")
2117
{
2218
_proxyClient = proxyClient;
2319
}

src/WebJobs.Script/Host/PrimaryHostCoordinator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ private void ResetLease()
203203

204204
private void SetTimerInterval(TimeSpan interval, TimeSpan? dueTimeout = null)
205205
{
206-
_timer.Change(dueTimeout ?? interval, interval);
206+
if (!_disposed)
207+
{
208+
_timer.Change(dueTimeout ?? interval, interval);
209+
}
207210
}
208211

209212
private void TryReleaseLeaseIfOwned()

0 commit comments

Comments
 (0)