Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listeners may be Disposed before StopAsync() is called #2823

Open
brettsam opened this issue Jan 18, 2022 · 0 comments
Open

Listeners may be Disposed before StopAsync() is called #2823

brettsam opened this issue Jan 18, 2022 · 0 comments

Comments

@brettsam
Copy link
Member

The check for _started in FunctionListner is not synchronized across StartAsync() and StopAsync() calls (https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Host/Listeners/FunctionListener.cs#L139).

This means that the following scenario can result in a Listener being disposed before it is stopped:

  1. Initial call to StartAsync() fails, resulting in the retry Task being started.
  2. While in the middle of a retry (calling StartAsync(), but _started is still false), the host is shut down, meaning StopAsync()` is called.
  3. The call to StopAsync() returns quickly without doing anything, meaning the Listener can now be disposed.
  4. The StartAsync() completes and calls StopAsync() here: https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.Host/Listeners/FunctionListener.cs#L119

If a Listener doesn't anticipate this, it is possible that it can invoke some functions on a disposed host, resulting in a HostDisposedException. This happens to the EventHub trigger, for example, because it doesn't seem to do anything in Dispose: https://github.com/Azure/azure-functions-eventhubs-extension/blob/dev/src/Microsoft.Azure.WebJobs.Extensions.EventHubs/Listeners/EventHubListener.cs#L72.

The SB Listener, on the other hand, does handle this so it likely doesn't have this issue: https://github.com/Azure/azure-functions-servicebus-extension/blob/dev/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus/Listeners/ServiceBusListener.cs#L176.

@mathewc / @fabiocav -- this feels like a requirement we should be pushing to the Listeners to me. We could likely make this better by blocking the StopAsync() call if there's any outstanding StartAsync() running, but I'm nervous that it'd introduce some other timing issues. I wanted to get your opinions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant