Referring back to #226 and #223.
The node module Request version 2.34.0 is not working on Azure Functions. Packages such as YQL are depending on it. Request version 2.70.0 is working correctly on Azure Functions.
The below code snippet is working fine on request version 2.70.0, on version 2.34.0, however, the callback is never called.
var request = require('request');
module.exports = function (context) {
request('http://www.bing.com', function (error, response, body) {
// This callback function will never be called on version 2.34.0
if (error) {
context.log(error);
}
if (!error && response.statusCode == 200) {
context.log(body) // Show the HTML for the Bing homepage.
}
context.done();
});
};
Maybe that older request version is not compatible with Node v5.9.1?
Just ran it locally on my machine with Node v5.9.1 and it works with request version 2.34.0.
Not sure, we'll need to investigate to see what's going on.
We think we have a fix for this soon.
Cool thanks! Do you have an estimate as to when this fix will be rolled out?
It's in our current milestone. It'll be whenever @mathewc has a chance to pick it up. He thought he knew the root cause when I posted my response. Sorry if it's taking a little long.
I've done some investigating here -- the specific commit to request that fixes things is here: https://github.com/request/request/commit/27dfc1177e01025da6d3b09a0fd3406b90596719.
The issue looks to be around calling process.nextTick from Edge. Executing this code in Edge.Func() hangs as well:
return function(arg, callback) {
process.nextTick(function() {
console.log('done');
callback();
});
};
If you replace process.nextTick with setImmediate, things work. I see others reporting the same thing over at the Edge repo: https://github.com/tjanczuk/edge/issues/325. I'll see if there's a workaround or fix for this -- otherwise, it appears that any package that uses process.nextTick will hang.
This is probably the same underlying cause of https://github.com/Azure/azure-webjobs-sdk-script/issues/223 right (YQL not working, hanging on request).
Yeah, this is the same underlying issue. I started looking at the YQL problem before I noticed this one, but this one is closer to the real problem (even though it's older).
So one way to get this working is to replace process.nextTick: process.nextTick = global.setImmediate;
That seems pretty gnarly, but gets this sample working. Would any node experts care to berate me for this workaround? :-)
@danderson00 and @christopheranderson, any ideas?
lol yes, will definitely berate you for suggesting such a workaround!
It is almost cetainly related to edge and setImmediate. Is there a newer version of edge we can upgrade to?
A weird workaround is suggested at https://github.com/tjanczuk/edge/issues/325#issuecomment-164027027 - add a call to console.log before calling the function containing the call to process.nextTick. Could be worth investigating.
I saw that console.log() trick and tried it but wasn't able to get it to work.
Now one other interesting bit -- I'm trying to write a test for my PR and cannot get the hang to repro (before my change). It repros when I F5 from the sample I put together, but not when invoked via a test. This seems to indicate that there's some way to make it work without swapping out process.nextTick.
What's different between how we run the host in the tests vs how we run it when I F5 WebJobs.Script.Host?
After some more experimenting, I figured out why the tests pass before my change was in.
In a pure Edge app --
We had one test that included a timer that fired every second and made an async call from within the function. So my NextTick test is sitting there stuck, the timer fires, which unsticks me, and the tests pass. I've got a small repro that I'll be posting to the Edge issue to help with debugging.
PR #486 works around this issue. If you want to get things working before this change is deployed to production, you can add this to the your code before you make the call to request: process.nextTick = global.setImmediate;
@brettsam Still not working on my side (Version ~0.3, added process.tick = global.setImmediate)
The following project is deployed:
https://github.com/arafato/azure-news-explorer-api
Specifically, the function news-import which is triggered on a recurring schedule never completes but always gets stuck somwhere in newsImporter.updateNews(...)
Works fine locally.
Happy to walk you through the code.
Also, the cold start of the function news takes around 15 seconds! Deployed project lives here azurenewsexplorer.azurewebsites.net/.
@arafato -- you said process.tick -- it should be process.nextTick. If that still doesn't work, let me know and I'll try pulling your project and repro'ing it.
Sorry, that was a typo. It's process.nextTick in the code on Azure. The code repository does not include this code line by the way. You might need help to set it up since the function is talking to the Azure Search service.
So be sure to set the environment variables SEARCH_API_KEY and SEARCH_ENDPOINT. Happy to send the values / credentials to you via private message to save you time.
I landed on this issue for an EdgeJS hang inside of Azure Functions, but the real problem for me wasn't setImmediate but using async/await without using a try/catch to capture errors occurring. The promise was erroring out but my process was exiting and never flushing the error logs. Once I delayed the process.exit() to allow my logger to flush it became apparent. Best not to use process.exit in Azure functions!
Hi azure function is not recognising the requests module . pasted the error and code please suggest the fix
exception:
File "D:\home\site\wwwroot\test\run.py", line 1, in
import requests
ImportError: No module named requests
.
python code:
import requests
import timeit
import datetime
def run_gomupload():
try:
start_time = timeit.default_timer()
cdate =datetime.datetime.now()
print('Cell Start Time: '+ str(cdate))
# code you want to evaluate
elapsed = timeit.default_timer() - start_time
cdate=datetime.datetime.now()
print('\nCell End Time: '+ str(cdate))
print('Cell Run Duration: '+ str(elapsed))
# Clean up resources. This includes the container and the temp files
#os.remove(full_path_to_file)
except Exception as e:
print(e)
if __name__ == '__main__':
run_gomupload()
Most helpful comment
I've done some investigating here -- the specific commit to request that fixes things is here: https://github.com/request/request/commit/27dfc1177e01025da6d3b09a0fd3406b90596719.
The issue looks to be around calling
process.nextTickfrom Edge. Executing this code inEdge.Func()hangs as well:If you replace
process.nextTickwithsetImmediate, things work. I see others reporting the same thing over at the Edge repo: https://github.com/tjanczuk/edge/issues/325. I'll see if there's a workaround or fix for this -- otherwise, it appears that any package that usesprocess.nextTickwill hang.