Azure / Azure/azure-functions-host

Javascript function execution may leak outside of execution boundaries

Open
#2,117 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

Due to the nature of the Node's event loop and how promises are handled callback execution may outlive the duration of a function leaking resources and clogging the event loop.
One would expect the function context to track execution until the resolution of the last callback or promise instead of just when the function returns.
Doing so would allow for better telemetry, performance analysis and hopefully the possibility of releasing bound resources.

#### Investigative information
- Function App version: 2.0-beta

#### Repro steps

Create the following function
``` javascript
module.exports = async (context) => {
setTimeout(() => {
context.log('Executed after finalization')
}, 5000);
}
```
#### Expected behavior
Function execution should also account for the timeout delay
```
info: Worker.Node.d79b1858-a339-4e4c-ba8f-ba31a5aca3f0[0]
Worker d79b1858-a339-4e4c-ba8f-ba31a5aca3f0 connecting on 127.0.0.1:55241
[11/10/17 7:27:31 PM] Function started (Id=8de11d3b-3841-4f36-8958-02d746feffe2)
[11/10/17 7:27:31 PM] Executing 'Functions.HttpTriggerJS' (Reason='This function was programmatically called via the host APIs.', Id=8de11d3b-3841-4f36-8958-02d746feffe2)
[11/10/17 7:27:31 PM] Function completed (Success, Id=8de11d3b-3841-4f36-8958-02d746feffe2, Duration=5223ms)
[11/10/17 7:27:31 PM] Executed 'Functions.HttpTriggerJS' (Succeeded, Id=8de11d3b-3841-4f36-8958-02d746feffe2)
```
#### Actual behavior
Function execution should also accounts only for the time until the functions returns without taking into account the delayed action.
```
info: Worker.Node.d79b1858-a339-4e4c-ba8f-ba31a5aca3f0[0]
Worker d79b1858-a339-4e4c-ba8f-ba31a5aca3f0 connecting on 127.0.0.1:55241
[11/10/17 7:27:31 PM] Function started (Id=8de11d3b-3841-4f36-8958-02d746feffe2)
[11/10/17 7:27:31 PM] Executing 'Functions.HttpTriggerJS' (Reason='This function was programmatically called via the host APIs.', Id=8de11d3b-3841-4f36-8958-02d746feffe2)
[11/10/17 7:27:31 PM] Function completed (Success, Id=8de11d3b-3841-4f36-8958-02d746feffe2, Duration=223ms)
[11/10/17 7:27:31 PM] Executed 'Functions.HttpTriggerJS' (Succeeded, Id=8de11d3b-3841-4f36-8958-02d746feffe2)
info: Worker.Node.d79b1858-a339-4e4c-ba8f-ba31a5aca3f0[0]
Executed after finalization
```
#### Known workarounds

No known workaronds

#### Related information
Links provided for further mitigation analysis:
https://github.com/nodejs/node-eps/blob/master/006-asynchooks-api.md
https://nodejs.org/api/async_hooks.html#async_hooks_async_hooks_createhook_callbacks
https://github.com/domenic/zones
https://github.com/angular/zone.js

https://stackoverflow.com/questions/42358151/azure-functions-timeout-for-consumption-plan
https://github.com/Azure/azure-webjobs-sdk-script/issues/18
https://github.com/Azure/Azure-Functions/issues/75#issuecomment-285392193
https://stackoverflow.com/questions/40800933/azure-functions-pricing-and-timeout

Contributor guide

Open the contributing guide

Research direction

Start with the JavaScript function entry point in the reproduction and review the linked Node.js async_hooks material to understand how asynchronous callbacks are tracked. Reproduce the delayed setTimeout case; done means function execution remains active through the timeout and the reported duration reflects that delay.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.