Azure / Azure/azure-functions-host

Console.WriteLine output before Functions App shuts down is silently discarded (with func.exe tool)

Open
#9,866 6 comments 0 reactions 0 assignees View on GitHub
bug Logging and Metrics
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 10h
Merged PRs (30d)
36

Description

Hello, this is in reference to the closed issue Azure/azure-functions-dotnet-worker#1450. The code snippet shown there to run custom code at shutdown does not work for me when used with the `func` tool at least.

I tried to use the snippet with func (azure functions core tools) version 4.0.4483 and 4.0.5198 (the newest release at the time of this writing) and also extended the code snippet to use a `finally` block. Full code below:

```csharp
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace Examples.AzureFunctionWorkerApp
{

public class Program
{
public static async Task Main()
{
try
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();
Console.WriteLine("Before host.Run()");
await host.RunAsync();
}
finally
{
Console.WriteLine("Finally after host.Run()");
}
}
}
}
```

These are the dependency versions (I just used `func new` to create a dotnet-isolated C# project and did not change anything):

```csproj

net6.0
v4






```

Running this with `func --verbose --debug` the relevant parts of the output (till the end) is this:

```
Azure Functions Core Tools
Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit)
Function Runtime Version: 4.21.1.20667
...
[2023-07-17T13:14:38.340Z] Before host.Run()
[2023-07-17T13:14:38.523Z] {
[2023-07-17T13:14:38.526Z] "ProcessId": 25028,
[2023-07-17T13:14:38.527Z] "RuntimeIdentifier": "win10-x64",
[2023-07-17T13:14:38.528Z] "WorkerVersion": "1.6.0.0",
[2023-07-17T13:14:38.530Z] "ProductVersion": "1.6.0-local202207141250\u002Bbb7aae9c07baca52004877db452762344f0db895",
[2023-07-17T13:14:38.531Z] "FrameworkDescription": ".NET 6.0.20",
[2023-07-17T13:14:38.532Z] "OSDescription": "Microsoft Windows 10.0.22621",
[2023-07-17T13:14:38.533Z] "OSArchitecture": "X64",
[2023-07-17T13:14:38.535Z] "CommandLine": "C:\\workspaces\\func-bug-repro\\bin\\output\\func-bug-repro.dll --host 127.0.0.1 --port 60297 --workerId 637efb9b-59ae-4795-b3c0-488691c82fb9 --requestId 3fcebeab-d776-4b9f-b671-a9c561878b19 --grpcMaxMessageLength 2147483647"

[2023-07-17T13:14:38.537Z] }
[2023-07-17T13:14:38.585Z] Worker process started and initialized.
[2023-07-17T13:14:43.136Z] Host lock lease acquired by instance ID '000000000000000000000000EC47B666'.
[2023-07-17T13:14:47.322Z] Stopping host...
[2023-07-17T13:14:47.334Z] Stopping JobHost
[2023-07-17T13:14:47.340Z] Job host stopped
[2023-07-17T13:14:47.386Z] Host shutdown completed.
```

As you can see, the expected Console.WriteLine output `BeforeStart` is visible, but the `Finally after host.Run()` line is not there.

Full output here: [output.log](https://github.com/Azure/azure-functions-dotnet-worker/files/12069674/output.log)

Of course, I dont' know if this bug actually needs to be fixed here or in https://github.com/Azure/azure-functions-core-tools or maybe https://github.com/Azure/azure-functions-host/, nor do I know how to find out, so I'm starting here. Please transfer the issue or request more information from me if you think it belongs somewhere else.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the sample in Program.Main with Azure Functions Core Tools using `func --verbose --debug`, and compare the `host.RunAsync()` shutdown sequence with output.log. Trace whether the missing `Finally after host.Run()` output belongs to the worker, Core Tools, or Functions host. Done means the responsible component and shutdown behavior are documented, with a verified fix or a clear transfer.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.